我正在使用背景扣除来检测OpenCV
中的移动车辆
检测到移动物体并在检测到的物体周围创建矩形。
我输入了包含移动物体的视频。
问题是:
我不知道如何计算移动物体的速度。我尝试在论坛,Google,StackOverflow上搜索,但对如何计算速度一无所知。
我希望实施与此YouTube video
中实施的内容相同的内容这是我的代码:
BgDetection.cpp
#include "BgDetection.h"
int BgDetection1();
using namespace cv;
int BgDetection1()
{
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
CvSeq* seq;
cv::VideoCapture cap("D:/Eclipse/bglib/video2.avi");
cap >> frame;
cv::initModule_video();
cv::BackgroundSubtractorMOG2 bg(100, 16, true); // history is an int, distance_threshold is an int (usually set to 16), shadow_detection is a bool
bg.set("nmixtures", 3);
bg(frame, fore, -1); //learning_rate = -1 here
std::vector<std::vector<cv::Point> > contours;
cv::namedWindow("Frame");
cv::namedWindow("Background");
for(;;)
{
cap >> frame;
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
std::vector<cv::Vec4i> hierarchy;
cv::findContours( fore, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(600,200));
for ( size_t i=0; i<contours.size(); ++i )
{
cv::drawContours( frame, contours, i, Scalar(200,0,0), 1, 8, hierarchy, 0, Point() );
cv::Rect brect = cv::boundingRect(contours[i]);
cv::rectangle(frame, brect, Scalar(255,0,0));
}
//cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
cv::imshow("Frame",frame);
cv::imshow("Background",back);
if(cv::waitKey(30) >= 0) break;
}
return 0;
}
BgDetection.h
#ifndef BGDETECTION_H_INCLUDED
#define BGDETECTION_H_INCLUDED
#include <iostream>
#include <sys/stat.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include "opencv2/features2d/features2d.hpp"
#include <opencv/highgui.h>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <vector>
#pragma comment (lib , "opencv_core244d.lib")
#pragma comment (lib ,"opencv_highgui244d.lib")
#pragma comment(lib , "opencv_imgproc244d.lib")
#pragma comment(lib ,"opencv_video244.lib")
int BgDetection1();
#endif // BGDETECTION_H_INCLUDED
的main.cpp
#include <iostream>
#include "BgDetection.h"
using namespace std;
int main()
{
cout << BgDetection1() << endl;
return 0;
}
任何帮助表示感谢。
答案 0 :(得分:1)
单个对象
如果要跟踪移动物体周围的单个矩形,则矩形在每个帧中都有一个唯一的中心。
中心位置之间的差异可能会用于生成瞬时速度矢量。
我对c ++中的opencv语法的记忆有点生疏,但有点像
// outside t-loop
cap >> frame;
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
std::vector<cv::Vec4i> hierarchy;
cv::findContours( fore, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
int i =0;
cv::drawContours( frame, contours, i, Scalar(200,0,0), 1, 8, hierarchy, 0, Point() );
cv::Rect rectold = cv::boundingRect(contours[i]);
cv::rectangle(frame, rectold, Scalar(255,0,0));
//cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
cv::imshow("Frame",frame);
cv::imshow("Background",back);
if(cv::waitKey(30) >= 0) break;
// Within t-loop
cv::Rect newrect = cv::boundingRect(contours[i]);
double vx = newrect.x - oldrect.x;
double vy = newrect.y - oldrect.y;
oldrect = newrect;
多个对象
如果您有多个对象,则可以在帧t和t + 1中为对象生成点列表,然后在两个点集上进行点集匹配。
取决于我建议的跟踪复杂性