python-3.xopencvimage-processingnumpy-ndarraycolor-detection

Reading BGR values of pixel in OpenCV


enter image description hereI want to detect color of shape in an image, so when I am run the code "bgr_list = img[cX,cY]" I get this error:

bgr_list = img[cX,cY]                 
IndexError: index 648 is out of bounds for axis 0 with size 598
        
        
but when I run "bgr_list = img[300,300]" I do not get any error  
->where [300,300] is white portion in image  
-> where, cX = x-coordinate of centroid and cY = y-coordinate of centroid  
-> BGR_list is bgr value of pixel at centroid  
-> the size of image is 898x598 pixel

Solution

  • Looks like you get X and Y cartesian coordinates from some sort of preprocessing but numpy matrices which opencv uses, use matrix indexing (row, column, channel). Your X coordinate corresponds with column index and Y coordinate is your row index.

    Fliping the indices should do the work bgr_list = img[cY,cX]