[Super straightforward explanation of opencv RGB and BGR] What is the difference between RGB mode and BGR mode, and how to convert each other?

1. Why are there two channels, BGR and RGB?

First of all, there is no difference between RGB and BGR in essence. The formats of OpenCV and PIL to read pictures are BGR and RGB respectively.

When we use OpenCV to read images, you should also find that the read array is actually in BGR format, not the RGB format that we hear and use the most . So why use BGR channels instead of RGB channels to describe images?

The default channel of OpenCV is BGR, which may be based on some hardware level. Because caffe, as the representative of the earliest and most popular batch of libraries, uses opencv, and the default channel of opencv is bgr. This is one of the entry pits of opencv. bgr is a historical problem. In order to be compatible with some hardware in the early years, opencv is not easy to change back. In fact, you can use rgb for training yourself, and the new library basically does not have the problem of bgr or rgb, just switch the order. But if you want to use some old trained models, you have to be compatible with the bgr of the old model.

[More detailed explanation on the official website] RGB is byte order. But an intentional implementation choice of most vanilla graphics libraries is that they internally treat colors as unsigned 32-bit integers with three (or four, often including alpha) components packed into the integer. On a little-endian machine (e.g. x86), the integer 0x01020304 will actually be stored in memory as 0x04030201. So 0x00BBGGRR will be stored as 0xRRGGBB00! So the term BGR (and BGRA etc.) is a leaky abstraction where the graphics library interprets how integers are logically ordered in order to make code that directly accesses color components more readable. Keep in mind that bitmaps are usually accessed by more parts of the hardware than the processor, and the endianness specified by legacy display adapters is not necessarily the same as that of the CPU. At the level of manipulating the channels in a pixel, the CPU can extract fields regardless of their order; it's purely a matter of programmers understanding labels.

2. Simple example verification

Let's read and display the following images with OpenCV and PIL respectively, and compare the results:

cv2

import cv2
img_path = 'dataset1/train/ants_img/0013035.jpg'
img = cv2.imread(img_path)
print(img)

PIL

from PIL import Image
img_path = 'dataset1/train/ants_img/0013035.jpg'
img = Image.open(img_path)
img_arr = np.array(img)
print(img_arr)

The result is as follows:

                                          

To put it simply, we analyze from the first [] , we can see that cv2 [233 151 80] and PIL [80 151 233] correspond to GBR and RGB respectively.

Since the color channels of the two are different, the results displayed in the same picture should also be different, as shown in the figure below

               

3. How to convert RGB to BGR

When [:,:,:::-1] is executed, the array will be turned left and right, so as to achieve the effect of mutual conversion, so we experiment with the image read by cv2, the specific code is as follows:

img_path = 'dataset1/train/ants_img/0013035.jpg'
img = cv2.imread(img_path)
img = img[:, :, ::-1]#BGR转为RGB
print(img)
plt.imshow(img)
plt.show()

 The result is as follows:

 

 In the end, we found that we realized the conversion of BGR to RGB through img[:, :, ::-1].

 4. Memo Summary

The following is a better summary of referring to others, the original link is below

1.
The image opened by img = Image.open(ImgPath) is of PIL type, and the default is RGB.
Convert the PIL type to the numpy type: im = numpy.array(img)
to see the shape attribute, which is an array of (height, width, channel), and the channel data of the channel is RGB.


2. cv2.imread(path, reading method):
path: the path of the picture;
reading method: cv2.IMREAD_COLOR: read a color picture; cv2.IMREAD_GRAYSCALE: read the picture in grayscale mode; cv2.IMREAD_UNCHANGED: Loads an image, including its alpha channel.
Defaults to cv2.IMREAD_COLOR.
The return value is an array of (height, width, channel), and the order of channels is BGR order


3. Mutual conversion between the two
PIL Image into OpenCV format:
img = Image.open()
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
OpenCV into PIL Image format:
img = cv2 .imread()
img2 = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.imshow() and cv2.imshow()


4. This display is opposite to the reading, that is to say, if you want to visualize the normal RGB format, the image for plt.imshow() should be in rgb format, and the image for cv2.imshow() should be in bgr format picture. Say more about plt.imshow(), because I hardly use cv2.imshow() at ordinary times. In plt.imshow(), the accepted image type can be any type of np.ndarray, tensor, PIL Image.
 

 


  Reference URL:

​​​​​​[PyTorch] Image analysis: RGB and BGR conversion - know almost

[BGR and RGB in openCV] img[:,:,::-1] and img[:, :, (2, 1, 0)] operate on images

Guess you like

Origin blog.csdn.net/weixin_52527544/article/details/128008221