题目源:GitHub - gzr2017/ImageProcessing100Wen: 「画像処理100本ノック」中文版本!为图像处理初学者设计的 100 个问题。
题目描述:
提供一种Opencv4的方法,使用cvtColor函数
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("C:/Users/79490/Desktop/ImageProcessing100Wen-master/ImageProcessing100Wen-master/Question_01_10/imori.jpg",
IMREAD_COLOR);
Mat img_RGB;
cvtColor(img,img_RGB,COLOR_BGR2RGB);
imshow("rgb",img_RGB);
waitKey(0);
return 0;
}
下面是官方的参考答案,
注意,新建的img_rgb必须赋值为有内容的Mat
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat swap_channel(Mat img){
int rows(img.rows),cols(img.cols);
Mat img_rgb = Mat::zeros(rows,cols,CV_8UC3);
for(int i(0);i<rows;i++){
for(int j(0);j<cols;j++){
img_rgb.at<Vec3b>(i,j)[0]=img.at<Vec3b>(i,j)[2];
img_rgb.at<Vec3b>(i,j)[1]=img.at<Vec3b>(i,j)[1];
img_rgb.at<Vec3b>(i,j)[2]=img.at<Vec3b>(i,j)[0];
}
}
return img_rgb;
}
int main()
{
Mat img=imread("C:/Users/79490/Desktop/ImageProcessing100Wen-master/ImageProcessing100Wen-master/Question_01_10/imori.jpg",IMREAD_COLOR);
Mat img_rgb=swap_channel(img);
imshow("img_rgb",img_rgb);
waitKey(0);
return 0;
}