Get value from detect colour python opencv
Question: 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() 
    


Answer: 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](http://docs.opencv.org/trunk/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html).

