3月 202013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

先上代码

参考

http://www.opencv.org.cn/index.php/%E8%AF%BB%E8%A7%86%E9%A2%91%E6%96%87%E4%BB%B6%E5%92%8C%E8%BF%90%E5%8A%A8%E7%89%A9%E4%BD%93%E6%A3%80%E6%B5%8B

运动检测的原理就是,首先获取一张背景图,然后不断的读入每一帧动画,然后和背景图进行相减,得到的图即为可以检测运动的图像,然后还需要不断的更新背景图。

public class test {        
    public static void main(String args[]) throws Exception{ 
//声明IplImage指针
IplImage pFrame = null; 
IplImage pFrImg = null;
IplImage pBkImg = null;

CvMat pFrameMat = null;
CvMat pFrMat = null;
CvMat pBkMat = null;

CvCapture pCapture = null;

int nFrmNum = 0;

//创建窗口
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
//使窗口有序排列
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0);
//打开视频文件
FrameGrabber grabber = FrameGrabber.createDefault(0);
	grabber.start();
	pFrame = grabber.grab();

//逐帧读取视频
while(pFrame!=null)
{
    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
	{
	  cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
	  cvConvert(pFrImg, pFrameMat);
	  //高斯滤波先,以平滑图像
	  //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);

	  //当前帧跟背景图相减
	  cvAbsDiff(pFrameMat, pBkMat, pFrMat);

	  //二值化前景图
	  cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);

	  //进行形态学滤波,去掉噪音  
	  //cvErode(pFrImg, pFrImg, 0, 1);
	  //cvDilate(pFrImg, pFrImg, 0, 1);

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

	  //显示图像
	  cvShowImage("video", pFrame);
	  cvShowImage("background", pBkImg);
	  cvShowImage("foreground", pFrImg);

	  //如果有按键事件,则跳出循环
	  //此等待也为cvShowImage函数提供时间完成显示
	  //等待时间可以根据CPU速度调整
	  if( cvWaitKey(2) >= 0 )
	    break;

	}
    try {
		pFrame = grabber.grab();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    }
		//销毁窗口
		cvDestroyWindow("video");
		cvDestroyWindow("background");
		cvDestroyWindow("foreground");

		//释放图像和矩阵
		cvReleaseImage(pFrImg);
		cvReleaseImage(pBkImg);

		cvReleaseMat(pFrameMat);
		cvReleaseMat(pFrMat);
		cvReleaseMat(pBkMat);

		cvReleaseCapture(pCapture);

    }
 }

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)