OpenCV车辆追踪学习笔记
生活随笔
收集整理的這篇文章主要介紹了
OpenCV车辆追踪学习笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// FrameDiff.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//#include"stdafx.h"
#include<iostream>
using namespace std;
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace cv;const unsigned char FORE_GROUD = 127;
int thresh = 15;int main(int argc, char*argv[])
{VideoCapture video("D:/文檔/研究生/研二/交通行為參數(shù)提取/數(shù)據(jù)/交叉口視頻/test1.avi");//判斷如果video是否可以打開if (!video.isOpened())return -1;//用于保存當(dāng)前幀的圖片Mat currentBGRFrame;//用來保存上一幀和當(dāng)前幀的灰度圖片Mat previousGrayFrame;Mat currentGaryFrame;//用來保存幀差Mat frameDifference;//CV_16SC1//用來保存幀差的絕對(duì)值Mat absFrameDifferece;//用來顯示前景Mat segmentation;//顯示原視頻namedWindow("原視頻", 0);//顯示前景namedWindow("二值化", 0);cvResizeWindow("二值化", 500, 500);createTrackbar("閾值:", "二值化", &thresh, FORE_GROUD, NULL);//幀數(shù)int numberFrame = 0;//形態(tài)學(xué)處理用到的算子Mat morphologyKernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));for (;;){//讀取當(dāng)前幀video >> currentBGRFrame;//判斷當(dāng)前幀是否存在if (!currentBGRFrame.data)break;numberFrame++;if (numberFrame == 1){//顏色空間的轉(zhuǎn)換cvtColor(currentBGRFrame, currentGaryFrame, COLOR_BGR2GRAY);//保存當(dāng)前幀的灰度圖previousGrayFrame = currentGaryFrame.clone();imshow("原視頻", currentBGRFrame);continue;}else{//顏色空間的轉(zhuǎn)換cvtColor(currentBGRFrame, currentGaryFrame, COLOR_BGR2GRAY);//src1-src2subtract(currentGaryFrame, previousGrayFrame, frameDifference, Mat(), CV_16SC1);//取絕對(duì)值absFrameDifferece = abs(frameDifference);//位深的改變absFrameDifferece.convertTo(absFrameDifferece, CV_8UC1, 1, 0);//閾值處理threshold(absFrameDifferece, segmentation, double(thresh), double(FORE_GROUD), THRESH_BINARY);//中值濾波medianBlur(segmentation, segmentation, 3);//形態(tài)學(xué)處理(開閉運(yùn)算)morphologyEx(segmentation,segmentation,MORPH_OPEN,morphologyKernel,Point(-1,-1),1,BORDER_REPLICATE);morphologyEx(segmentation, segmentation, MORPH_CLOSE, morphologyKernel, Point(-1, -1), 2, BORDER_REPLICATE);//顯示二值化圖片imshow("二值化", segmentation);//找邊界vector< vector<Point> > contours; //contours定義為2維浮點(diǎn)向量vector<Vec4i> hierarchy; //定義的層級(jí)findContours(segmentation, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //CV_RETR_TREEvector< vector<Point> > contours_poly(contours.size());/*存儲(chǔ)運(yùn)動(dòng)物體*/vector<Rect> boundRect;boundRect.clear();//畫出運(yùn)動(dòng)物體//對(duì)視頻中出現(xiàn)的運(yùn)動(dòng)物體,進(jìn)行初次的篩選for (int index = 0; index < contours.size(); index++){approxPolyDP(Mat(contours[index]), contours_poly[index], 3, true);Rect rect = boundingRect(Mat(contours_poly[index]));if (rect.area() > 400)rectangle(currentBGRFrame, rect, Scalar(0, 255, 255), 2);}//顯示原視頻namedWindow("原視頻", 1); imshow("原視頻", currentBGRFrame);//保存當(dāng)前幀的灰度圖previousGrayFrame = currentGaryFrame.clone();}if (waitKey(33) == 'q')break;}return 0;
}
?
總結(jié)
以上是生活随笔為你收集整理的OpenCV车辆追踪学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现斗地主
- 下一篇: 克朗代克正在连接服务器,克朗代克