我只想查看 RGB 图像中的 R+G channel ,因为当蓝色 channel 被移除时,我可以获得更好的对比度来检测物体。我使用 OpenCV 拆分 channel ,但是在将蓝色 channel 设置为 0 后合并相同的 channel 时,我的代码无法编译。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image,fin_img;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
// Show our image inside it.
// Create Windows
namedWindow("Red",1);
namedWindow("Green",1);
namedWindow("Blue",1);
// Create Matrices (make sure there is an image in input!)
Mat channel[3];
imshow( "Original Image", image );
// The actual splitting.
split(image, channel);
channel[0]=Mat::zeros(Size(image.rows, image.cols), CV_8UC1);//Set blue channel to 0
//Merging red and green channels
merge(channel,image);
imshow("R+G", image);
waitKey(0);//Wait for a keystroke in the window
return 0;
}
我可以对我哪里出错有任何反馈吗?我怀疑是将蓝色 channel 设置为 0。有没有更好的方法将其设置为 0?有没有办法使用 cvMixChannels() 来做到这一点?
最佳答案
你需要改变这些行
channel[0]=Mat::zeros(Size(image.rows, image.cols), CV_8UC1);//Set blue channel to 0
//Merging red and green channels
merge(channel,image);
到
channel[0]=Mat::zeros(image.rows, image.cols, CV_8UC1);//Set blue channel to 0
//Merging red and green channels
merge(channel,3,image);
编辑
根据您的评论,这里是完整的代码和结果。
#include <iostream>
#include "opencv2/opencv.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image,fin_img;
image = imread("bgr.png", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
// Show our image inside it.
// Create Windows
namedWindow("Red",1);
namedWindow("Green",1);
namedWindow("Blue",1);
// Create Matrices (make sure there is an image in input!)
Mat channel[3];
imshow( "Original Image", image );
// The actual splitting.
split(image, channel);
channel[0]=Mat::zeros(image.rows, image.cols, CV_8UC1);//Set blue channel to 0
//Merging red and green channels
merge(channel,3,image);
imshow("R+G", image);
imwrite("dest.jpg",image);
waitKey(0);//Wait for a keystroke in the window
return 0;
}
源图片
没有蓝色分量的结果
关于c++ - OpenCV:获取 3 channel RGB 图像,拆分 channel 并仅使用 R+G 查看图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341910/