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
saber_track.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录