将单通道图像转换为 3 通道图像 C++/OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3614163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:17:17  来源:igfitidea点击:

Convert single channle image to 3 channel image C++ / OpenCV

c++opencv

提问by Harry

Im using openCV C++

我使用 openCV C++

Im need to convert a single channel image to 3 channels image. So i can use this: cvCvtColor(result,gray,CV_BGR2GRAY);

我需要将单通道图像转换为 3 通道图像。所以我可以使用这个:cvCvtColor(result,gray,CV_BGR2GRAY);

I cannot do that because the result img is a single channel image. Any ideas?

我不能这样做,因为结果 img 是单通道图像。有任何想法吗?

回答by Vadim Shender

You should use CV_GRAY2BGRinstead of CV_BGR2GRAY.

你应该使用CV_GRAY2BGR而不是CV_BGR2GRAY.

回答by Harry

Try this:

尝试这个:

CvSize dim = cvSize(int width, int height);
IplImage* dst = cvCreateImage( dim, 8, 3 );
IplImage* gray = cvCreateImage(dim, 8, 1);
// Load the gray scale image
cvMerge(gray , NULL, NULL, NULL, dst);
cvShowImage("gray",gray);
cvShowImage("dst",dst);
cvWaitKey(0);

Both dst and gray must have their data types same. You cant simply merge a float in a uint matrix. You will have to use cvScale for that.

dst 和 gray 的数据类型必须相同。您不能简单地合并 uint 矩阵中的浮点数。为此,您将不得不使用 cvScale。

回答by thundertrick

I checked cvtColorin OpenCV reference book, and found this:

我在 OpenCV 参考书中检查了cvtColor,发现了这个:

C++:void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

dstCn– Number of channels in the destination image. If the parameter is 0, the number of the channels is derived automatically from src and code .

C++:void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

dstCn– 目标图像中的通道数。如果参数为 0,则通道数自动从 src 和 code 导出。

I have used

我用过了

cvtColor(src,gray,CV_BGR2GRAY,1);

to convert a 3-channels Mat to 1-channels, and it worked. So changing dstCnto 3 might work for you :)

将 3 通道 Mat 转换为 1 通道,并且有效。因此,将dstCn更改为 3 可能对您有用:)

回答by Utkarsh Sinha

Try merging gray,gray,gray into a BGR.

尝试将灰色、灰色、灰色合并为 BGR。

回答by Damilola

IMHO: If input is gray, then output would be gray. The best you can get is a colored distributed image based on pixel data say (110, 110, 110)for pixel data of 110, and not the true colored image

恕我直言:如果输入为灰色,则输出为灰色。你能得到的最好的是基于像素数据的彩色分布式图像,比如(110, 110, 110)的像素数据110,而不是真正的彩色图像