将图像颜色从灰度转换为 RGB OpenCV C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14571790/
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
Convert Image Color from Grayscale to RGB OpenCV C++
提问by Joey Tawadrous
Basically I am trying to convert the below output image to color(RGB). The image that this code currently outputs is grayscale, however, for my application I would like it to be output as color. Please let me know where I should convert the image.
基本上我试图将下面的输出图像转换为颜色(RGB)。此代码当前输出的图像是灰度的,但是,对于我的应用程序,我希望它以颜色的形式输出。请让我知道我应该在哪里转换图像。
Also the code below is C++ and it using a function from openCV. Please keep in mind that I am using a wrapper to use this code in my iphone application.
下面的代码也是 C++,它使用了 openCV 中的一个函数。请记住,我正在使用包装器在我的 iphone 应用程序中使用此代码。
cv::Mat CVCircles::detectedCirclesInImage(cv::Mat img, double dp, double minDist, double param1, double param2, int min_radius, int max_radius) {
//(cv::Mat img, double minDist, int min_radius, int max_radius)
if(img.empty())
{
cout << "can not open image " << endl;
return img;
}
Mat cimg;
medianBlur(img, img, 5);
cvtColor(img, cimg, CV_GRAY2RGB);
vector<Vec3f> circles;
HoughCircles( img //InputArray
, circles //OutputArray
, CV_HOUGH_GRADIENT //int method
, 1//dp //double dp=1 1 ... 20
, minDist //double minDist=10 log 1...1000
, 100//param1 //double param1=100
, 30//param2 //double param2=30 10 ... 50
, min_radius //int minRadius=1 1 ... 500
, max_radius //int maxRadius=30 1 ... 500
);
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
circle( cimg, Point(c[0], c[1]), c[2], Scalar(255,0,0), 3, CV_AA);
circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, CV_AA);
}
return cimg;
}
回答by foundry
This is currently set up to expect a grayscale image as input. I think that you are asking how to adapt it to accept a colour input image and return a colour output image. You don't need to change much:
当前设置为期望灰度图像作为输入。我认为您是在问如何调整它以接受彩色输入图像并返回彩色输出图像。你不需要改变太多:
cv::Mat CVCircles::detectedCirclesInImage(cv::Mat img, double dp, double minDist, double param1, double param2, int min_radius, int max_radius) {
if(img.empty())
{
cout << "can not open image " << endl;
return img;
}
Mat img;
if (img.type()==CV_8UC1) {
//input image is grayscale
cvtColor(img, cimg, CV_GRAY2RGB);
} else {
//input image is colour
cimg = img;
cvtColor(img, img, CV_RGB2GRAY);
}
the rest stays as is.
其余保持原样。
If your input image is colour, you are converting it to gray for processing by HoughCircles, and applying the found circles to the original colour image for output.
如果您的输入图像是彩色的,您会将其转换为灰色以供 HoughCircles 处理,并将找到的圆圈应用于原始彩色图像以进行输出。
回答by sumeet
The code you have pasted is returning colored image.
您粘贴的代码正在返回彩色图像。
You are already doing cvtColor(img, cimg, CV_GRAY2RGB), and then I don't see cimg getting converted to grayscale anywhere !, To verify it try displaying it before returning from this function :
您已经在执行 cvtColor(img, cimg, CV_GRAY2RGB),然后我在任何地方都看不到 cimg 转换为灰度!,要验证它,请在从此函数返回之前尝试显示它:
imshow("c",cimg);
waitKey(0);
return cimg;
回答by trumpetlicks
The cvtImage routine will simply copy your gray element to each of the three elements R, G, and B for each pixel. In other words if the pixel gray value is 26, then the new image will have R = 26, G = 26, B = 26.
cvtImage 例程将简单地将您的灰色元素复制到每个像素的三个元素 R、G 和 B 中的每一个。换句话说,如果像素灰度值为 26,则新图像将具有 R = 26、G = 26、B = 26。
The image presented will still LOOK grayscale even though it contains all 3 color components, all you have essentially done is to triple the space necessary to store the same image.
即使显示的图像包含所有 3 种颜色分量,它仍然看起来是灰度的,您基本上所做的只是将存储相同图像所需的空间增加三倍。
If indeed you want color to appear in the image (when you view it), this is truly impossible to go from grayscale back to the ORIGINAL colors. There are however means of pseudo-coloring or false coloring the image.
如果您确实希望图像中出现颜色(当您查看它时),那么从灰度返回原始颜色确实是不可能的。然而,存在对图像进行伪着色或假着色的方法。
http://en.wikipedia.org/wiki/False_color
http://en.wikipedia.org/wiki/False_color
http://blog.martinperis.com/2011/09/opencv-pseudocolor-and-chroma-depth.html
http://blog.martinperis.com/2011/09/opencv-pseudocolor-and-chroma-depth.html
http://podeplace.blogspot.com/2012/11/opencv-pseudocolors.html
http://podeplace.blogspot.com/2012/11/opencv-pseudocolors.html
回答by Vinod Paul
You can draw circles to the input color image. Check the documentation given in the openCV
您可以在输入的彩色图像上绘制圆圈。检查 openCV 中给出的文档
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html