Showing posts with label Image processing. Show all posts
Showing posts with label Image processing. Show all posts

Use of Hough circles in OpenCV

OpenCV provides and inbuilt function to detect circles in your image. The Hough transform is a technique which can be used to isolate features of a particular shape within an image. The implementation of the circle  transform in OpenCV uses a tricky method called the Hough gradient method ( Link to Google Books ).


cvHoughCircles( ) is the function provided in OpenCV.

CvSeq* cvHoughCircles( 
        CvArr* image,         
        void*  circle_storage, 
        int    method, 
        double dp, 
        double min_dist, 
        double param1 = 100, 
        double param2 = 300, 
        int    min_radius = 0, 
        int    max_radius = 0 
      ); 

Following is a example code snippet.

How to convert IplImage to a matrix in OpenCV


Getting a digital image into numbers can be very handy for your image processing. In this post i will brief just that. I am a beginner in OpenCV and i found it very difficult to find material to do this. I was looking for storing a 8-bit image, 3 channel image into a 3D matrix. That's the method i am going to explain in this post.

Before i get on with the implementation, i would like to brief about the matrix structure that we are going to use. Its a 3D matrix; the row and column values represent the respective X and Y co-ordinates of the image. That is each value in the matrix represent a pixel. I am considering a 3 channeled image, so each pixel will have 3 channel values i.e for BGR. For more info on the matrix basic you can refer this link.

Following is the method I used..

First load a 8-Bit image with 3 channels RGB and create define a matrix structure accordingly.

          IplImage *img = cvLoadImage( "cataract.jpg");
    CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );

Next use the cvConvert function. cvConvert is a Macro provided by OpenCV which is the same as cvConvertScale() but is conventionally used when the scale and shift arguments will be let at their default values. 

           cvConvert( img, mat );

That does the work! Now your IplImage is stored into the matrix. You can retrieve the matrix values just by specifying the corresponding X,Y co-ordinate values in the cvGet2D function. I have used a for loop to print the RGB values of a small portion of the image.


        for(int i=0;i<10;i++)
        {
          for(int j=0;j<10;j++)
            {
               CvScalar scal = cvGet2D( mat,j,i);
               printf( "(%.f,%.f,%.f)  ",scal.val[0], scal.val[1], scal.val[2] );
            }
          printf("\n");
        }

cvScalar function is used to define a variable that stores the RBG value retrieved. You can check the OpenCV Reference links for some more tutorial links on OpenCV.

Please leave a comment so that know if this post is helpful or not. Also mention if any changes is required. It could help others :)