How to find the coordinates of a point w.r.t another point on an image using OpenCV

╄→гoц情女王★ 提交于 2019-12-24 10:03:52

问题


Today 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:


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


回答1:


Before you show the image, use cvPutText to add the desired text. The parameters of this function are self-explaining. The font should be initialized using cvInitFont.

When you calculate the relative coordinates, keep in mind that in OpenCV, the coordinate system is like this

-----> x
|
|
v
y

just in case you are interested in showing the relative coordinates in a system in which the axes point in another direction.

You should check that the Hough transform has detected exactly two circles. If so, all the data you need is in the circles variable. If (xa,ya) are the coordinates of the bigger circle and (xb,yb) the coordinates of the smaller one, the relative coordinates are (xa-xb,ya-yb).



来源:https://stackoverflow.com/questions/5795921/how-to-find-the-coordinates-of-a-point-w-r-t-another-point-on-an-image-using-ope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!