// pedetriandetect.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <cv.h> #include <highgui.h> #include "cxcore.h" #include <stdio.h> int otsuThreshold(IplImage *frame); int selectVideo = 1; #define GrayScale 256 int main( int argc, char** argv ) { //声明IplImage指针 IplImage* pFrame = NULL; IplImage* pFrImg = NULL; IplImage* pBkImg = NULL; IplImage* pImg =NULL; CvMat* pFrameMat = NULL; CvMat* pFrMat = NULL; CvMat* pBkMat = NULL; CvMemStorage * storage = NULL; CvSeq * contour = 0; int mode = CV_RETR_EXTERNAL; CvCapture* pCapture = NULL; int nFrmNum = 0; //创建窗口 cvNamedWindow("video", 1); cvNamedWindow("background",1); cvNamedWindow("foreground",1); cvNamedWindow("contour",1); //使窗口有序排列 cvMoveWindow("video", 30, 0); cvMoveWindow("background", 382, 0); cvMoveWindow("foreground", 734, 0); cvMoveWindow("contour",720,288); //打开摄像头 if (selectVideo == 0) if( !(pCapture = cvCaptureFromCAM(-1))) { fprintf(stderr, "Can not open camera./n"); return -2; } //打开视频文件 if(selectVideo == 1) if( !(pCapture = cvCaptureFromFile("D://红外//new//20110414-191839.mpg"))) { fprintf(stderr, "Can not open video file %s/n", argv[1]); return -2; } //逐帧读取视频 while(pFrame = cvQueryFrame( pCapture )) { 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); pImg = 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); storage = cvCreateMemStorage(0); //转化成单通道图像再处理 cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY); cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); cvConvert(pFrImg, pFrMat); cvConvert(pFrImg, pBkMat); } else { cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); //高斯滤波先,以平滑图像 //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0); //当前帧跟背景图相减 cvAbsDiff(pFrameMat, pBkMat, pFrMat); cvDilate(pFrMat, pFrMat, 0, 5); cvGetImage(pFrMat,pImg); int thredhold = otsuThreshold(pImg)-30; printf("otsu : %d /n",thredhold); //二值化前景图 cvThreshold(pFrMat, pFrImg,70, 255.0, CV_THRESH_BINARY); cvFindContours( pFrImg, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); CvRect rect; double imgArea = pFrImg->width * pFrImg->height; double contArea =0.0; float contLenth =0; CvPoint pt1,pt2; CvPoint pt3,pt4; char x_char =cvWaitKey(10); static float Hx_create ; static float Hy_create ; static float Lx_create ; static float Ly_create ; if( x_char =='a') Lx_create -=0.01; if( x_char == 'd') Lx_create +=0.01; if( x_char == 'w') Hy_create -=0.01; if( x_char == 's') Hy_create +=0.01; if( x_char == 'r') Ly_create +=0.01; if( x_char == 't') Ly_create -=0.01; if( x_char == 'f') Hx_create +=0.01 ; if( x_char == 'g') Hx_create -=0.01 ; pt3.x = pFrame->width *(0.15+Lx_create-Hx_create) ; pt4.x =pFrame->width * (0.85+Lx_create+Hx_create); pt3.y = pFrame->height *( 0.5+Hy_create-Ly_create) ; pt4.y = pFrame->height * (0.85+Hy_create+Ly_create) ;//*/ cvRectangle(pFrame,pt3,pt4,CV_RGB(255,0,255),1,8,0); for(;contour;contour = contour->h_next) { CvRect rect; rect = cvBoundingRect(contour,0); pt1.x = rect.x; pt1.y = rect.y; pt2.x = rect.width +rect.x; pt2.y = rect.height + rect.y; contArea = fabs(cvContourArea(contour,CV_WHOLE_SEQ)); contLenth = cvArcLength(contour,CV_WHOLE_SEQ,-1); printf("contArea:%f/n",contArea); printf("contLength:%f/n",contLenth); if(contArea >= 30 && contLenth>= 20 &&rect.width>2 &&rect.width <50 &&( rect.height -rect.width) > 0) { //cvDrawContours (pFrame, contour, CV_RGB (255,255, 0), CV_RGB (0, 255, 0), //1, 1, CV_AA, cvPoint (0, 0)); cvRectangle(pFrame,pt1,pt2,CV_RGB(255,0,0),1,8,0); } } // cvSmooth(pFrImg, pFrImg, CV_MEDIAN, 3, 0, 0);//CV_GAUSSIAN //进行形态学滤波,去掉噪音 //cvErode(pFrImg, pFrImg, 0, 1); //cvDilate(pFrImg, pFrImg, 0, 2); //更新背景 cvRunningAvg(pFrameMat, pBkMat, 0.0005, 0); //将背景转化为图像格式,用以显示 cvConvert(pBkMat, pBkImg); //显示图像 cvShowImage("video", pFrame); cvShowImage("background", pBkImg); cvShowImage("foreground", pFrImg); //如果有按键事件,则跳出循环 //此等待也为cvShowImage函数提供时间完成显示 //等待时间可以根据CPU速度调整 char c = cvWaitKey(30); if( c==27 ) break; } } //销毁窗口 cvDestroyWindow("video"); cvDestroyWindow("background"); cvDestroyWindow("foreground"); //释放图像和矩阵 cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat); cvReleaseMat(&pFrMat); cvReleaseMat(&pBkMat); cvReleaseCapture(&pCapture); return 0; } int otsuThreshold(IplImage *frame) { int width = frame->width; int height = frame->height; int pixelCount[GrayScale]; float pixelPro[GrayScale]; int i, j, pixelSum = width * height, threshold = 0; uchar* data = (uchar*)frame->imageData; for(i = 0; i < GrayScale; i++) { pixelCount[i] = 0; pixelPro[i] = 0; } //统计灰度级中每个像素在整幅图像中的个数 for(i = 0; i < height; i++) { for(j = 0;j < width;j++) { pixelCount[(int)data[i * width + j]]++; } } //计算每个像素在整幅图像中的比例 for(i = 0; i < GrayScale; i++) { pixelPro[i] = (float)pixelCount[i] / pixelSum; } //遍历灰度级[0,255] float w0, w1, u0tmp, u1tmp, u0, u1, u, deltaTmp, deltaMax = 0; for(i = 0; i < GrayScale; i++) { w0 = w1 = u0tmp = u1tmp = u0 = u1 = u = deltaTmp = 0; for(j = 0; j < GrayScale; j++) { if(j <= i) //背景部分 { w0 += pixelPro[j]; u0tmp += j * pixelPro[j]; } else //前景部分 { w1 += pixelPro[j]; u1tmp += j * pixelPro[j]; } } u0 = u0tmp / w0; u1 = u1tmp / w1; u = u0tmp + u1tmp; deltaTmp = w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2); if(deltaTmp > deltaMax) { deltaMax = deltaTmp; threshold = i; } } return threshold; }