【问题标题】:How can I get the minimum enclosing circle with OPENCV?如何使用 OPENCV 获得最小封闭圆?
【发布时间】:2017-11-23 14:36:02
【问题描述】:

我正在使用cv::minEnclosingCircle(...) 来获得与我的轮廓完全一致的最小圆,但我得到的圆要大一点。

换句话说,我试图得到这样的东西:

https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Simple_concave_polygon_Min_Enclosing_Circle.svg/441px-Simple_concave_polygon_Min_Enclosing_Circle.svg.png

但我得到了这个(圆圈):

注意圆圈比要包围的项目大一点。

我需要将我的对象围成一个圆圈,而不是一个椭圆。

提前谢谢你。

这是我的代码:

cv::vector<cv::Point> allPixels;
    int columnas = img.cols, filas = img.rows;
    cv::Point pt;
    for(int col = 0; col < columnas; col++){
        for(int row = 0; row < filas; row++){

            int val = img.at<uchar>(row,col);

            if(val == 255){
                pt.x = col;
                pt.y = row;

                allPixels.push_back(pt);
            }
        }
    }

    cv::Mat dispImage = img.clone();
    cv::Point2f center;
    float radius;

    cv::minEnclosingCircle(allPixels,center,radius);

    cv::circle(dispImage,center,radius,cv::Scalar(128),1);
    cv::circle(dispImage,center,1,cv::Scalar(128),1);

    cv::imwrite("Enclosing_Result.png",dispImage);

使用 'img' 一个 cv::Mat,大小为 (760,760),格式为 CV_8UC1。最终结果(“Enclosure_Result.png”)是下一个:

我的目标如下(绘制):

【问题讨论】:

  • 你应该展示你的代码。
  • 有代码,谢谢

标签: c++ opencv


【解决方案1】:

我的结果还可以。


1. 只针对一个地区

## only one region
cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]

(x,y), r = cv2.minEnclosingCircle(cnts[0])


2. 不止一个地区

## more than one region
mask = threshed.copy()

## find centers 
for cnt in cnts:
    (x,y), r = cv2.minEnclosingCircle(cnt)
    pt = (int(x), int(y))
    centers.append(pt)

## connect the `centers`
for i in range(1, len(centers)):
    cv2.line(mask, centers[i-1], centers[i], 255, 2)

## find the center 
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
(x,y), r = cv2.minEnclosingCircle(cnts[0])

【讨论】:

  • 那是 Python,不是吗?
  • 是的,我刚做个测试,没关系。
  • 好的,但是具有不同轮廓的图像呢?我想得到这样的东西:oi67.tinypic.com/296cghe.jpg
  • 此图像需要更多步骤:连接“中心”,然后再次执行minEnclosingCircle
  • 感谢编辑。我已经完成了类似的步骤。我没有使用线条,而是使用了每个轮廓的最小包围圆,然后是所有轮廓中的最小值。但我仍然遇到同样的问题:圆总是比轮廓大一点(我使用的是 C++-CLI)。也许我使用了错误的参数?谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
相关资源
最近更新 更多