[英]filtering unwanted contours - opencv with C++
我正在处理要检测一些物体的视频。
首先,我删除背景,然后将其设置为灰色,并使用cv::threshold
制作二进制图像,然后再使用cv::findcontours
。
我要删除太长和太短的轮廓。 因此,我在“ Opencv 2食谱”中找到了一个代码。 但是在我的视频的第21帧上有运行时错误。
// Eliminate too short or too long contours
size_t cmin{ 15 }; // minimum contour length
size_t cmax{ 120 }; // maximum contour length
std::vector<std::vector<cv::Point> >::
const_iterator itc = contours.begin();
while (itc != contours.end()) {
if (itc->size() < cmin || itc->size() > cmax)
itc = contours.erase(itc);
else
++itc;
}
我搜索并找到另一个代码,并且在同一帧上有相同的错误:
double min_area = 500; // area threshold
double max_area = 1500; // area threshold
for (int i = 0; i < contours.size(); i++) // iterate through each contour.
{
double area = contourArea(contours[i], false); // Find the area of contour
if (area < min_area || area > max_area)
contours.erase(contours.begin() + i);
}
我写了一个代码来创建一个新矢量,然后在其中复制可接受的轮廓。 但在同一帧上也有相同的错误:
std::vector<std::vector<cv::Point> > goodcontours;
size_t cmin{ 15 };
size_t cmax{ 120 };
double contourlength = 1;
size_t contoursize = contours.size();
for (int i = 0; i != contoursize;i++) {
contourlength = cv::arcLength(contours[i], true);
if (contourlength > cmin && contourlength < cmax)
goodcontours.push_back(contours.at(i));
}
我尝试了其他视频,但所有视频在特定框架上都存在相同的问题。
在出现错误之前,我仍然可以看到不需要的轮廓,这意味着它们并没有真正消除。
错误只是说:
0x75017FB2(ucrtbase.dll)的未处理异常
您对过滤轮廓有什么建议吗?
我的一位朋友回答了我的问题,我将与您分享:
cv::findContours(image,contours,CV_RETR_LIST,mode,cv::Point(offset_x,offset_y));
for(int i=0;i<contours.size();i++)
{
AreaContour=cv::contourArea(contours[i]);
if(AreaContour<MaxAreaContour && AreaContour>MinAreaContour)
drawContours(Result,contours,i,cv::Scalar(DrawColor),Thickness,LineType,cv::noArray(),2147483647,cv::Point(DrawOffset_x,DrawOffset_y));
}
这是我的Find_Filter_Draw Contours函数,共享它可能对某人有用
bool Find_Filter_Draw_Contours(){
// Find Contours on our working image which is ... currentImage.
cv::Mat hierarchy = cv::Mat();
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = currentImage.clone();
cv::findContours(contourOutput, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE);
hierarchy.release();
// filter them based on Area. good to remove small dots
double minArea = 50;
double maxArea = 900000;
std::vector<std::vector<cv::Point> > GoodContours;
for (size_t idx = 0; idx < contours.size(); idx++) {
double area = cv::contourArea(contours[idx]);
// http://www.cplusplus.com/reference/limits/numeric_limits/
if (area >= (minArea == -1 ? std::numeric_limits<double>::min() : minArea) && area <= (maxArea == -1 ? std::numeric_limits<double>::max() : maxArea)) {
GoodContours.push_back(contours.at(idx));
}
}
//Draw the contours
cv::Mat contourImage(currentImage.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < GoodContours.size(); idx++) {
cv::drawContours(contourImage, GoodContours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", currentImage);
cv::imshow("Contours", contourImage);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.