Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to read center pixel color

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('Image1.jpg')
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)

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"

How to read center pixel color

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('Image1.jpg')
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)

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

How to read center pixel color

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('Image1.jpg')
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

How to read center pixel color

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 ????