Imread in pylab vs opencv: returns completely different array values

I get behavior that 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 data type, but the values ​​do not match. If the values ​​were just mixed, I could add this to the fact that opencv uses BGR, while matplotlib (pylab) uses RGB, but that does not seem to explain this mismatch.

Any thoughts?

+6
source share
1 answer

They do not match for several reasons:

  • matplotlib reads color values ​​as RGB, whereas OpenCV uses BGR
  • Lines in the matplotlib array display a list of lines of pixels from the bottom of the image at the top (don't ask me why), while OpenCV goes from top to bottom

There may be a nicer way to do this, but if you want to match them, you will find:

 pylab_img[::-1,:,::-1] == cv_img 
+9
source

Source: https://habr.com/ru/post/949793/


All Articles