1

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() 
2
  • Display mask on 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. Commented Apr 7, 2014 at 5:31
  • thank you sir but which fuction or command shall i use to iterating pixels to get x and y coordinate if there any keyword for this Commented Apr 7, 2014 at 6:45

1 Answer 1

1

You can get the color values using:

values = img[100,100]

You have to Check if Mask is 255 (Pixel in Range) and get the color from your image (hsv).

To iterate over the image you can use:

img.shape

which returns rows, columns, and channels.

In total you get something like:

row, col, chan = img.shape
for r in row:
    for c in col:
        if mask[r,c] == 255:
            print img[r,c]

You can find more information in the OpenCV Python Tutorial.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.