2015-06-20 31 views
3

如何使用OpenCV Hough Circle转换获得精确的中心点?我需要在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 ? 
+0

请定义_“精确”_更多[在您的问题](http://stackoverflow.com/posts/30958751/edit)! –

+0

您似乎在执行'int/int'除法,这会导致(必然会截断)整数。尝试在分区之前将参数转换为“float”或“double”(无论库使用什么)*。 – o11c

+0

我看不到我在做int/int divison。即时打印仅霍夫转换的圆矢量 –

回答

1

这不是你的数据丢失了什么。这只是你跳过一些数字的打印功能。

尝试的std :: setprecision:

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

不要忘记:

#include <iomanip> 

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

+0

实际上,就像我写的,我只需要小数点后的3-4个数字,但是您的方法并不能解决问题。我试过精度10,但只有弧度在小数点后有10个数字,而xy坐标是相同的....如果你看看我的输出结果,它们总是结束到5 –