I'm just going to gloss over how to do this without writing up a full program. The short answer is this is VERY possible and relatively simple.
Step 1: Get an image from the camera
For this I would recommend just using VideoCapture
and processing each image that comes in. This can be done like so:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Read the current frame
Step 2: Detecting the color (I'll give an example of red)
For this you will need to define some boundaries in the RGB colorspace. For this you really just need to determine some lower and upper boundaries
boundaries = [
([17, 15, 100], [50, 56, 200]),
([25, 146, 190], [62, 174, 250])
]
This is the lower and upper bounds of two boundaries. For instance ([17, 15, 100], [50, 56, 200])
is saying we are looking for R>=17
, B>=15
, G>=100
and R<=50
, B<=56
, G<=200
. And yes, the format is RBG for this because of the default scheme in OpenCV
Step 3: Put it together
while True:
ret, frame = cap.read() # Read the current frame
for (lo, up) in boundaries:
lo = np.array(lo, dtype='uint8')
up = np.array(up, dtype='uint8')
# Find the colors within those boundaries in the image
mask = cv2.inRange(frame, lo, up)
out = cv2.bitwise_and(frame, frame, mask=mask)
out
now contains the current frame but only the red
that is in the image within those boundaries. You can then determine if there is enough red in the image to follow, or turn or what. If you want to actually determine if it's a line of some sort, you can also use findContours
to determine the shape of the object that is in red, and then figure out if it's a line that way.