是否有机会将框架中的所有轮廓转换为实心圆?
让我解释一下,我的视频有很多小物体,使用 Mosse 跟踪很难检测到,因为我的物体改变了它的形式。所以我有一个想法,将所有找到的轮廓更改为实心圆圈,这意味着,转换此图像上找到的所有这些对象:
像这样:
我正在使用 python 和 Opencv。
是否有机会将框架中的所有轮廓转换为实心圆?
让我解释一下,我的视频有很多小物体,使用 Mosse 跟踪很难检测到,因为我的物体改变了它的形式。所以我有一个想法,将所有找到的轮廓更改为实心圆圈,这意味着,转换此图像上找到的所有这些对象:
像这样:
我正在使用 python 和 Opencv。
要在实心圆中变换轮廓,一个简单的方法是:
下面是一个关于如何编写上述步骤的示例(它是 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;
}
原始图像
带圆圈的结果