基于OpenCV的车辆检测论文 - 图文 下载本文

大连交通大学2015届本科生毕业论文

附 录

通过以下代码,在Microsoft Visual Studio 2008下运行,完成图像预处理、背景更新及前景运动车辆检测等工作。

#include #include #include #include #include \#include using namespace cv; using namespace std; #ifdef _DEBUG

#define new DEBUG_NEW #undef THIS_FILE

static char THIS_FILE[] = __FILE__; #endif

void main()

{//打开文件/////////////////////////////////////////////////// IplImage* pFrame = NULL; IplImage* pFrImg = NULL; IplImage* pBkImg = NULL; IplImage* pFrImg1 = NULL; CvMat* pFrameMat = NULL; CvMat* pFrMat = NULL; CvMat* pBkMat = NULL; CvMat* pFrMat1 = NULL;

CvMemStorage * storage = cvCreateMemStorage(0);//轮廓边缘提取时的参数 CvSeq * contour = 0;//轮廓边缘提取时的参数

int mode = CV_RETR_EXTERNAL;//轮廓边缘提取时的参数 //形态学处理时内核的大小

IplConvKernel* Element = cvCreateStructuringElementEx(13, 13, 1, 1, CV_SHAPE_RECT, NULL);

CvFont font1;//初始化字体格式 int linetype = CV_AA;

cvInitFont(&font1, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 8);

//用字符串时一定要把using namespace std;写在前面,否则不能用,下面是用于显示的字符串 string msg[10] = { \\int No = 0;//用于记录显示车辆 bool FindCar = false;

CvPoint pt1, pt2, pt3, pt4, pt5; pt1.x = 200;//(视频中左下点) pt1.y = 200;

21

大连交通大学2015届本科生毕业论文

pt2.x = 600;//(视频中右上点) pt2.y = 320;

CvRect bndRect = cvRect(0, 0, 0, 0);//用cvBoundingRect画出外接矩形时需要的矩形 int avgX = 0;//The midpoint X position of the rectangle surrounding the moving objects int avgY = 0;//The midpoint Y position of the rectangle surrounding the moving objects int avgX1 = 0;//用来合并相近的车辆 int avgY1 = 0;

CvCapture* pCapture = NULL;

int nFrmNum = 0;//表示图像的帧数 //创建窗口

cvNamedWindow(\

cvNamedWindow(\cvMoveWindow(\

cvMoveWindow(\

pCapture = cvCaptureFromFile(\毕业设计\\\\视频\\\\2.avi\pFrame = cvQueryFrame(pCapture); int widthT, heightT;

widthT = pFrame->width; heightT = pFrame->height;

IplImage* pFrameTemp = cvQueryFrame(pCapture); pFrame = cvCreateImage(cvGetSize(pFrameTemp), 8, 3);

cvCopy(pFrameTemp, pFrame);//逐帧读取视频,cvQueryFrame从摄像头或者文件中抓取并返回一帧

while (pFrameTemp = cvQueryFrame(pCapture)) {

cvCopy(pFrameTemp, pFrame);

nFrmNum++;//如果是第一帧,需要申请内存,并初始化 if (nFrmNum == 1) {

pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1); pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1); pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY); cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); cvConvert(pFrImg, pFrMat); cvConvert(pFrImg, pBkMat); }

else if (nFrmNum >=2)//从第三帧开始,根据完全目标档案来新增或删除运动车辆档案。 {

cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);

cvConvert(pFrImg, pFrameMat);//高斯滤波先,以平滑图像

22

大连交通大学2015届本科生毕业论文

cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);//在视频中设置并画出感兴趣的区域

cvRectangle(pFrame, pt1, pt2, CV_RGB(255, 0, 0), 2, 8, 0);//当前帧跟背景图相减,cvAbsDiff计算两个数组差的绝对值

cvAbsDiff(pFrameMat, pBkMat, pFrMat);

//二值化前景图,void cvThreshold( const CvArr* src, CvArr* dst, double threshold, //double max_value, int threshold_type );

cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);

//通过查找边界找出ROI矩形区域内的运动车辆,建立完全目标档案 cvDilate(pFrImg, pBkImg, Element, 1);

cvFindContours(pBkImg, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE); for (; contour != 0; contour = contour->h_next) {

bndRect = cvBoundingRect(contour, 0);

avgX = (bndRect.x + bndRect.x + bndRect.width) / 2; avgY = (bndRect.y + bndRect.y + bndRect.height) / 2; pt5.x = bndRect.x;//写字的左下角点 pt5.y = avgY;

if (avgX > 200 && avgX < 600 && avgY < 280 && avgY > 100) {

pt3.x = bndRect.x; pt3.y = bndRect.y;

pt4.x = bndRect.x + bndRect.width; pt4.y = bndRect.y + bndRect.height;

if (bndRect.width>40&&bndRect.height>45) //把长度小于某个阀值的干扰矩形去掉 {

cvRectangle(pFrame, pt3, pt4, CV_RGB(255, 0, 0), 1, 8, 0); } } }

//更新背景/////////////////////////////////////////////////// cvRunningAvg(pFrameMat, pBkMat, 0.005, 0); //将背景转化为图像格式,用以显示 cvConvert(pBkMat, pBkImg);

//显示图像//////////////////////////////////////////////////// cvShowImage(\

cvShowImage(\cvShowImage(\if (cvWaitKey(2) >= 0) break; }

}//while循环结束

cvReleaseStructuringElement(&Element);//删除结构元素

23

大连交通大学2015届本科生毕业论文

//销毁窗口

cvDestroyWindow(\

cvDestroyWindow(\cvDestroyWindow(\//释放图像和矩阵

cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat); cvReleaseMat(&pFrMat); cvReleaseMat(&pBkMat); }

24