C++ OpenCV:拍摄3通道RGB图像,分割通道并仅使用R+查看图像;G
我只想查看RGB图像中的R+G通道,因为当蓝色通道被移除时,我可以获得更好的对比度来检测对象。我使用OpenCV分割通道,但在将蓝色通道设置为0后合并通道时,我的代码无法编译C++ OpenCV:拍摄3通道RGB图像,分割通道并仅使用R+查看图像;G,c++,opencv,C++,Opencv,我只想查看RGB图像中的R+G通道,因为当蓝色通道被移除时,我可以获得更好的对比度来检测对象。我使用OpenCV分割通道,但在将蓝色通道设置为0后合并通道时,我的代码无法编译 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main(
#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;
}
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
如果(argc!=2)
{
难道你需要更改这些行吗
channel[0]=Mat::zeros(Size(image.rows, image.cols), CV_8UC1);//Set blue channel to 0
//Merging red and green channels
merge(channel,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;
}
#包括
#包括“opencv2/opencv.hpp”
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
如果(argc!=2)
{
cout好的,我开始使用mixChannels():我在上面的代码片段中附加了一个附加内容:
Mat gr( image.rows, image.cols, CV_8UC3);
// forming an array of matrices is a quite efficient operation,
// because the matrix data is not copied, only the headers
Mat out[] = {gr};
// bgr[1] -> gr[1],
// bgr[2] -> gr[2],
int from_to[] = {1,1, 2,2 };
mixChannels( &image, 1, out, 2, from_to, 2 );
imshow("R+G",gr);
谢谢
哈沙另一种方法是从源图像中减去标量(255,0,0)
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv)
{
Mat src = imread(argv[1], CV_LOAD_IMAGE_COLOR);
imshow("src", src );
src -= Scalar(255,0,0);
imshow("Green and Red channels", src );
waitKey();
return 0;
}
#包括
使用名称空间cv;
int main(int argc,字符**argv)
{
Mat src=imread(argv[1],CV\u LOAD\u IMAGE\u COLOR);
imshow(“src”,src);
src-=标量(255,0,0);
imshow(“绿色和红色通道”,src);
waitKey();
返回0;
}
最有效的方法是不进行任何拆分和合并。这既节省了时间又节省了内存
只需按位将图像与cv::Scalar(0255255)
对齐,即可将蓝色通道设置为零
正如:imshow(“R+G”,src&cv::Scalar(0255255));
感谢Haris的反馈,我尝试了这一点,但在运行时出现了分段错误。因此我决定尝试使用混合通道的另一种方法,这很有效。我可以共享代码:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv)
{
Mat src = imread(argv[1], CV_LOAD_IMAGE_COLOR);
imshow("src", src );
src -= Scalar(255,0,0);
imshow("Green and Red channels", src );
waitKey();
return 0;
}