C++ 使用OpenCV检测视频中的基本变化
尝试重新创建一个基本的变更检测程序,我从Adrian Rosebrock写的一个很棒的博客中获得(如果想了解python和OpenCV,请点击此处)。代码是在Python中设计的,我正在尝试将它转换成C++。你可以找到这篇博文。我的难题是使用absdiff(firsFrame,gray,imageDifference),因为循环的每个迭代都有firstFrame和gray相等。我想问题是我初始化了C++ 使用OpenCV检测视频中的基本变化,c++,opencv,motion-detection,C++,Opencv,Motion Detection,尝试重新创建一个基本的变更检测程序,我从Adrian Rosebrock写的一个很棒的博客中获得(如果想了解python和OpenCV,请点击此处)。代码是在Python中设计的,我正在尝试将它转换成C++。你可以找到这篇博文。我的难题是使用absdiff(firsFrame,gray,imageDifference),因为循环的每个迭代都有firstFrame和gray相等。我想问题是我初始化了firstFrame=gray,但是我做了一个cout检查它被点击了多少次,所以不确定。以下是代码:
firstFrame=gray
,但是我做了一个cout
检查它被点击了多少次,所以不确定。以下是代码:
int min_area = 500; //min area of motion detectable
//get camera operational and make sure working correctly
VideoCapture camera(0);
if(!camera.isOpened()){
cout << "cannot open camera" << endl;
return(1);
}
Mat firstFrame, gray, imageDifference, thresh;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
while(true){
Mat frame;
camera.read(frame);
if(frame.empty()){
cout << "frame was not captured" << endl;
return(2);
}
//pre processing
//resize(frame, frame, Size (1200,900));
cvtColor(frame, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size( 21, 21 ), 0, 0 );
//initrialize first frame if necessary
if(firstFrame.empty()){
cout << "hit" << endl;
firstFrame = gray;
continue;
}
//get difference
absdiff(firstFrame, gray, imageDifference);
threshold(imageDifference, thresh, 25, 255, THRESH_BINARY);
//fill in holes
dilate(thresh, thresh, Mat(), Point(-1, -1), 2, 1, 1);
findContours(thresh, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
//loop over contours
for(int i = 0; i < contours.size(); i++){
//get the boundboxes and save the ROI as an Image
if (contourArea(contours[i]) < min_area){
continue;
}
Rect boundRect = boundingRect( Mat(contours[i]));
rectangle( frame, boundRect.tl(), boundRect.br(), (0,255,0), 1, 8, 0 );
}
//draw everything
imshow("Security feed", frame);
imshow("Thresh", thresh);
imshow("Difference", imageDifference);
if (waitKey(30) >= 0)
break;
}
camera.release();
destroyAllWindows();
return(0);
int最小面积=500//最小运动面积
//让摄像头正常工作,确保工作正常
摄像机(0);
如果(!camera.isOpened()){
不能一些需要改进的地方:
firstFrame=gray
=>gray.copyTo(firstFrame)
您在同一行定义firstFrame
和gray
,然后在执行firstFrame=gray
之后,它们共享相同的数据内存。因此每次它们都是相同的
跳过一些帧
由于相机刚刚启动,因此第一帧不太稳定,因此应跳过一些帧(如10帧)
标量元组
在C++中:
cv::标量(b,g,r)元组(b,g,r)
(b,g,r)=>r
修改代码:
int cnt = 0;
while(true) {
Mat frame;
camera.read(frame);
if(frame.empty()) {
cout << "frame was not captured" << endl;
return(2);
}
//pre processing
//resize(frame, frame, Size (1200,900));
cvtColor(frame, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size( 21, 21 ), 0, 0 );
//initrialize first frame if necessary
if(firstFrame.empty()) {
if(cnt<10){
++cnt;
}else{
cout << "hit" << endl;
gray.copyTo(firstFrame);
}
continue;
}
// ...
/// cv::Scalar(b,g,r) <==> tuple(b,g,r)
/// (b,g,r) ==> r
rectangle( frame, boundRect.tl(), boundRect.br(), Scalar(0,255,0), 1, 8, 0 );
// ...
}
int cnt=0;
while(true){
垫架;
摄像机读取(帧);
if(frame.empty()){
不能一些需要改进的地方:
firstFrame=gray
=>gray.copyTo(firstFrame)
您在同一行定义firstFrame
和gray
,然后在执行firstFrame=gray
之后,它们共享相同的数据内存。因此每次它们都是相同的
跳过一些帧
由于相机刚刚启动,因此第一帧不太稳定,因此应跳过一些帧(如10帧)
标量元组
在C++中:
cv::标量(b,g,r)元组(b,g,r)
(b,g,r)=>r
修改代码:
int cnt = 0;
while(true) {
Mat frame;
camera.read(frame);
if(frame.empty()) {
cout << "frame was not captured" << endl;
return(2);
}
//pre processing
//resize(frame, frame, Size (1200,900));
cvtColor(frame, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size( 21, 21 ), 0, 0 );
//initrialize first frame if necessary
if(firstFrame.empty()) {
if(cnt<10){
++cnt;
}else{
cout << "hit" << endl;
gray.copyTo(firstFrame);
}
continue;
}
// ...
/// cv::Scalar(b,g,r) <==> tuple(b,g,r)
/// (b,g,r) ==> r
rectangle( frame, boundRect.tl(), boundRect.br(), Scalar(0,255,0), 1, 8, 0 );
// ...
}
int cnt=0;
while(true){
垫架;
摄像机读取(帧);
if(frame.empty()){
库特