gpt4 book ai didi

c++ - 如何使用 OpenCv 中的背景减法测量移动车速

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:49 24 4
gpt4 key购买 nike

我正在使用 背景减法OpenCV 中检测移动车辆。
检测到移动物体并在检测到的物体周围创建一个矩形。我输入了其中有移动物体的视频。

问题是:
我不知道如何计算移动物体的速度。我尝试在论坛、谷歌、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;
}

感谢任何帮助。

最佳答案

单个对象

如果您正在跟踪移动对象周围的单个矩形,则该矩形在每一帧中都有一个唯一的中心。

中心位置之间的差异可用于生成瞬时速度 vector 。

我对 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 中的对象生成一个点列表,然后对两个点集进行点集匹配。

根据我建议的跟踪复杂度

  • 如果分配本质上是微不足道的,则进行简单的最近邻匹配
  • 全局最近的邻居(例如 Jonkers-Volgenant http://www.assignmentproblems.com/LAPJV.htm )用于更困难的事情
  • 如果这仍然不起作用,您可能必须深入研究状态估计(有关基本示例,请参阅 Kalman filter)并在调用 LAPJV 之前设计成本函数。

关于c++ - 如何使用 OpenCv 中的背景减法测量移动车速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21235712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com