17
votes

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
ehmmm that's not an easy task! basically is like solving the following problem given 1+2+3 = 6 you must find "1,2,3" given six. Not possible there are lot of sums that output "6" as number. Probably is possible to detect certain colours due to rounding errors (since every color has different max luminance) and then use the "auto-correlation" to expand color areas, still the final image will not have all original colours and will have lower quality anyway since information is always lost.CoffeDeveloper
Im trying to understand what you mean grayscale to RGB. If you simply mean a 3 color component image then you simply copy the grayscale component to all three R, G, and B components of an RGB newly created image. If you truly mean to have color in the image, then you will NEVER be able to end up with the TRUE color image. You at best will only be able to come up with a pseudo-colored VERSION of the image. This is the same as how IR (infrared) images get color.trumpetlicks

4 Answers

22
votes

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.

0
votes

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;
0
votes

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

-1
votes

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