#include <opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
/*************************************************
// Method: convertTo3Channels
// Description: 将单通道图像转为三通道图像
// Returns: cv::Mat
// Parameter: binImg 单通道图像对象
*************************************************/
Mat convertTo3Channels(const Mat &binImg)
{
Mat three_channel = Mat::zeros(binImg.rows,binImg.cols,CV_8UC3);
vector<Mat> channels;
for(int i = 0;i < 3; i ++)
{
channels.push_back(binImg);
}
merge(channels,three_channel);
return three_channel;
}
int main()
{
Mat src;
src = imread("D:/lena.png");
if (src.empty()) {
printf("could not find the picture!");
return-1;
}
//方法1
int height = src.rows;//row表示行,rows表示行的总数,即图像的高
int width = src.cols;//col表示列,cols表示列的总数,即图像的宽
//方法2
cout<<src.size()<<endl;
//获取通道数
int channels = src.channels();
//打印输出
printf("height=%d width=%d channels=%d", height, width, channels);
if(channels != 3)
{
Mat three_channels = convertTo3Channels(src);
imshow("dst",three_channels);
}else
imshow("dst",src);
waitKey(0);
return 0;
}
OpenCV 获取图像的通道数,实现单通道转3通道
最新推荐文章于 2025-05-30 23:05:09 发布