xcode - 使用OpenCV仅检测圆

标签 xcode opencv

我正在使用以下代码仅检测圆圈。但是它也在检测其他形状。请帮忙做到这一点。我已经使用了HoughCircles,但是没有给出良好的结果。我的要求是只检测圆圈。

Mat src, src_gray;

/// Read the image
src = t2;

if(! src.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    cv::waitKey(5000);
}

/// Convert it to gray
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );

/// Reduce the noise so we avoid false circle detection
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );


Mat src_lines; Mat src_gray_lines;
int thresh_lines = 100;
RNG rng_lines(12345);

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

src_gray_lines = src_gray;
/// Detect edges using Threshold
threshold( src_gray_lines, threshold_output, thresh_lines, 255, THRESH_BINARY );
/// Find contours
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() );

for( size_t i = 0; i < contours.size(); i++ )
{
    minRect[i] = minAreaRect( Mat(contours[i]) );
}

/// Draw contours + rotated rects + ellipses
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
    // rotated rectangle
    Point2f rect_points[4];
    minRect[i].points( rect_points );
    for( int j = 0; j < 4; j++ )
        line( src, rect_points[j], rect_points[(j+1)%4], Scalar(255,0,0), 1, 8 );
}

如果我的问题不清楚,请告诉我。

最佳答案

您具有轮廓 vector ,因此可以轻松检查它们的长度。圈也有面积。
圆形应具有特定的面积与长度之比(您应该计算该比例应为多少)。现在,消除不适合此比例的形状(带有一些增量),您将只得到圆。

关于xcode - 使用OpenCV仅检测圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13873617/

相关文章:

python-3.x - openvino 在树莓派 4 中运行推理几秒钟后崩溃

python - OpenCV:WAITING不同的 key ?

c++ - 在 beaglebone black 上安装 opencv

ios - 在不同计算机上打开项目时,找不到生成的 Coredata 类和扩展的构建输入文件

c++ - 如何在 Xcode 中包含 .a 库?

ios - 如何让 Controller 处理 View 中添加的手势? iOS

python - 将图像 opencv c++ 发送到 python

c++ - 在来自相机的视频上显示一个矩形

iphone - iOS 应用程序为调用添加前缀

xcode - 我可以将USDZ转换为诸如STL的实体网格吗