11

I'm getting behavior I don't quite understand:

In [1]: import cv2

In [2]: pylab_img=pylab.imread('lena.jpg')

In [3]: cv_img=cv2.imread('lena.jpg')

In [4]: pylab_img[200,200,:]
Out[4]: array([228, 197, 176], dtype=uint8)

In [5]: cv_img[200,200,:]
Out[5]: array([ 84,  48, 132], dtype=uint8)

Both versions of imread read the same image into a numpy array of the same datatype, yet the values don't match. If the values were just mixed up I could chalk it up to the fact that opencv uses BGR whereas matplotlib (pylab) uses RGB, but that doesn't seem to account for this discrepancy.

Any thoughts?

2
  • 2
    It might be informative to take the result from cv2's imread and view it with pylab's imshow, then take the result from pylab's imread and view it with cv2's imshow. Commented Jul 19, 2013 at 3:52
  • SOmewhat related: stackoverflow.com/questions/1349230/… Commented Jul 19, 2013 at 3:59

1 Answer 1

13

They don't match for a couple reasons:

  1. matplotlib reads the color values as RGB whereas OpenCV uses BGR
  2. The rows in matplotlib's array appear to list pixel rows from the bottom of the image to the top (don't ask me why), whereas OpenCV goes from top to bottom

Perhaps there is a nicer way of doing this, but if you wanted to match them, you would find:

pylab_img[::-1,:,::-1] == cv_img
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.