OpenCV 以像素为单位获取坐标/半径

OpenCV get coordinates / radius in pixel

我有对图像应用一些变换以检测圆圈的代码

(GaussianBlur->cvtColor(gray)->canny->HoughCircles)

结果我得到了vector<Vec3f> circles;数组。

如果我用OpenCV绘制结果:

cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
    //circle center
circle( originalMat, center, 3, Scalar(0,255,0), -1, 8, 0 );
    // circle outline
circle( originalMat, center, radius, Scalar(0,0,255), 1, 8, 0 );

看起来不错(见附图,蓝圈

但是如果我不是通过 OpenCV 尝试绘制图像,我有另一个圆圈(有时是椭圆形)(附加图像上的红色圆圈):

 UIGraphicsBeginImageContext(image.size); //the same size as original
 [image drawAtPoint:CGPointZero];
 CGContextRef ctx = UIGraphicsGetCurrentContext();
 [[UIColor redColor] setStroke];
 CGRect circleRect = CGRectMake(circles[i][0] - circles[i][2] ,
                                circles[i][1] - circles[i][2],
                                circles[i][0] + circles[i][2],
                                circles[i][1] + circles[i][2]
                               );
circleRect = CGRectInset(circleRect, 5, 5);
CGContextStrokeEllipseInRect(ctx, circleRect);
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

为什么会这样?如何获得以像素为单位的坐标和半径大小以正确绘制它?

p.s:UIImageMat 对话我得到了形式 documentation

CGRectMake needs (x, y, width, height) 而不是 左上角 右下角 点的坐标。所以你需要改为:

 CGRect circleRect = CGRectMake(circles[i][0] - circles[i][2] ,
                                circles[i][1] - circles[i][2],
                                2 * circles[i][2],
                                2 * circles[i][2]
                           );