這是背景減法技術的代碼。如果背景中有任何變化(在這種情況下是你的車道),你將能夠檢測到它。
你應該有一個由openCV樣本提供的「Background subtraction」的示例代碼。
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap;
bool update_bg_model = true;
cap.open(0);
cv::BackgroundSubtractorMOG2 bg;//(100, 3, 0.3, 5);
bg.set ("nmixtures", 3);
std::vector < std::vector <cv::Point> >contours;
cv::namedWindow ("Frame");
cv::namedWindow ("Background");
Mat frame, fgmask, fgimg, backgroundImage;
for(;;)
{
cap >> frame;
bg.operator()(frame, fgimg);
bg.getBackgroundImage (backgroundImage);
cv::erode (fgimg, fgimg, cv::Mat());
cv::dilate (fgimg, fgimg, cv::Mat());
cv::findContours (fgimg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::drawContours (frame, contours, -1, cv::Scalar (0, 0, 255), 2);
cv::imshow ("Frame", frame);
cv::imshow ("Background", backgroundImage);
char k = (char)waitKey(30);
if(k == 27) break;
}
return 0;
}
來源
2014-02-22 11:11:44
skm
[OpenCV如何使用背景減法方法] (http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html) – Haris