您的位置:首页 > 其它

关于Mat型数据的三通道读取

2017-03-23 23:11 295 查看
与之前的采用图像指针进行读取的图像的三原色通道相比,本文采用两个函数进行,一个是分通道函数,一个是通道组合,但是由于本人刚入门,只能读取三个通道后通过灰度显示,彩色显示时,其他两个通道填充0时出现问题,目前还在思考中,如果有盆友能够解答自是万分感谢!疑问:如何将蓝色、绿色以及红色通道分别赋予三个图像的对应通道,并且将其他两个通道置零?

#include<opencv2/opencv.hpp>  

#include<iostream>  

#include<cassert>  

#include<vector>  

using namespace cv;

using namespace std;

int main()

{
Mat srcImage = imread("D:/Visual Studio 2015/lena.bmp");
Mat imageBlue, imageGreen, imageRed;
Mat mergeImage;
//Mat r[512][512] , g[512][512], b[512][512];
//r = imageBlue(Rect(0, 0, imageBlue.cols, imageBlue.rows));
//g = imageBlue(Rect(0, 0, imageBlue.cols, imageBlue.rows));
//b = imageBlue(Rect(0, 0, imageBlue.cols, imageBlue.rows));

//定义一个Mat向量容器保存拆分后的数据  
vector<Mat> channelrgb;
//vector<Mat> channelr;
//vector<Mat> channelg;
//vector<Mat> channelb;

//判断文件加载是否正确  
assert(srcImage.data != NULL);
namedWindow("image_red", CV_WINDOW_AUTOSIZE);
namedWindow("image_green", CV_WINDOW_AUTOSIZE);
namedWindow("image_blue", CV_WINDOW_AUTOSIZE);
namedWindow("mergeImage", CV_WINDOW_AUTOSIZE);

//通道的拆分  
split(srcImage, channelrgb);

//提取蓝色通道的数据  
imageBlue = channelrgb.at(0);
//cout << imageBlue.cols << endl<< imageBlue.rows << endl;
//channelb.push_back(imageBlue);
//channelb.push_back(g);
//channelb.push_back(r);
//channelb.at(1) = g;
//channelb.at(2) = r;

//提取绿色通道的数据  
imageGreen = channelrgb.at(1);
//channelg.at(0) = 0;
//channelg.at(1) = imageGreen;
//channelg.at(2) = 0;

//提取红色通道的数据  
imageRed = channelrgb.at(2);
//channelr.at(0) = 0;
//channelr.at(1) = 0;
//channelr.at(2) = imageRed;

//对拆分的通道数据合并  

//channelrgb.push_back(imageBlue);
//channelrgb.push_back(imageGreen);
//channelrgb.push_back(imageRed);
merge(channelrgb, mergeImage);
//merge(channelb, imageBlue);
//merge(channelg, imageGreen);
//merge(channelr, imageRed);
imshow("image_blue", imageBlue);
imshow("image_green", imageGreen);
imshow("image_red", imageRed);
imshow("mergeImage", mergeImage);
waitKey();
system("pause");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: