【问题标题】:how to transform contours to filled circles?如何将轮廓转换为实心圆?
【发布时间】:2015-07-28 06:23:44
【问题描述】:

是否有机会将框架中的所有轮廓转换为实心圆?

让我解释一下,我的视频有很多小物体,使用 Mosse 跟踪很难检测到,因为我的物体改变了它的形式。于是我有了一个想法,把找到的所有轮廓都改成实心圆圈,意思是,把这张图片上找到的所有这些对象都转换一下:

到这样的事情:

我正在使用 python 和 Opencv。

【问题讨论】:

    标签: python opencv tracking opencv-contour


    【解决方案1】:

    要在实心圆中变换轮廓,一个简单的方法是:

    1. 获取每个轮廓
    2. 找到轮廓的边界框
    3. 找到边界框的中心,将是圆的中心
    4. 找到边界框的对角线,将是圆的半径
    5. 用给定的圆心和半径画一个圆。

    下面是一个关于如何编写上述步骤的示例(它是 C++,但您可以轻松地移植到 python):

    #include "opencv2/opencv.hpp"
    using namespace cv;
    
    int main(int, char**)
    {
        Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    
        // This will enlarge the circles by a "factor"
        float factor = 2;
    
        // Find blobs and extract contours
        vector<vector<Point>> contours;
        findContours(img.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    
        Mat1b res(img.size(), uchar(0));
    
        for (int i = 0; i < contours.size(); ++i)
        {
            // Get the bounding box of the contours
            Rect bb = boundingRect(contours[i]);
    
            // Get the center of the bounding box
            // Will be the center of the circle
            Point center(bb.x + bb.width/2, bb.y + bb.height/2);
    
            // Get the length of the diagonal of the bounding box.
            // Will be the radius of the circle (eventually multiplied by a factor)
            int radius = sqrt(bb.width*bb.width + bb.height*bb.height) / 2;
    
            // Draw the circle
            circle(res, center, radius*factor, Scalar(255), CV_FILLED);
        }
    
        return 0;
    }
    

    原图

    带圆圈的结果

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2020-01-14
      相关资源
      最近更新 更多