OpenCV圈像形状检测及其区域

viz*_*z12 4 c++ geometry opencv hough-transform

在此输入图像描述我有一个像一个圆形的图像,形状包含另一个相似的形状.我正在尝试找到这两种形状的区域.我正在使用openCv c ++ Hough圆检测,但它没有检测到形状.OpenCV中是否还有其他功能可用于检测形状并查找区域?

[编辑]图像已添加.

这是我的示例代码

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;
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*uke 6

我有类似的方法.

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 )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

#---Find and draw all existing contours
_, contours , _= cv2.findContours(edged, cv2.RETR_TREE, 1)
rep = cv2.drawContours(img1, contours, -1, (0,255,0), 3)
cv2.imshow(Contours',rep)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

由于您正在分析圆形边缘的形状,因此在这种情况下确定轮廓的偏心率将有所帮助.

#---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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在根据eccentricity变量给出的值,您可以得出轮廓是否为圆形的结论.阈值取决于您认为是圆形或近似圆形.