For my project, I want to control an RC car to avoid red obstacles using a webcam camera running Raspberry Pi.
I can control my RC car, but I don't know much about openCV. I can also detect selected colours using the webcam, but I do not know how to get the colour value from the image to my RC car.
The issue that I am having is that my car does not know that a blue object is in front of the camera.
Here is my code:
import cv2
import numpy as np
cap = cv2.videoCapture(0)
while(1):
# take each frame
_,frame=cap.read()
#convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#define range of blue color in HSV
lower_blue = np.array([160,50,50],dtype=np.uint8)
uper_blue np.array([179,255,255],dtype=np.uint8)
#Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv,lower_blue,upper_blue)
cv2.imshow('mask',mask)
k=cv2.waitKey(5) &0xFF
if k == 27 :
break
cv2.destroyAllwindows()
maskon a window and you will see that objects in it are those defined between the range you specified. After that, iterating on the pixels of this image will give inform the specific (x,y) coordinates of those objects, so you can turn your robot to the other way.