0

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

让我解释一下,我的视频有很多小物体,使用 Mosse 跟踪很难检测到,因为我的物体改变了它的形式。所以我有一个想法,将所有找到的轮廓更改为实心圆圈,这意味着,转换此图像上找到的所有这些对象:

在此处输入图像描述

像这样:

在此处输入图像描述

我正在使用 python 和 Opencv。

4

1 回答 1

0

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

  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;
}

原始图像

在此处输入图像描述

带圆圈的结果

在此处输入图像描述

于 2015-07-29T10:08:37.107 回答