OpenCV 获取图像的通道数,实现单通道转3通道

本文介绍了一种将单通道图像转换为三通道图像的方法,并提供了详细的C++代码实现。该方法通过复制单通道图像三次并合并来创建三通道图像。此外,还展示了如何读取图像、检查图像通道数并显示图像。
#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值