boundingrect在OpenCV C ++中不适用于轮廓 [英] boundingrect not working for a contour in opencv c++

查看:1086
本文介绍了boundingrect在OpenCV C ++中不适用于轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{

 #include<opencv2\opencv.hpp>
 #include<iostream>
 #include<conio.h>

 using namespace std;
 using namespace cv;

 int main()
{
int a = 0;

Mat frame, diffimage,back,frame_gray;
VideoCapture cap("D:\\elance\\check\\Sent3.avi");
vector<vector<Point>> contours;
BackgroundSubtractorMOG2 bg;
vector<int> params;
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(9);

for (int i = 0; i < 200;i++)
{
    cap >> frame;
    if (frame.empty())
        break;
    bg(frame,back);
}

bg.getBackgroundImage(back);
cap.set(CV_CAP_PROP_POS_FRAMES,0);
cvtColor(back, back, CV_BGR2GRAY);
//for (int f = 0; f < 20;f++)
while (1)
{
    a = a + 1;
    cout << "Frame no : " << a<<endl;
    cap >> frame;
    if (frame.empty())
        break;
    cvtColor(frame, frame_gray, CV_BGR2GRAY);
    absdiff(frame_gray, back, diffimage);
    threshold(diffimage, diffimage, 10, 255, CV_THRESH_BINARY);

    for (int i = 0; i < 2; i++)
    {
        cv::erode(diffimage, diffimage, cv::Mat());
        cv::dilate(diffimage, diffimage, cv::Mat());
    }
    findContours(diffimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    cout << "Contour Size : " << contours.size()<<endl;
    vector<Rect> boundRect(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        drawContours(frame, contours, i, cv::Scalar(0, 255, 255), 1);
        Mat smoothCont;
        smoothCont = cv::Mat(contours[i]);
        cout << smoothCont.rows << "\t" << smoothCont.cols <<"\t"<<smoothCont.depth()<< endl << endl;
        if (smoothCont.rows > 0 && smoothCont.rows < 10000)
            boundRect[i] = boundingRect(Mat(contours[i]));
    }
    for (int i = 0; i < contours.size(); i++)
        rectangle(frame, boundRect[i], Scalar(0, 255, 255), 1, 8, 0);
    imshow("Diff", diffimage);
    imshow("frame", frame);
    imwrite("D:\\test.jpg", frame, params);
    waitKey(30);
    break;
}

}

此代码基本上采用轮廓,结果是轮廓上的矩形.但是只有一个以框为界,而其他轮廓仍然不是以框为界.

This code basically takes the contours and results are the rectangles on the contours. But only one is bounded by the box and other contour is is still not bounded box.

任何人都可以在这件事上提供帮助吗?

Can anyone help in this matter ?

推荐答案

也许是如果(smoothCont.rows> 0&& smoothCont.rows< 10000)"将它们过滤掉了?

Maybe "if (smoothCont.rows > 0 && smoothCont.rows < 10000)" filtered them out?

这篇关于boundingrect在OpenCV C ++中不适用于轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆