1

I have the code that applies some transformations for image for detecting circles

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

and as for result i got vector<Vec3f> circles; array.

If I draw the result with 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 );

It looks ok(see attached image, blue circle)

But in case I'm trying to draw image not via OpenCV, I've got another circle (or ellipse sometimes)(red circle on attached image):

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

Why does it happen? How can I get coordinates and radius size in pixel to draw it correctly?

enter image description here

p.s: UIImage to Mat conversation i got form documentation

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Siarhei
  • 2,358
  • 3
  • 27
  • 63

1 Answers1

2

CGRectMake needs (x, y, width, height) and not the coordinates to top-left and bottom-right points. So you need to change to:

 CGRect circleRect = CGRectMake(circles[i][0] - circles[i][2] ,
                                circles[i][1] - circles[i][2],
                                2 * circles[i][2],
                                2 * circles[i][2]
                           );
Miki
  • 40,887
  • 13
  • 123
  • 202