Ask Your Question
0

numpy.ndarray returning incorrect pixel value vs pixelaccess type

asked 2013-09-06 13:40:26 -0500

smhall05 gravatar image

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]
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-06 14:51:49 -0500

berak gravatar image

updated 2013-09-06 14:53:58 -0500

  1. numpy & opencv use pixel[y,x]

  2. PIL is using pixel[x,y]

so in fact, youre looking at different pixels

edit flag offensive delete link more

Comments

Thank you, I actually started looking at that a few minutes ago and noticed when I swapped my x,y to y,x I got what I expected with numpy.ndarray. Now to make sense of this in my full program and make the swap!

smhall05 gravatar imagesmhall05 ( 2013-09-06 15:19:11 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-06 13:40:26 -0500

Seen: 274 times

Last updated: Sep 06 '13