【问题标题】:How can i get accurate center points?如何获得准确的中心点?
【发布时间】:2015-09-06 15:40:38
【问题描述】:

如何使用 OpenCV 霍​​夫圆变换获得准确的中心点?我在 x,y 坐标中需要更多精确的小数位。

(准确地说,我的意思是这样的) 我用matlab得到了这些中心坐标

x107,775526315904   y112,963480232638
x469,550463441518   y208,309866770404
x217,386414755458   y490,882895036058

这是我的代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;


int main()
{
    Mat src, src_gray, src0;
    printf("Give the name of the picture\n");
    char ImageName[20];
    scanf("%s", &ImageName);
    /// Read the image 
    src = imread(ImageName, 1);
    if (!src.data)
    {
        printf("no image data\n");
        return main();
    }
    // src0 = src;
    // imshow("orignal", src0);
    /// Convert it to gray
    cvtColor(src, src_gray, CV_BGR2GRAY);
    GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
    vector<Vec3f> circles;
    /// Apply the Hough Transform to find the circles
    HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 0.92, src_gray.rows / 10, 20, 10, 55, 60);
    for (size_t i = 0; i < circles.size(); i++)
    {
        Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
        int radius = cvRound(circles[i][2]);
    //  printf("%d center x %d y %d\n", i, cvRound(circles[i][0]), cvRound(circles[i][1]));
    //  printf("rad: %d\n", cvRound(circles[i][2]));
        cout << i + 1 << " centerpoint " << " x: " << circles[i][0] << "   y: " << circles[i][1] << "rad: " << circles[i][2] << endl;
        // cout << i + 1 << " othermethod" << "x : " << center.x << endl;
        // circle center
        circle(src, center, 1, Scalar(0, 255, 0), -1, 8, 0);
        // circle outline
        circle(src, center, radius, Scalar(255, 0, 0), 1, 8, 0);
    }
    imshow("circlefinder", src);
    waitKey(1);
    system("PAUSE");
    cvDestroyWindow("orignal");
    cvDestroyWindow("circlefinder");
    printf("new picture ?(y/n)");
    char ans[1];
    scanf("%s", &ans);
    if (strcmp(ans, "y"))
    {
        return 0;
    }
    else if (strcmp(ans, "n"))
    {
        return main();
    }

}

这是输出:

Give the name of the picture
as2.jpg
145.5test x
1 centerpoint  x: 105.5   y: 112.5rad: 55.5563
2 centerpoint  x: 235.5   y: 427.5rad: 56.626
3 centerpoint  x: 466.5   y: 196.5rad: 55.5203
A folytatáshoz nyomjon meg egy billentyűt . . .
new picture ?

【问题讨论】:

  • 请定义“准确”更多in your question
  • 您似乎在做int/int 除法,这会产生一个(必然被截断的)整数。尝试将参数转换为floatdouble(无论库使用什么)除法之前。
  • 我看不到我在哪里做 int/int divison。我只打印霍夫变换中的圆向量
  • 虽然结果不够好?使用更高分辨率的图像!
  • 我需要它在实验室进行实验,实际上当我使用 houghcircles 时,它会从显微镜相机获取图像。虽然它的高清(1080p)相机我只使用图像的 300x300 部分

标签: c++ opencv transform geometry hough-transform


【解决方案1】:

丢失的不是您的数据。这只是您跳过一些数字的打印功能。

试试 std::setprecision:

std::cout << 432.123456789f << "   " << std::setprecision(10) << 432.123456789f << std::endl;

别忘了:

#include <iomanip>

无论如何,我认为您并不需要那种精确度。您在图像处理中的测量误差通常很大,最后一位数字并不能提供任何真实信息。

【讨论】:

  • 实际上,正如我所写,我只需要小数点后 3-4 位数字,但您的方法不能解决问题。我尝试了精度 10,但只有弧度在小数点后有 10 个数字,x y 坐标相同....如果你看看我的输出,它们总是以 5 结尾
  • Istvan Grexa 您有没有找到提高 OpenCV 霍​​夫圆变换精度的方法?为什么这些值总是 nnn.5 ?
猜你喜欢
  • 2020-12-13
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多