Question & Answer: 1. My codes are below for the control using OpenCV to identify a particular color:…..

1. My codes are below for the control using OpenCV to identify a particular color:

2. The code in italics and close to the bottom with the asterisk is what I need help with. I want the color detected to control a pair of thrusters with the while and else statement. It detects the red color but does not do anything. If it does not detect a color it does nothing too.

#thresh = cv2.inRange(hsv,np.array((0, 200, 200)), np.array((20, 255, 255)))

lower = np.array([4,10,120],dtype=”uint8″)

upper = np.array([90,100,255], dtype=”uint8″)

thresh = cv2.inRange(blur, lower, upper)
thresh2 = thresh.copy()

# find contours in the threshold image
image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

# finding contour with maximum area and store it as best_cnt
max_area = 0
best_cnt = 1
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt

# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M[‘m10’]/M[‘m00’]), int(M[‘m01’]/M[‘m00’])
#if best_cnt>1:
cv2.circle(blur,(cx,cy),10,(100,100,255),-1)
cv2.putText(blur, “Red Detected”, (cx – 20, cy – 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# show the frame
cv2.imshow(“Frame”, blur)
#cv2.imshow(‘thresh’,thresh2)
key = cv2.waitKey(1) & 0xFF

# clear the stream in preparation for the next frame
rawCapture.truncate(0)

*while (thresh = cv2.inRange(blur, lower, upper)):
Thrusters_control.forward()
else:
Thrusters_control.turn_right()*

# if the `q` key was pressed, break from the loop
if key == ord(“q”):
break

 

Expert Answer

 

camera = PiCamera()

camera.resolution = (640, 480)

camera.framerate = 50

camera.hflip = True

rawCapture = PiRGBArray(camera, size=(640, 480))

time.sleep(0.1)

image = frame.array

blur = cv2.blur(image, (3,3))

lower = np.array([76,31,4],dtype=”uint8″)

upper = np.array([210,90,70], dtype=”uint8″)

thresh = cv2.inRange(blur, lower, upper)

thresh2 = thresh.copy()

image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

max_area = 0

best_cnt = 1

for cnt in contours:

area = cv2.contourArea(cnt)

if area > max_area:

max_area = area

best_cnt = cnt

M = cv2.moments(best_cnt)

cx,cy = int(M[‘m10’]/M[‘m00’]), int(M[‘m01’]/M[‘m00’])

cv2.circle(blur,(cx,cy),10,(0,0,255),-1)

cv2.imshow(“Frame”, blur)

key = cv2.waitKey(1) & 0xFF

rawCapture.truncate(0)

if key == ord(“q”):

break

Still stressed from student homework?
Get quality assistance from academic writers!