Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Image Color from Grayscale to RGB OpenCV C++

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;
}
like image 432
dev Avatar asked Jan 28 '13 21:01

dev


1 Answers

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.

like image 130
foundry Avatar answered Oct 20 '22 19:10

foundry