EasyManua.ls Logo

SunFounder picar-x - Page 69

Default Icon
153 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder picar-x
(continued from previous page)
hsv = cv2.cvtColor(resize_img, cv2.COLOR_BGR2HSV) # Convert from BGR
˓to HSV
color_type = color_name
mask = cv2.inRange(hsv,np.array([min(color_dict[color_type]), 60, 60]), np.
˓array([max(color_dict[color_type]), 255, 255]) ) # inRange()Make the ones
˓between lower/upper white, and the rest black
if color_type == 'red':
mask_2 = cv2.inRange(hsv, (color_dict['red_2'][0],0,0), (color_dict['red_2
˓'][1],255,255))
mask = cv2.bitwise_or(mask, mask_2)
morphologyEx_img = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_5,iterations=1)
˓ # Perform an open operation on the image
# Find the contour in morphologyEx_img, and the contours are arranged according
˓to the area from small to large.
_tuple = cv2.findContours(morphologyEx_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_
˓SIMPLE)
# compatible with opencv3.x and openc4.x
if len(_tuple) == 3:
_, contours, hierarchy = _tuple
else:
contours, hierarchy = _tuple
color_area_num = len(contours) # Count the number of contours
if color_area_num > 0:
for i in contours: # Traverse all contours
x,y,w,h = cv2.boundingRect(i) # Decompose the contour into the
˓coordinates of the upper left corner and the width and height of the recognition
˓object
# Draw a rectangle on the image (picture, upper left corner coordinate,
˓lower right corner coordinate, color, line width)
if w >= 8 and h >= 8: # Because the picture is reduced to a quarter of
˓the original size, if you want to draw a rectangle on the original picture to
˓circle the target, you have to multiply x, y, w, h by 4.
x = x
*
4
y = y
*
4
w = w
*
4
h = h
*
4
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) # Draw a rectangular
˓frame
cv2.putText(img,color_type,(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,
˓255),2)# Add character description
return img,mask,morphologyEx_img
with PiCamera() as camera:
print("start color detect")
camera.resolution = (640,480)
camera.framerate = 24
rawCapture = PiRGBArray(camera, size=camera.resolution)
time.sleep(2)
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_
˓port=True):# use_video_port=True
(continues on next page)
4.8. Color Detection 65

Related product manuals