viz12
viz12

Reputation: 725

OpenCV circle like shape detection and its area

enter image description hereI have an image with one circle like shape that contains another similar shape. I am trying find the areas of those two shapes. I am using openCv c++ Hough circle detection, but it does not detect the shapes. Is there any other functions in OpenCV can be used to detect the shapes and find the ares?

[EDIT] The image has been added.

Here is my sample code

int main()
{
  Mat src, gray;
  src = imread( "detect_circles_simple.jpg", 1 );resize(src,src,Size(640,480));
  cvtColor( src, gray, CV_BGR2GRAY );
  // Reduce the noise so we avoid false circle detection
  GaussianBlur( gray, gray, Size(9, 9), 2, 2 );

  vector<Vec3f> circles;

  // Apply the Hough Transform to find the circles
  HoughCircles( gray, circles, CV_HOUGH_GRADIENT, 1, 30, 200, 50, 0, 0 );
  cout << "No. of circles : " << circles.size()<<endl;
  // Draw the circles detected
  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]);
      circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );// circle center     
      circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );// circle outline
      cout << "center : " << center << "\nradius : " << radius << endl;
   }
  exit(0);
  // Show your results
  namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
  imshow( "Hough Circle Transform Demo", src );

  waitKey(0);
  return 0;
}

Upvotes: 4

Views: 7839

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21233

I have a similar approach.

img1 = cv2.imread('disc1.jpg', 1)
img2 = img1.copy()
img = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

#--- Blur the gray scale image
img = cv2.GaussianBlur(img,(5, 5),0)

#--- Perform Canny edge detection (in my case lower = 84 and upper = 255, because I resized the image, may vary in your case)
edges = cv2.Canny(img, lower, upper)
cv2.imshow('Edges', edges )

enter image description here

#---Find and draw all existing contours
_, contours , _= cv2.findContours(edges, cv2.RETR_TREE, 1)
rep = cv2.drawContours(img1, contours, -1, (0,255,0), 3)
cv2.imshow(Contours',rep)

enter image description here

Since you are analyzing the shape of a circular edge, determining the eccentricity of your contours will help in this case.

#---Determine eccentricity
cnt = contours
for i in range(0, len(cnt)):
    ellipse = cv2.fitEllipse(cnt[i])
    (center,axes,orientation) =ellipse
    majoraxis_length = max(axes)
    minoraxis_length = min(axes)
    eccentricity=(np.sqrt(1-(minoraxis_length/majoraxis_length)**2))
    cv2.ellipse(img2,ellipse,(0,0,255),2)

cv2.imshow('Detected ellipse', img2)

enter image description here

Now based on the value given by the eccentricity variable you can come to a conclusion whether your contour is circular or not. The threshold depends on what you consider to be circular or an approximate circle.

Upvotes: 6

Martin Beckett
Martin Beckett

Reputation: 96167

If you have complete shapes (the edge completely or very nearly joins) it is generally easier to edge detect -> contour -> analyse the contour shape.

Hough lines or circles are very useful when you only have small fragments of a line or circle, but can be tricky to tune

edit: Try cv::adaptiveThreshold to get the edges, then cv::findContours.

For each contour compare the area to the perimeter to see if it is the right size to be your target. Then do cv::fitEllipse to check if it is a circle and get the accurate center. FindCOntours also has a mode which tells you which contours are inside which others, so you can easily find one circle inside another.

You might (depending on lighting) find the same circle with 2 or more contours, ie. for the inner and outer edge.

Upvotes: 1

Related Questions