OpenCV 如果是单通道图片,根据对应类型实现单通道转3通道



cv::Mat convertTo3Channels(const cv::Mat& image_data)
{
	if (image_data.channels() == 1) //单通道
	{
		cv::Mat image = image_data.clone();
		int type = image.type();
		int rows = image.rows;
		int cols = image.cols;
		int three_type = CV_8UC3; //默认是8U
		//如果不是8U的转成对应的三通道数据类型
		if (type == CV_8SC1) { three_type = CV_8SC3; }
		if (type == CV_16UC1) { three_type = CV_16UC3; }
		if (type == CV_16SC1) { three_type = CV_16SC3; }
		if (type == CV_32SC1) { three_type = CV_32SC3; }
		if (type == CV_32FC1) { three_type = CV_32FC3; }
		if (type == CV_64FC1) { three_type = CV_64FC3; }
		if (type == CV_16FC1) { three_type = CV_16FC3; }
		cv::Mat three_channel = cv::Mat::zeros(rows, cols, three_type);//转三通道
		vector<cv::Mat> channels;
		for (int i = 0; i < 3; i++)
		{
			channels.push_back(image);
		}
		merge(channels, three_channel);
		return three_channel;
	}
	else
	{
		return image_data;
	}
}

参考:https://blog.csdn.net/my_angle2016/article/details/128483733

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值