def find_robot(im):
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
lower = np.array([50, 28, 0])
upper = np.array([60, 168, 255])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(im, im, mask=mask)
blur = cv2.blur(result, (5, 5))
bw = cv2.cvtColor(blur, cv2.COLOR_HSV2BGR)
bw2 = cv2.cvtColor(bw, cv2.COLOR_BGR2GRAY)
ret, th3 = cv2.threshold(bw2, 30, 255, cv2.THRESH_BINARY)
edges = cv2.Canny(th3, 100, 200)
th4 = copy.copy(th3)
perimeter = 0
j = 0
image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = np.array([])
for i in range(len(contours)):
if (perimeter < cv2.contourArea(contours[i])):
perimeter = cv2.contourArea(contours[i])
j = i;
cnt = contours[j]
x = 0
y = 0
for i in range(len(cnt)):
x = x + cnt[i][0][0]
y = y + cnt[i][0][1]
x = x / len(cnt)
y = y / len(cnt)
#print x, y
x = int(x)
y = int(y)
cv2.circle(im, (x, y), 5, (255, 0, 255), 2)
#show_image(im)
return (int(x), int(y))
python类blur()的实例源码
Artificial-potential-without-controller.py 文件源码
项目:Artificial-Potential-Field
作者: vampcoder
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
Artificial-potential-controller.py 文件源码
项目:Artificial-Potential-Field
作者: vampcoder
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def find_robot(frame):
im = copy.copy(frame)
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
lower = np.array([50, 28, 0])
upper = np.array([60, 168, 255])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(im, im, mask=mask)
blur = cv2.blur(result, (5, 5))
bw = cv2.cvtColor(blur, cv2.COLOR_HSV2BGR)
bw2 = cv2.cvtColor(bw, cv2.COLOR_BGR2GRAY)
ret, th3 = cv2.threshold(bw2, 30, 255, cv2.THRESH_BINARY)
edges = cv2.Canny(th3, 100, 200)
th4 = copy.copy(th3)
perimeter = 0
j = 0
image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = np.array([])
for i in range(len(contours)):
if (perimeter < cv2.contourArea(contours[i])):
perimeter = cv2.contourArea(contours[i])
j = i;
cnt = contours[j]
x = 0
y = 0
for i in range(len(cnt)):
x = x + cnt[i][0][0]
y = y + cnt[i][0][1]
x = x / len(cnt)
y = y / len(cnt)
#print x, y
x = int(x)
y = int(y)
cv2.circle(im, (x, y), 5, (255, 0, 255), 2)
cv2.imshow('img', im)
k = cv2.waitKey(0)
cv2.imwrite('robot.jpg', im)
#show_image(im)
return (int(x), int(y))
def xmedian(ref,mwid):
temp=np.isnan(ref)
tmean=np.nanmean(ref)
ref[temp]=tmean
ref2=cv2.blur(ref,(mwid,mwid))
ref[temp]=ref2[temp]
tempx=np.uint8(255*ref)
return cv2.medianBlur(tempx,mwid)/255.0
def ymedian0(aero,cls,mwid):
temp=np.isnan(aero)
tmean=np.nanmean(aero)
aero[temp]=tmean
aero2=cv2.blur(aero,(mwid,mwid))
aero[temp]=aero2[temp]
tempx=np.uint8(100*aero)
aerox=cv2.medianBlur(tempx,mwid)/100.0
return aerox
def ymedian(aero,cls,mwid,twid):
temp=np.isnan(aero)
tmean=np.nanmean(aero)
aero[temp]=tmean
aero2=cv2.blur(aero,(mwid,mwid))
aero[temp]=aero2[temp]
# 4/28/2016
#tempx=np.uint8(255*aero)
tempx=np.uint8(100*aero)
#aerox=cv2.medianBlur(tempx,mwid)/255.0
aerox=cv2.medianBlur(tempx,mwid)/100.0
ptemp=np.where(np.abs(aero-aerox) > twid)
cls[ptemp]=-1
return aerox
def binarize(self):
# ????????? ????? ??? = retval2, thres = cv2.threshold(data, 50,70,cv2.THRESH_BINARY) thres = cv2.blur(thres, (50, 50))
# ????????? ???????? =
a = cv2.adaptiveThreshold(self.data, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 125, 1)
# a = cv2.adaptiveThreshold(a, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 55, 1)
# retval2, a = cv2.threshold(self.data, 90, 255, cv2.THRESH_BINARY)
# a1 = np.median(a, 0)
# plt.hist(a1, 256, range=[0, 255], fc='k', ec='k')
# plt.show()
self.data = a
def blur(self):
px = 5
self.data = cv2.blur(self.data, (px, px))
# self.data = cv2.medianBlur(self.data, px)
def Bin(data):
# ????????? ????? ??? = retval2, thres = cv2.threshold(data, 50,70,cv2.THRESH_BINARY) thres = cv2.blur(thres, (50, 50))
# ????????? ???????? =
# retval2, thres = cv2.threshold(data,240,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# thres =cv2.adaptiveThreshold(data, 255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 0)
retval2, thres = cv2.threshold(data, 120,127,cv2.THRESH_BINARY)
# thres = cv2.blur(thres, (50, 50))
return thres
def smooth_data(data, iterations=4):
"""smooth image stack
:param data: NxMxF ndarray, where F is the number of frames and NxM are the image dimensions
:param iterations: number of times to run the smoothing
:return sv: smoothed data
"""
sv = data
for i in range(iterations):
sv[:, :, i] = cv2.blur(d.T, (3, 3))
return sv
def random_blur(image, size):
if np.random.random() < 0.5:
image = cv2.blur(image, size)
return image
def paintGL(self, sun_x, sun_y, sun_z, moon_x, moon_y, moon_z):
# Draw the sun
self.fbo.bind()
self.draw_sun(sun_x, sun_y, sun_z)
glFlush()
self.fbo.release()
image = self.fbo.toImage()
# Produce blurred image of sun
npimage = qimage_to_numpy(image)
h, w, b = npimage.shape
blur = cv2.GaussianBlur(npimage, (75, 75), 0, 0)
cv2.convertScaleAbs(blur, blur, 2, 1)
# Combine the blurred with the sun
combo = cv2.addWeighted(blur, 0.5, npimage, 0.5, -1)
h, w, b = combo.shape
qimage = QtGui.QImage(combo.data,w,h,QtGui.QImage.Format_ARGB32).rgbSwapped()
self.fbo.bind()
device = QtGui.QOpenGLPaintDevice(RES_X, RES_Y)
painter = QtGui.QPainter()
painter.begin(device)
rect = QtCore.QRect(0, 0, RES_X, RES_Y)
# Draw the blurred sun/sun combo image on the screen
painter.drawImage(rect, qimage, rect)
painter.end()
self.fbo.release()
# Draw the moon
self.fbo.bind()
self.draw_moon(moon_x, moon_y, moon_z)
glFlush()
self.fbo.release()
def smooth(image, method='gaussian', kernel=(5, 5)):
''' blur filter for noise removal. '''
if method == 'blur':
return cv.blur(image, kernel)
elif method =='gaussian':
return cv.GaussianBlur(image, kernel, 0)
else:
raise ValueError('Unknown smoothing method.')
def generate_blur(img,label_img,num,ratio_h,ratio_w):
rect_list = random_rect(num,img.shape,ratio_h,ratio_w)
ksize = 20
handle_img = np.copy(img)
rect = rect_list[0]
x,y,h,w = rect
roi = handle_img[y:y+h,x:x+w]
handle_img[y:y+h,x:x+w] = cv2.blur(roi,(ksize,ksize))
pixelpoints,_ = convex_hull_generate(img,rect,10)
label_img[pixelpoints] = 255
# print pixelpoints
img[pixelpoints] = handle_img[pixelpoints]
def blur(img,label_img):
generate_blur(img,label_img,1,(0.05,0.1),(0.05,0.1))
saber_track.py 文件源码
项目:Vision_Processing-2016
作者: Sabercat-Robotics-4146-FRC
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def get_bounding_rect( self, key, cap, win_cap, win, upper, lower, return_value=False, text=True ):
hsv = cv2.cvtColor( cap, cv2.COLOR_BGR2HSV )
hsv = cv2.blur(hsv,(5,5)) # blur the image for smoothing
msk = cv2.inRange( hsv, np.array(lower), np.array(upper) ) # get an object of all of the pixels with color values in the range
# Make images smooth again!
#msk = cv2.blur(msk,(5,5))
msk = cv2.erode(msk, None, iterations=3) # erode the image to reduce background noise
msk = cv2.dilate(msk, None, iterations=3) # dilate the image to reduce background noise
if self.settings["display"]: # if the display is true,
self.show( str(win)+ " Image", msk ) # show the binary range image
# Get the image contours in the mask
im2, contours, hierarchy = cv2.findContours( msk, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
# If a contour was found
if len(contours) > 0:
areas = [cv2.contourArea(c) for c in contours] # get the area of each contour
max_index = np.argmax(areas) # get the index of the largest contour by area
cnts = contours[max_index] # get the largest contout by area
cv2.drawContours(msk, [cnts], 0, (0,255,0), 3) # Draw the contours to the mask image
x,y,w,h = cv2.boundingRect(cnts) # get the bouding box information about the contour
cv2.rectangle(win_cap,(x,y),(x+w,y+h),(255,255,255),2) # Draw rectangle on the image to represent the bounding box
try:
self.smt_dash.putNumber('vis_x', x)
self.smt_dash.putNumber('vis_y', y)
self.smt_dash.putNumber('vis_w', w)
self.smt_dash.putNumber('vis_h', h)
except Exception:
pass
if text:
cv2.putText( win_cap , str(key), ( x, y+h ), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
if return_value: # if the function needs a return value
return [ x, y, w, h ] # return an array of the bounding box values
# Update function should be invoked whenever the camera frame needs refreshing
# Usage: self.update( )
# This should be embedded inside a while loop
def headblur(clip,fx,fy,r_zone,r_blur=None):
"""
Returns a filter that will blurr a moving part (a head ?) of
the frames. The position of the blur at time t is
defined by (fx(t), fy(t)), the radius of the blurring
by ``r_zone`` and the intensity of the blurring by ``r_blur``.
Requires OpenCV for the circling and the blurring.
Automatically deals with the case where part of the image goes
offscreen.
"""
if r_blur is None: r_blur = 2*r_zone/3
def fl(gf,t):
im = gf(t)
h,w,d = im.shape
x,y = int(fx(t)),int(fy(t))
x1,x2 = max(0,x-r_zone),min(x+r_zone,w)
y1,y2 = max(0,y-r_zone),min(y+r_zone,h)
region_size = y2-y1,x2-x1
mask = np.zeros(region_size).astype('uint8')
cv2.circle(mask, (r_zone,r_zone), r_zone, 255, -1,
lineType=cv2.CV_AA)
mask = np.dstack(3*[(1.0/255)*mask])
orig = im[y1:y2, x1:x2]
blurred = cv2.blur(orig,(r_blur, r_blur))
im[y1:y2, x1:x2] = mask*blurred + (1-mask)*orig
return im
return clip.fl(fl)
#------- OVERWRITE IF REQUIREMENTS NOT MET -----------------------------
def headblur(clip,fx,fy,r_zone,r_blur=None):
"""
Returns a filter that will blurr a moving part (a head ?) of
the frames. The position of the blur at time t is
defined by (fx(t), fy(t)), the radius of the blurring
by ``r_zone`` and the intensity of the blurring by ``r_blur``.
Requires OpenCV for the circling and the blurring.
Automatically deals with the case where part of the image goes
offscreen.
"""
if r_blur is None: r_blur = 2*r_zone/3
def fl(gf,t):
im = gf(t)
h,w,d = im.shape
x,y = int(fx(t)),int(fy(t))
x1,x2 = max(0,x-r_zone),min(x+r_zone,w)
y1,y2 = max(0,y-r_zone),min(y+r_zone,h)
region_size = y2-y1,x2-x1
mask = np.zeros(region_size).astype('uint8')
cv2.circle(mask, (r_zone,r_zone), r_zone, 255, -1,
lineType=cv2.CV_AA)
mask = np.dstack(3*[(1.0/255)*mask])
orig = im[y1:y2, x1:x2]
blurred = cv2.blur(orig,(r_blur, r_blur))
im[y1:y2, x1:x2] = mask*blurred + (1-mask)*orig
return im
return clip.fl(fl)
#------- OVERWRITE IF REQUIREMENTS NOT MET -----------------------------
def blur(self):
return cv2.blur(self.imageToProcess,9)
def toGray(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Simplified gaussian blur, not to be confused with cv2.blur()