Dr  Dre
Dr Dre

Reputation: 3

How to read center pixel color

The input image link is : Input Image

enter image description here

I want to read the center pixel value of the input image as shown above and detect color If output color is Orange then print 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 code is as shown

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

Upvotes: 0

Views: 830

Answers (1)

Zack Graber
Zack Graber

Reputation: 1136

To test if arrays are equal, you can use numpy.array_equal

center_px = img[cy, cx]
if np.array_equal(center_px, (0, 127, 255)):
    print("Orange")

Upvotes: 1

Related Questions