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

时间:2017-02-28 11:35:26

标签: ios opencv image-processing opencv3.0 opencv3.1

我有一些代码可以应用一些图像转换来检测圆圈

af5

至于结果我得到了GaussianBlur->cvtColor(gray)->canny->HoughCircles数组。

如果我用OpenCV绘制结果:

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

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

enter image description here

p.s: 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(); UIImage我的对话documentation

1 个答案:

答案 0 :(得分:2)

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

C