Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

numpy.ndarray returning incorrect pixel value vs pixelaccess type

I am confused as to why opencv is returning the incorrect BGR value when given a pixel location, x,y.

BGR color space:

When I request x = 12, y = 5 I get back 147, 147, 147 when the image type is of numpy.ndarray

When I request x = 12, y = 5 I get back 238, 235, 235 of type PixelAccess.

I expect BGR(238,235,235) or RGB(235,235,238) to be the correct answer. Below I have a code snippet that returns the incorrect then the correct pixel values after converting numpy to pixelaccess. The image under test is also attached. Thank you for any help.

C:\fakepath\incorrect_color_values_returned.png

Questions:

1) What subtlety am I missing/overlooking here w.r.t numpy.ndarrays?

2) Is it possible to get numpy.ndarray to return the correct value without having to convert to PixelAccess?

3) What is the correct way to access numpy.ndarray x,y values?

Here is my code (I use videocapture intentionally to handle images from a url)

from PIL import Image
import cv2
cap = cv2.VideoCapture("incorrect_color_values_returned.png")
ret, im = cap.read()

print type(im)
print im[12,5]
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(im)
pix = pil_im.load()
print type(pix)
print pix[12,5]