def execute_Threshold(proxy,obj):
try: img=obj.sourceObject.Proxy.img.copy()
except: img=cv2.imread(__dir__+'/icons/freek.png')
# img = cv2.imread('dave.jpg',0) ??
img = cv2.medianBlur(img,5)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if obj.globalThresholding:
ret,th1 = cv2.threshold(img,obj.param1,obj.param2,cv2.THRESH_BINARY)
obj.Proxy.img = cv2.cvtColor(th1, cv2.COLOR_GRAY2RGB)
if obj.adaptiveMeanTresholding:
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,11,2)
obj.Proxy.img = cv2.cvtColor(th2, cv2.COLOR_GRAY2RGB)
if obj.adaptiveGaussianThresholding:
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,17,2)
obj.Proxy.img = cv2.cvtColor(th3, cv2.COLOR_GRAY2RGB)
python类medianBlur()的实例源码
Artificial-potential-controller-2.py 文件源码
项目:Artificial-Potential-Field
作者: vampcoder
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def classify(img):
cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = cv2.medianBlur(cimg, 13)
ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
t2 = copy.copy(thresh1)
x, y = thresh1.shape
arr = np.zeros((x, y, 3), np.uint8)
final_contours = []
image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cv2.imshow('image', image)
#k = cv2.waitKey(0)
for i in range(len(contours)):
cnt = contours[i]
if cv2.contourArea(cnt) > 3000 and cv2.contourArea(cnt) < 25000:
cv2.drawContours(img, [cnt], -1, [0, 255, 255])
cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
final_contours.append(cnt)
#cv2.imshow('arr', arr)
#k = cv2.waitKey(0)
return arr
def createTrainingData(filename,time_start,time_stop):
vidcap = cv2.VideoCapture(filename)
try:
os.makedirs("trainingdata_"+filename)
except OSError:
pass
os.chdir("trainingdata_"+filename)
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(vidcap.get(cv2.CAP_PROP_FPS))
for time in range(time_start,time_stop):
vidcap.set(cv2.CAP_PROP_POS_MSEC,time*1000)
success,image = vidcap.read()
image = cv2.medianBlur(image,7)
resized = imutils.resize(image, width=800)
p1 = resized[370:430,220:300]
p2 = resized[370:430,520:600]
p1 = cv2.Canny(p1, 400, 100, 255)
p2 = cv2.Canny(p2, 400, 100, 255)
cv2.imwrite('p1_'+str(time)+".png",p1)
cv2.imwrite('p2_'+str(time)+".png",p2)
os.chdir("..")
def dif_gaus(image, lower, upper):
lower, upper = int(lower-1), int(upper-1)
lower = cv2.GaussianBlur(image,ksize=(lower,lower),sigmaX=0)
upper = cv2.GaussianBlur(image,ksize=(upper,upper),sigmaX=0)
# upper +=50
# lower +=50
dif = lower-upper
# dif *= .1
# dif = cv2.medianBlur(dif,3)
# dif = 255-dif
dif = cv2.inRange(dif, np.asarray(200),np.asarray(256))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
dif = cv2.dilate(dif, kernel, iterations=2)
dif = cv2.erode(dif, kernel, iterations=1)
# dif = cv2.max(image,dif)
# dif = cv2.dilate(dif, kernel, iterations=1)
return dif
def skin_detect(self, raw_yrb, img_src):
# use median blurring to remove signal noise in YCRCB domain
raw_yrb = cv2.medianBlur(raw_yrb, 5)
mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb)
# morphological transform to remove unwanted part
kernel = np.ones((5, 5), np.uint8)
#mask_skin = cv2.morphologyEx(mask_skin, cv2.MORPH_OPEN, kernel)
mask_skin = cv2.dilate(mask_skin, kernel, iterations=2)
res_skin = cv2.bitwise_and(img_src, img_src, mask=mask_skin)
#res_skin_dn = cv2.fastNlMeansDenoisingColored(res_skin, None, 10, 10, 7,21)
return res_skin
# Do background subtraction with some filtering
def predict(url):
global model
# Read image
image = io.imread(url)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
# Use otsu to mask
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
mask = cv2.medianBlur(mask, 5)
features = describe(image, mask)
state = le.inverse_transform(model.predict([features]))[0]
return {'type': state}
def read_captured_circles(self):
img = cv2.cvtColor(self.query, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(img, 7)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 30,
param1=50, param2=30, minRadius=20, maxRadius=50)
if circles is None:
return
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
if i[1] < 400:
continue
self.circlePoints.append((i[0], i[1]))
if self._debug:
self.draw_circles(circles, cimg)
Artificial-potential-without-controller.py 文件源码
项目:Artificial-Potential-Field
作者: vampcoder
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def classify(img):
cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = cv2.medianBlur(cimg, 13)
ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
t2 = copy.copy(thresh1)
x, y = thresh1.shape
arr = np.zeros((x, y, 3), np.uint8)
final_contours = []
image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cv2.imshow('image', image)
#k = cv2.waitKey(0)
for i in range(len(contours)):
cnt = contours[i]
if cv2.contourArea(cnt) > 35000 and cv2.contourArea(cnt) < 15000:
cv2.drawContours(img, [cnt], -1, [0, 255, 255])
cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
final_contours.append(cnt)
cv2.imshow('arr', arr)
k = cv2.waitKey(0)
return arr
Artificial-potential-controller.py 文件源码
项目:Artificial-Potential-Field
作者: vampcoder
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def classify(img):
cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = cv2.medianBlur(cimg, 13)
ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
t2 = copy.copy(thresh1)
x, y = thresh1.shape
arr = np.zeros((x, y, 3), np.uint8)
final_contours = []
image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cv2.imshow('image', image)
#k = cv2.waitKey(0)
for i in range(len(contours)):
cnt = contours[i]
if cv2.contourArea(cnt) > 3600 and cv2.contourArea(cnt) < 25000:
cv2.drawContours(img, [cnt], -1, [0, 255, 255])
cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
final_contours.append(cnt)
cv2.imshow('arr', arr)
k = cv2.waitKey(0)
return arr
def _process_image(self, image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsv = cv2.medianBlur(hsv, 5)
draw_col = (0,0,255)
p1 = (self.edges['l'], self.edges['d'])
p2 = (self.edges['r'], self.edges['u'])
cv2.rectangle(hsv, p1, p2, draw_col)
vert_spacing = (self.edges['r'] - self.edges['l'])/float(len(grid))
for i in range(1, len(grid)):
x_pos = int(self.edges['l'] + i*vert_spacing)
p1 = (x_pos, self.edges['d'])
p2 = (x_pos, self.edges['u'])
cv2.line(hsv, p1, p2, draw_col)
horiz_spacing = (self.edges['d'] - self.edges['u'])/float(len(grid[0]))
for i in range(1, len(grid[0])):
y_pos = int(self.edges['u'] + i*horiz_spacing)
p1 = (self.edges['l'], y_pos)
p2 = (self.edges['r'], y_pos)
cv2.line(hsv, p1, p2, draw_col)
return hsv
def _detect_bot(self, hsv_image):
BOT_MIN = np.array([28,8,100], np.uint8)
BOT_MAX = np.array([32,255,255], np.uint8)
thresholded_image = cv2.inRange(hsv_image, BOT_MIN, BOT_MAX)
thresholded_image = cv2.medianBlur(thresholded_image, 15)
_, contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
(bot_x, bot_y) = (-1000,-1000)
else:
bot = contours[0]
M = cv2.moments(bot)
if len(bot) > 2:
bot_x = int(M['m10']/M['m00'])
bot_y = int(M['m01']/M['m00'])
else:
(bot_x, bot_y) = (-1000,-1000)
return thresholded_image, (bot_x, bot_y)
def process(img):
img=cv2.medianBlur(img,5)
kernel=np.ones((3,3),np.uint8)
#img=cv2.erode(img,kernel,iterations = 1)
sobel = cv2.Sobel(img, cv2.CV_8U, 1, 0, ksize = 3)
element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 1))
element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
dilation = cv2.dilate(sobel, element2, iterations = 1)
erosion = cv2.erode(dilation, element1, iterations = 1)
dilation2 = cv2.dilate(erosion, element2,iterations = 3)
#img=cv2.dilate(img,kernel,iterations = 1)
#img=cv2.Canny(img,100,200)
return dilation2
motionDetectionPiMultiProcessing_COM_LiveFeed.py 文件源码
项目:smart-cam
作者: smart-cam
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def frameBlurrer(writer_blurrer_filename_Queue, blur_to_motiondetector_blurred_Queue):
while True:
BLURS = list()
FRAMES = list()
filename = writer_blurrer_filename_Queue.get()
t1 = time.time()
camera = cv2.VideoCapture(filename)
for counter in xrange(0, FRAMES_PER_CLIP):
ret, frame = camera.read()
FRAMES.append(frame)
camera.release()
while len(FRAMES) > 0:
frame = FRAMES.pop(0)
blurred = cv2.medianBlur(frame, 9)
BLURS.append(blurred)
print "Blurred", time.time() - t1
# Sending blurs to motion detector
blur_to_motiondetector_blurred_Queue.put((filename, BLURS))
del filename
del BLURS
return
def extracttext(imgpath, preprocess):
if imgpath.startswith('http://') or imgpath.startswith('https://') or imgpath.startswith('ftp://'):
image = url_to_image(imgpath)
else:
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif preprocess == "blur":
gray = cv2.medianBlur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
return {"text": text}
def __blur(src, type, radius):
"""Softens an image using one of several filters.
Args:
src: The source mat (numpy.ndarray).
type: The blurType to perform represented as an int.
radius: The radius for the blur as a float.
Returns:
A numpy.ndarray that has been blurred.
"""
if(type is BlurType.Box_Blur):
ksize = int(2 * round(radius) + 1)
return cv2.blur(src, (ksize, ksize))
elif(type is BlurType.Gaussian_Blur):
ksize = int(6 * round(radius) + 1)
return cv2.GaussianBlur(src, (ksize, ksize), round(radius))
elif(type is BlurType.Median_Filter):
ksize = int(2 * round(radius) + 1)
return cv2.medianBlur(src, ksize)
else:
return cv2.bilateralFilter(src, -1, round(radius), round(radius))
def render(self,frame):
numDownSamples = 2
img_rgb = frame
# number of downscaling steps
numBilateralFilters = 7
# number of bilateral filtering steps
# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in xrange(numDownSamples):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in xrange(numBilateralFilters):
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
# upsample image to original size
for _ in xrange(numDownSamples):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
# -- STEP 5 --
# convert back to color so that it can be bit-ANDed with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
final = cv2.bitwise_and(img_color, img_edge)
return cv2.medianBlur(final,7)
def render(self,frame):
canvas = cv2.imread("pen.jpg", cv2.CV_8UC1)
numDownSamples = 2
img_rgb = frame
# number of downscaling steps
numBilateralFilters = 3
# number of bilateral filtering steps
# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in xrange(numDownSamples):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in xrange(numBilateralFilters):
img_color = cv2.bilateralFilter(img_color, 9, 9, 3)
# upsample image to original size
for _ in xrange(numDownSamples):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 3)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
return cv2.multiply(cv2.medianBlur(img_edge,7), canvas, scale=1./256)
def median_blur(im, size=3):
return cv2.medianBlur(im, size)
def getDepth(self):
"""
Return a median smoothed depth image
:return: depth data as numpy array
"""
if self.mirror:
depth = dsc.getDepthMap()[:, ::-1]
else:
depth = dsc.getDepthMap()
depth = cv2.medianBlur(depth, 3)
return (numpy.count_nonzero(depth) != 0), numpy.asarray(depth, numpy.float32)
def equalize(image, image_lower=0.0, image_upper=255.0):
image_lower = int(image_lower*2)/2
image_lower +=1
image_lower = max(3,image_lower)
mean = cv2.medianBlur(image,255)
image = image - (mean-100)
# kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
# cv2.dilate(image, kernel, image, iterations=1)
return image
def background_subtract(self, img_src):
fgmask = self.fgbg.apply(cv2.GaussianBlur(img_src, (25, 25), 0))
kernel = np.ones((5, 5), np.uint8)
fgmask = cv2.dilate(fgmask, kernel, iterations=2)
#fgmask = self.fgbg.apply(cv2.medianBlur(img_src, 11))
org_fg = cv2.bitwise_and(img_src, img_src, mask=fgmask)
return org_fg
# Update Position of ROI
def medianBlur(srcpath, dstpath):
img = cv2.imread(srcpath, 0)
blur = cv2.medianBlur(img, 3)
# cv2.imshow(dstpath, img)
# cv2.imwrite(dstpath, blur)
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(blur,'gray')
plt.show()
# ????
def filter_smooth_thres(self, RANGE, color):
for (lower, upper) in RANGE:
lower = np.array(lower, dtype='uint8')
upper = np.array(upper, dtype='uint8')
mask_bottom = cv2.inRange(self.img_roi_bottom_hsv, lower, upper)
mask_top = cv2.inRange(self.img_roi_top_hsv, lower, upper)
blurred_bottom = cv2.medianBlur(mask_bottom, 5)
blurred_top = cv2.medianBlur(mask_top, 5)
# Morphological transformation
kernel = np.ones((2, 2), np.uint8)
smoothen_bottom = blurred_bottom #cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)
smoothen_top = blurred_top # cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)
"""
if self.debug:
cv2.imshow('mask bottom ' + color, mask_bottom)
cv2.imshow('blurred bottom' + color, blurred_bottom)
cv2.imshow('mask top ' + color, mask_top)
cv2.imshow('blurred top' + color, blurred_top)
"""
return smoothen_bottom, smoothen_top
# Gets metadata from our contours
def predict(url):
global model, COOKED_PHRASES, RAW_PHRASES
# Read image
image = io.imread(url)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
# Use otsu to mask
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
mask = cv2.medianBlur(mask, 5)
# Get features
features = describe(image, mask)
# Predict it
result = model.predict([features])
probability = model.predict_proba([features])[0][result][0]
state = le.inverse_transform(result)[0]
phrase = ''
if 'cook' in state:
phrase = COOKED_PHRASES[int(random.random()*len(COOKED_PHRASES))]
elif 'raw' in state:
phrase = RAW_PHRASES[int(random.random()*len(RAW_PHRASES))]
return {'type': state, 'confidence': probability, 'phrase': phrase}
def pretty_blur_map(blur_map, sigma=5):
abs_image = numpy.log(numpy.abs(blur_map).astype(numpy.float32))
cv2.blur(abs_image, (sigma, sigma))
return cv2.medianBlur(abs_image, sigma)
def reduce_noise_raw(im):
bilat = cv2.bilateralFilter(im, 9, 75, 75)
blur = cv2.medianBlur(bilat, 5)
return blur
def median_fltr_opencv(dem, size=3, iterations=1):
"""OpenCV median filter
"""
import cv2
dem = malib.checkma(dem)
if size > 5:
print("Need to implement iteration")
n = 0
out = dem
while n <= iterations:
dem_cv = cv2.medianBlur(out.astype(np.float32).filled(np.nan), size)
out = np.ma.fix_invalid(dem_cv)
out.set_fill_value(dem.fill_value)
n += 1
return out
def ProcessImage(self, image):
global autoMode
# Get the red section of the image
image = cv2.medianBlur(image, 5)
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) # Swaps the red and blue channels!
red = cv2.inRange(image, numpy.array((115, 127, 64)), numpy.array((125, 255, 255)))
# Find the contours
contours,hierarchy = cv2.findContours(red, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# Go through each contour
foundArea = -1
foundX = -1
foundY = -1
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
cx = x + (w / 2)
cy = y + (h / 2)
area = w * h
if foundArea < area:
foundArea = area
foundX = cx
foundY = cy
if foundArea > 0:
ball = [foundX, foundY, foundArea]
else:
ball = None
# Set drives or report ball status
if autoMode:
self.SetSpeedFromBall(ball)
else:
if ball:
print 'Ball at %d,%d (%d)' % (foundX, foundY, foundArea)
else:
print 'No ball'
# Set the motor speed from the ball position
def ProcessImage(self, image):
# Get the red section of the image
image = cv2.medianBlur(image, 5)
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) # Swaps the red and blue channels!
red = cv2.inRange(image, numpy.array((115, 127, 64)), numpy.array((125, 255, 255)))
# Find the contours
contours,hierarchy = cv2.findContours(red, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# Go through each contour
foundArea = -1
foundX = -1
foundY = -1
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
cx = x + (w / 2)
cy = y + (h / 2)
area = w * h
if foundArea < area:
foundArea = area
foundX = cx
foundY = cy
if foundArea > 0:
ball = [foundX, foundY, foundArea]
else:
ball = None
# Set drives or report ball status
self.SetSpeedFromBall(ball)
# Set the motor speed from the ball position
def process(self, img, median_filtering=True, blur_kn_size=3,
artif_suppression=True, low_int_threshold=.05, kernel_size=15,
pect_removal=False, high_int_threshold=.8, **pect_kwargs):
'''Perform multi-stage preprocessing on the input image
Args:
blur_kn_size ([int]): kernel size for median blurring.
low_int_threshold ([int]): cutoff used in artifacts suppression.
high_int_threshold ([int]): cutoff used in pectoral muscle removal.
Returns:
a tuple of (processed_image, color_image_with_boundary). If
pectoral removal was not called, the color image is None.
'''
img_proc = img.copy()
if median_filtering:
img_proc = cv2.medianBlur(img_proc, blur_kn_size)
if artif_suppression:
img_proc, mask_ = self.suppress_artifacts(
img_proc, global_threshold=low_int_threshold,
kernel_size=kernel_size)
else:
_, mask_ = self.suppress_artifacts(img_proc)
if pect_removal:
img_proc, img_col = self.remove_pectoral(
img_proc, mask_, high_int_threshold=high_int_threshold,
**pect_kwargs)
else:
img_col = None
return (img_proc, img_col)