Convert Image Color from Grayscale to RGB OpenCV C

2019-01-26 04:16发布

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.

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.

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;
}

4条回答
Explosion°爆炸
2楼-- · 2019-01-26 04:32

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.

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.

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://blog.martinperis.com/2011/09/opencv-pseudocolor-and-chroma-depth.html

http://podeplace.blogspot.com/2012/11/opencv-pseudocolors.html

查看更多
女痞
3楼-- · 2019-01-26 04:32

You can draw circles to the input color image. Check the documentation given in the openCV

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

查看更多
Juvenile、少年°
4楼-- · 2019-01-26 04:34

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 :

imshow("c",cimg);
waitKey(0);
return cimg;
查看更多
来,给爷笑一个
5楼-- · 2019-01-26 04:41

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.

查看更多
登录 后发表回答