1
我的OpenCV和C++寫了一個短程序來跟蹤與攝像頭的對象。網絡攝像頭的配方很快沒有滯後,但在週末下班前,我記錄了一個典型的序列,作爲測試模板,直到我工作到星期一。代碼中的這一點和相應的變化使得視頻以非常慢的速度播放。這裏是代碼,打開「Test.avi」,約20秒長,而不是運行網絡攝像頭的恆定流:慢速視頻,使用OpenCV的
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
Mat drawBoundingBoxes (Mat canvasImage, vector<vector<Point>> contours);
int main(int argc, char** argv[])
{
Mat frame;
Mat back;
Mat fGround;
BackgroundSubtractorMOG2 bGround;
bGround.nmixtures = 3;
//bGround.nShadowDetection = 0;
bGround.fTau = .5;
VideoCapture cap;
cap.open("Test.avi");
if (!cap.isOpened())
{
cout << "Can't open video" << endl;
return -1;
}
vector<vector<Point>> contours;
namedWindow("video", CV_WINDOW_AUTOSIZE);
while (true)
{
static int count = 1;
cap >> frame;
if (frame.empty())
break;
bGround.operator()(frame, fGround);
bGround.getBackgroundImage(back);
erode(fGround, fGround, Mat(), Point(-1,-1), 2, BORDER_DEFAULT);
dilate(fGround, fGround, Mat(), Point(-1,-1), 10, BORDER_DEFAULT);
if (count > 50)
{
findContours(fGround, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(frame, contours, -1, Scalar(239,255,0), 2);
drawBoundingBoxes(frame, contours);
}
imshow("video", frame);
if(waitKey(30) >= 0)
break;
count++;
}
return 0;
}
Mat drawBoundingBoxes (Mat canvasImage, vector<vector<Point>> contours)
{
vector<Rect> boundRect(contours.size());
for (int i=0; i<contours.size(); i++)
{
boundRect[i] = boundingRect(contours[i]);
rectangle(canvasImage, boundRect[i], Scalar(153,0,76), 2, 8, 0);
}
return canvasImage;
}
任何想法?內存泄漏的地方?謝謝,
託尼
「在代碼中相應的變化」 - 這你們沒有找出任何影響。它的工作,你做了*某事*,現在它播放得太慢了。嗯... – 2013-02-16 04:27:22
對不起,我認爲任何有OpenCV經驗的人都會知道這個變化是非常非常小的。基本變化回去攝像頭飼料版本將刪除這些兩行: cap.open(「Test.avi」); namedWindow(「video」,CV_WINDOW_AUTOSIZE); 而更換: VideoCapture帽; 用: VideoCapture帽(0); 對不起,感謝您的回覆! – TonyRo 2013-02-16 04:38:24
新視頻中的圖像與您在直接使用網絡攝像頭時測試的圖像非常相似嗎?背景減法器或輪廓搜索階段是否有可能比平時花費更長的時間,因爲圖像更具有「挑戰性」。 – 2013-02-16 04:48:56