It turns out that the storage order of RGB image data in OpenCV is BGR

It turns out that the storage order of RGB image data in OpenCV is BGR.

Examples that verify this fact are as follows:

We read in three pure color images, namely pure red, pure green, and pure blue.

Baidu network disk download link for the above three pictures:
https://pan.baidu.com/s/1b0XfTQcGxiXePu1_ZEsUig?pwd=m6ut  

Then use the following code to read in the following three pictures and observe the data storage structure of variables:

//出处:昊虹AI笔记网(hhai.cc)
//用心记录计算机视觉和AI技术

//博主微信/QQ 2487872782
//QQ群 271891601
//欢迎技术交流与咨询

//OpenCV版本 OpenCV3.0

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
{

	cv::Mat Red_pic = cv::imread("F:/material/images/2022/2022-10/red.bmp"); //读入纯红色图像
	cv::Mat Green_pic = cv::imread("F:/material/images/2022/2022-10/green.bmp"); //读入纯绿色图像
	cv::Mat Blue_pic = cv::imread("F:/material/images/2022/2022-10/blue.bmp"); //读入纯蓝色图像


	int red_0, red_1, red_2;//分别存储纯红色图像0通道、1通道、2通道的数值
	int green_0, green_1, green_2;//分别存储纯绿色图像0通道、1通道、2通道的数值
	int blue_0, blue_1, blue_2;//分别存储纯绿色图像0通道、1通道、2通道的数值


	red_0 = Red_pic.at<cv::Vec3b>(50, 50)[0];
	red_1 = Red_pic.at<cv::Vec3b>(50, 50)[1];
	red_2 = Red_pic.at<cv::Vec3b>(50, 50)[2];

	green_0 = Green_pic.at<cv::Vec3b>(50, 50)[0];
	green_1 = Green_pic.at<cv::Vec3b>(50, 50)[1];
	green_2 = Green_pic.at<cv::Vec3b>(50, 50)[2];

	blue_0 = Blue_pic.at<cv::Vec3b>(50, 50)[0];
	blue_1 = Blue_pic.at<cv::Vec3b>(50, 50)[1];
	blue_2 = Blue_pic.at<cv::Vec3b>(50, 50)[2];


	return 0;
}

Start debugging, the results are as follows:

 From the results it can be seen that:

For a pure red image, the value of the second channel is 255, indicating that the second channel is the R channel;

For a pure green image, the value of the first channel is 255, indicating that the first channel is the G channel;

For a pure blue image, the value of the 0th channel is 255, indicating that the 0th channel is the B channel.

In summary, the storage order of OpenCV for RGB image data is BGR.

In addition, it can be seen from the article  https://www.hhai.cc/thread-89-1-1.html  that in Python-OpenCV, the storage order of OpenCV's RGB image data is also BGR.

In addition, everyone should pay attention that the parameter order of Scalar() in OpenCV is also B, G, R, so cv::Scalar(0, 0, 255) represents red.

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/51554530#comments_27137012