Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python | cv2.imshow() loading arrays as BGR?

I have recorded some data as npy file. And I tried to diplay the image (data[0]) to check if it makes sense with the following code

import numpy as np
import cv2

train_data = np.load('c:/data/train_data.npy')

for data in train_data:
    output = data[1]
    # only take the height, width and channels of the 4 dimensional array
    image = data[0][0, :, :, :]
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    cv2.imshow('test', image)
    print('output {}'.format(output))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

But if I display the images without the line image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) the images seem to be BGR based. If I comment this line into the code the images are displayed correctly.

My question: Does this observation imply that the image array is already in BGR format? Or does this imply that cv2.imshow() does by default interprete the array as BGR array?

like image 671
MrYouMath Avatar asked Jul 16 '26 02:07

MrYouMath


1 Answers

Matplotlib and Numpy read images into RGB and processes them as RGB. OpenCV reads images into BGR and processes them as BGR. Either system recognizes a range of input types, has ways to convert between color spaces of almost any type, and offers support of a variety of image processing tasks.

This gives three different ways to load an image (plt.imread(), ndimage.imread() and cv2.imread()), two systems for processing the data (Numpy and CV2), and two ways to display the image (plt.imshow() and cv2.imshow()), and really, there is a third way to display the image using pyplot, if you want to treat the image as numerical data in 2-d plus another dimension for each color.

Here is some simple code to demonstrate some of this.

#!/usr/bin/python

import matplotlib.pyplot as plt
from scipy.ndimage import imread
import numpy as np
import cv2

img = imread('index.jpg')
print( "img data type: %s shape %s"%( type(img), str( img.shape) ) )

plt.imshow( img )
plt.title( 'pyplot as read' )
plt.savefig( 'index.plt.raw.jpg' )

cv2.imshow('cv2, read by numpy', img)
cv2.imwrite('index.cv2.raw.jpg',img)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

cv2.imshow('after conversion', img)
cv2.imwrite('index.cv2.bgr2rgb.jpg',img)

This generates the following line of text, and the following three example image files.

img data type: <type 'numpy.ndarray'> shape (225, 225, 3)

The correct image has red as the upper circle. We read the image into a numpy array, using ndimage.imread(), and show it with Pyplot's imshow() and get the correct image. We then show it with cv2.imshow() and we see that the red channel is interpreted as the blue channel and vice versa. Then we convert the colorspace and we see that cv2.imshow() now interprets the result correctly.

plt.imshow(), as read by ndimage():

plt.imshow(), as read by ndimage()

cv2.imshow(), the image as read by ndimage:

cv2.imshow(), the image as read by ndimage

cv2.imshow(), after converting from RGB to BGR:

cv2.imshow(), after converting from RGB to BGR

like image 52
DrM Avatar answered Jul 18 '26 16:07

DrM