如何使用 OpenCV 查找图像上一个点相对于另一个点的坐标

发布于 2024-11-03 18:45:17 字数 2180 浏览 3 评论 0原文

今天我用C语言编写了一个使用OpenCV使用霍夫变换检测圆的程序。 程序输入3张图像,每张图像包含一个固定的小圆和一个位置可变的大圆。然后程序识别这两个圆并标记两个圆的中心。现在我想做的是,在输出图像中,应相对于固定小圆的中心显示大圆中心的 (x,y) 坐标。这是“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;
}

,这是输入和输出图像:
在此处输入图像描述 在此处输入图像描述 在此处输入图像描述
在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

请建议我应该在代码中进行哪些更改才能显示所需的(x ,y) 坐标。非常感谢:)

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:
enter image description here enter image description here enter image description here
enter image description here enter image description here enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-11-10 18:45:17

在显示图像之前,请使用 cvPutText 添加所需的文本。该函数的参数是不言自明的。应使用 cvInitFont 初始化字体。

当您计算相对坐标时,请记住,在 OpenCV 中,坐标系是这样的,

-----> x
|
|
v
y

以防万一您有兴趣在轴指向另一个方向的系统中显示相对坐标。

您应该检查霍夫变换是否恰好检测到两个圆。如果是这样,您需要的所有数据都在 circles 变量中。如果(xa,ya)是大圆的坐标,(xb,yb)是小圆的坐标,则相对坐标是(xa-xb,ya-yb)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文