silentassasin 0 Newbie Poster

Hi,
I wrote a program for detecting circles using Hough Transform using OpenCV in C. The program inputs 3 images, each image contains a fixed small circle and a big circle with variable position. The program then recognizes both the circles and marks the centres of both the circles. Now what I want to do is that in the output image the (x,y) coordinates of the centre of the bigger circle should be displayed with respect to the centre of the fixed smaller circle . Here's the code for 'circle.cpp'

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv)
{
    IplImage* img;
    int n=3;
    char input[21],output[21];    

    for(int l=1;l<=n;l++)
    {     
      sprintf(input,"Frame%d.jpg",l);  // Inputs Images

      if(  (img=cvLoadImage(input))!= 0)
    {
        IplImage* gray = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );
        IplImage* canny=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
        IplImage* rgbcanny=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
        CvMemStorage* storage = cvCreateMemStorage(0);
        cvCvtColor( img, gray, CV_BGR2GRAY );
        cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
        cvCanny(gray,canny,50,100,3);

        CvSeq* circles = cvHoughCircles( canny, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
        int i;
        cvCvtColor(canny,rgbcanny,CV_GRAY2BGR);
        for( i = 0; i < circles->total; i++ )
        {
             float* p = (float*)cvGetSeqElem( circles, i );
             cvCircle( rgbcanny, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
             cvCircle( rgbcanny, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
        }
        cvNamedWindow( "circles", 1 );
        cvShowImage( "circles", rgbcanny );

        //Displays Output images
        sprintf(output,"circle%d.jpg",l);   
        cvSaveImage(output,rgbcanny);    
        cvWaitKey(0);
    }
}
    return 0;
}

And here are the input and output images:
[IMG]imgur.com/0vCe0.png[/IMG] [IMG]imgur.com/7RlHP.jpg[/IMG] [IMG]imgur.com/KxMei.jpg[/IMG] [IMG]imgur.com/y9aGq.jpg[/IMG] [IMG]imgur.com/cuAva.jpg[/IMG] [IMG]imgur.com/TscrG.jpg[/IMG]

Please suggest what changes should I make in the code to display the desired (x,y)coordinates. Thanx a lot :)

Dani AI

Generated

After the Hough step the simplest, robust way to get coordinates of the big circle relative to the fixed small one is to pick the two detected centers and subtract them: relative = (x_big - x_small, y_big - y_small). Image coordinates in OpenCV are in pixels with origin at the top-left (x rightwards, y downwards), so a positive y difference means "down" on the image; negate the y difference if a standard Cartesian (y-up) frame is required. The computed vector can also be converted to distance and angle for easier interpretation.

Example (place after the two centers have been identified):

/* small center (xs,ys) and big center (xb,yb) are floats from Hough output */
float dx = xb - xs;
float dy = yb - ys;              /* OpenCV y increases downward */
float dist = sqrtf(dx*dx + dy*dy);
float ang_deg = atan2f(dy, dx) * 57.2957795f;  /* degrees */

char label[64];
snprintf(label, sizeof(label), "(%+.1f,%+.1f) d=%.1f a=%.1fdeg", dx, dy, dist, ang_deg);

CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
cvPutText(rgbcanny, label, cvPoint((int)xb + 10, (int)yb), &font, CV_RGB(255,255,0));

Selecting the two correct circles: if the small circle is truly fixed and smaller in radius, choose the detected circle with the minimum radius as the fixed one and the maximum radius as the moving one. If sizes overlap or noise appears, use additional constraints (expected location, color masking, or stability across frames) to disambiguate.

Practical notes tied to ’s code: insert the relative-coordinate calculation and cvPutText call after circle detection and before cvSaveImage/cvShowImage. If detections are flaky, try running cvHoughCircles on the smoothed gray image (not only the Canny output) and tune the Hough parameters (min/max radius, accumulator threshold). For physical measurements, perform camera calibration or use known circle diameters to convert pixels to real-world units.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.