c++ - 如何使用 opencv 和 C++ 在图像的特定区域找到轮廓?

标签 c++ opencv image-processing

我是 opencv 的新手。我正在尝试检测图像的特定区域中的车辆,例如大小为 (300, 400) 的图像的 x=0 到 139 和 y=0 到 300。

那么我应该用什么条件来检测特定区域的车辆,并在车辆通过进入框架后对车辆进行计数?

enter code herevoid processVideo(char* videoFilename) {
//create the capture object
`VideoCapture capture(videoFilename);
if(!capture.isOpened()){
    //error in opening the video input
    cerr << "Unable to open video file: " << videoFilename << endl;
    exit(EXIT_FAILURE);
}
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
    //read the current frame
    if(!capture.read(frame)) {
        cerr << "Unable to read next frame." << endl;
        cerr << "Exiting..." << endl;
        exit(EXIT_FAILURE);
    }
    //update the background model
    pMOG2->apply(frame, fgMaskMOG2);
    Mat im_th, src_gray;
    cvtColor( frame, src_gray, COLOR_BGR2GRAY );
    medianBlur( fgMaskMOG2, fgMaskMOG2, 15 );
    threshold( fgMaskMOG2, im_th,230 , 255, THRESH_BINARY_INV);

        Mat im_floodfill = im_th.clone();
       floodFill(im_floodfill, cv::Point(0,0), Scalar(255));

       Mat im_floodfill_inv;
       bitwise_not(im_floodfill, im_floodfill_inv);
       fgMaskMOG2= im_floodfill_inv;

       dilate(fgMaskMOG2, fgMaskMOG2,Mat ());
       erode(fgMaskMOG2, fgMaskMOG2,Mat ());
       medianBlur( fgMaskMOG2, fgMaskMOG2, 15 );
       //adaptiveThreshold(fgMaskMOG2,fgMaskMOG2 ,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,11,2);

               rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                         cv::Scalar(255,0,255), -1);

               stringstream ss;
               ss << capture.get(CAP_PROP_POS_FRAMES);
               string frameNumberString = ss.str();
               putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
                       FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
               //show the current frame and the fg masks
               imshow("Frame", frame);
               imshow("FG Mask MOG 2", fgMaskMOG2);

              vector<vector<Point> > contours;
               vector<Vec4i> hierarchy;

               findContours( fgMaskMOG2, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0) );

               Mat drawing = frame;

                   Rect bounding_rect;

                   vector<Rect> boundRect( contours.size() );
                   vector<vector<Point> > contours_poly( contours.size() );


                   Scalar color( 255,0,0);  // color of the contour in the
                   //Draw the contour and rectangle


                   for( int i = 0; i < s; i++ )
                       { //approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
                       approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
                         boundRect[i] = boundingRect( Mat(contours_poly[i]) );

                       }
);

                   for( int i = 0; i< s; i++ )
                       {
                       drawContours( frame, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
                       rectangle( frame, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
                       }
                   imshow( "Display window", frame);
               }

               keyboard = waitKey(39);
}

最佳答案

改变:

findContours( fgMaskMOG2, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, 
Point(0,0) );

收件人:

findContours( fgMaskMOG2(cv::Rect(x,y,width,height)), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, 
Point(0,0) );

其中 cv::Rect(x,y,width,height) 表示 ROI

关于c++ - 如何使用 opencv 和 C++ 在图像的特定区域找到轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257504/

相关文章:

c++ - Homebrew 公式一步一步安装调试

c++ - 如何从 MySQL Connector/C++ 获取 native C API 连接结构?

image-processing - 使用小波和曲波进行特征提取

c - 外部 uvc -usb 相机中原始数据的默认格式是什么 - yuv420 或 yuv422?

c++ - 删除向上转换为基指针的对象数组

c++ - Arduino 立体声音量表

c++ - 你能把库放在命名空间里吗?

python - 如何实时使用 matplotlib?

opencv - 如何在没有默认YUV2RGB-> RGB2GRAY转换的情况下从H264 4:2:0视频读取Y分量

python - 我怎样才能得到下图的黑白图像?