Ask Your Question
0

How to read center pixel color

asked 2016-02-05 11:36:45 -0600

Dr Dre gravatar image

updated 2016-02-06 08:24:27 -0600

Hi guys

This is my image

I want to read the centre pixel value of the following contour an output the color as Orange. But i tend to get an error as

if (center_px == ORANGE):
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The input image is

image description

import numpy as np
import cv2
img = cv2.imread('New Bitmap Image.bmp')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,contours,-1,(0,0,255),19)

for i in range(0,len(contours)):
    M = cv2.moments(contours[i])
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    print "Centroid = ", cx, ", ", cy

    center_px =  img[cx,cy]
    print center_px
    ORANGE = (0,127,255)

    if (center_px == ORANGE):
            print "Orange"

I am using pyhton and cv2

Update

As suggested by @berak I have modified the code as

import numpy as np
import cv2
img = cv2.imread('New Bitmap Image.bmp')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,190,255,cv2.THRESH_BINARY_INV)
cv2.imshow("windows1",thresh)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,contours,-1,(0,0,255),2)
cv2.imshow("windows",img)
print len(contours)
for i in range(0,len(contours)):
    M = cv2.moments(contours[i])
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    print "Centroid = ", cx, ", ", cy
    center_px = np.array([4,3,2],dtype=np.uint8)

    ORANGE = (0,127,255)


    center_px =  img[cx,cy]
    print center_px

    if ((center_px == ORANGE).all()):
        print "Orange"
    else:
        print "Not Orange"




cv2.waitKey(0)
cv2.destroyAllWindows()

The output is

1
Centroid =  146 ,  94
[255 255 255]
Not Orange

It shows not orange even though the image is orange color .What is it that i am doing wrong ????

edit retag flag offensive close merge delete

Comments

Could you please paste the original code? e.g. i is not defined so your code can't be tested.

FooBar gravatar imageFooBar ( 2016-02-05 13:51:59 -0600 )edit

@FooBar , the code has been updated .. Check it out

Dr Dre gravatar imageDr Dre ( 2016-02-05 19:56:45 -0600 )edit

@FooBar@berak Important point to be noted

the center_px value is showing as 255 255 255 rather than 0,127,255

Here is the error .. But i dont know why it is doing so .

Dr Dre gravatar imageDr Dre ( 2016-02-06 08:33:21 -0600 )edit
1

^^ welcome to row/col world, sir ;)

you have to index your numpy img like: img[y,x] not img[x,y]

(that's why you're measuring not the center, but somewhere at the lower (white) border)

berak gravatar imageberak ( 2016-02-06 08:41:25 -0600 )edit

Thank u that solves the problem

Dr Dre gravatar imageDr Dre ( 2016-02-06 08:44:01 -0600 )edit

plz update ur answer so that it might help anyone having similar problem

Dr Dre gravatar imageDr Dre ( 2016-02-06 08:44:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-02-06 00:24:18 -0600

berak gravatar image

updated 2016-02-06 00:28:29 -0600

this is more a numpy, than an opencv problem.

your pixel , center_px = img[cx,cy] is a numpy array, and ORANGE is a plain tuple, let's simulate it:

>>> pixel = np.array([4,3,2],dtype=np.uint8)
>>> ORANGE = (0,127,255)
>>> pixel == ORANGE
array([False, False, False], dtype=bool)

as you can see, you get a whole array of True, False values, one for each channel.

the solution, as stated in the error msg, is to check if the comparison is True for all channels, like:

>>> (pixel == ORANGE).all()
False

so, your if-statement should be:

if ((center_px == ORANGE).all()):
        print "Orange"
edit flag offensive delete link more

Comments

@berak Sir check out the updated question

Dr Dre gravatar imageDr Dre ( 2016-02-06 08:25:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-05 11:36:45 -0600

Seen: 2,615 times

Last updated: Feb 06 '16