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