/****************************************************
* smoke detection
*
******************************************************/
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "windows.h"
//#include "swavelet.h"
#include "connected_component.h"
#include "swavelet.h"
#include "windows.h"
//#define WSIZE 2
#define CVCONTOUR_APPROX_LEVEL 2
int main(int argc, char** argv)
{
CvCapture* pCapture = NULL;
cvNamedWindow("source", 1);
//cvNamedWindow("spatial_wavelet_detection",1);
cvNamedWindow("move_detection", 1);
cvNamedWindow("color_detection", 1);
cvNamedWindow("result", 1);
cvMoveWindow("source", 30, 0);
//cvMoveWindow("spatial_wavelet_detection",360,0);
cvMoveWindow("move_detection", 690, 0);
cvMoveWindow("color_detection", 1020, 0);
cvMoveWindow("result", 30, 330);
if (argc>2)
{
fprintf(stderr, "Usage:bkgrd [video_file_name]\n");
return -1;
}
if (argc == 1)
{
if (!(pCapture = cvCaptureFromCAM(-1)))
{
fprintf(stderr, "Can not open camera.\n");
return -2;
}
}
if (argc == 2)
{
if (!(pCapture = cvCaptureFromFile(argv[1])))
{
fprintf(stderr, "Can not open video file %s\n", argv[1]);
return -2;
}
}
IplImage* pFrame = NULL;
IplImage* pFrImg = NULL;
IplImage* pBkImg = NULL;
IplImage* pResult = NULL;
IplImage* pImg = NULL;
IplImage* pColor = NULL;
CvMat* pFrameMat = NULL;
CvMat* pFrMat = NULL;
CvMat* pBkMat = NULL;
CvMat* pMatChannels[4] = { NULL,NULL,NULL,NULL };
CvMat* pFrameMatPre1 = NULL;
CvMat* pFrameMatPre2 = NULL;
CvMat* pFrameMatCur = NULL;
CvMat* framediff1 = NULL;
CvMat* framediff2 = NULL;
CvMat* pWaveletCur = NULL;
CvMat* pWaveletBk = NULL;
CvMat* threshold = NULL;
CvMat* color_detection = NULL;
//保存视频文件
//CvVideoWriter* writerin=NULL;
//CvVideoWriter* writerout=NULL;
//int isColor=1;
//int fps=20;
int nFrmNum = 0;
int frame_height;
int frame_width;
int frame_channel;
int k;
while (pFrame = cvQueryFrame(pCapture))
{
nFrmNum++;
frame_height = pFrame->height;
frame_width = pFrame->width;
frame_channel = pFrame->nChannels;
if (nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(frame_width, frame_height), IPL_DEPTH_8U, 1);
pFrImg = cvCreateImage(cvSize(frame_width, frame_height), IPL_DEPTH_8U, 1);
pResult = cvCreateImage(cvSize(frame_width, frame_height), IPL_DEPTH_8U, 3);
pBkMat = cvCreateMat(frame_height, frame_width, CV_32FC1);
pFrMat = cvCreateMat(frame_height, frame_width, CV_32FC1);
pFrameMat = cvCreateMat(frame_height, frame_width, CV_32FC3);
pFrameMatPre1 = cvCreateMat(frame_height, frame_width, CV_32FC1);
pFrameMatPre2 = cvCreateMat(frame_height, frame_width, CV_32FC1);
pFrameMatCur = cvCreateMat(frame_height, frame_width, CV_32FC1);
framediff1 = cvCreateMat(frame_height, frame_width, CV_32FC1);
framediff2 = cvCreateMat(frame_height, frame_width, CV_32FC1);
pWaveletCur = cvCreateMat(frame_height, frame_width, CV_32FC1);
pWaveletBk = cvCreateMat(frame_height, frame_width, CV_32FC1);
threshold = cvCreateMat(frame_height, frame_width, CV_32FC1);
color_detection = cvCreateMat(frame_height, frame_width, CV_32FC1);
pImg = cvCreateImage(cvSize(frame_width, frame_height), IPL_DEPTH_8U, 1);
pColor = cvCreateImage(cvSize(frame_width, frame_height), IPL_DEPTH_8U, 1);
for (k = 0; k<frame_channel; k++)
{
pMatChannels[k] = cvCreateMat(frame_height, frame_width, CV_32FC1);
}
//writerin=cvCreateVideoWriter("source.avi",-1,fps,cvSize(frame_width,frame_height),isColor);
//writerout=cvCreateVideoWriter("result.avi",-1,fps,cvSize(frame_width,frame_height),isColor);
//转化成单通道图像再处理
cvConvertScale(pFrame, pFrameMat, 1, 0);
cvSplit(pFrameMat, pMatChannels[0], pMatChannels[1], pMatChannels[2], pMatChannels[3]);
cvAddWeighted(pMatChannels[0], 0.114, pMatChannels[1], 0.587, 0, pFrameMatPre1);
cvAddWeighted(pMatChannels[2], 0.299, pFrameMatPre1, 1, 0, pFrameMatPre1);
cvConvert(pFrameMatPre1, pBkMat);
cvConvert(pFrameMatPre1, pFrameMatPre2);
cvZero(pFrameMatCur);
cvZero(pFrameMatPre2);
cvSet(threshold, cvScalar(5), NULL);
}
else
{
double t = (double)cvGetTickCount(); //计算每帧时间
//const threshold_detection = 20;
//const double T1 = 3.0;
//const double T2 = 3.0;
const double alpha = 0.02;
const int threshold_wavelet = 5;
const int threshold_color = 7;
int i, j;
cvConvertScale(pFrame, pFrameMat, 1, 0);
cvConvertScale(pFrame, pResult, 1, 0);
cvSplit(pFrameMat, pMatChannels[0], pMatChannels[1], pMatChannels[2], pMatChannels[3]);
cvAddWeighted(pMatChannels[0], 0.114, pMatChannels[1], 0.587, 0, pFrameMatCur);
cvAddWeighted(pMatChannels[2], 0.299, pFrameMatCur, 1, 0, pFrameMatCur);
//当前帧跟背景图相减
cvAbsDiff(pFrameMatCur, pFrameMatPre1, framediff1);
cvAbsDiff(pFrameMatCur, pFrameMatPre2, framediff2);
//cvAbsDiff(pFrameMatCur, pBkMat, pFrMat);
//二值化前景图
for (i = 0; i<frame_height; i++)
for (j = 0; j<frame_width; j++)
{
if ((cvmGet(framediff1, i, j) > cvmGet(threshold, i, j)) && (cvmGet(framediff2, i, j) > cvmGet(threshold, i, j)))
cvmSet(pFrMat, i, j, 255.0);
else
cvmSet(pFrMat, i, j, 0.0);
if (cvmGet(pFrMat, i, j))
{
double differ = abs(cvmGet(pFrameMatCur, i, j) - cvmGet(pBkMat, i, j));
if (differ > cvmGet(threshold, i, j))
cvmSet(pFrMat, i, j, 255);
else
cvmSet(pFrMat, i, j, 0);
}
}
//进行形态学滤波 ,连通域分析
//cvErode(pFrMat, pFrMat, 0, 1);
//cvDilate(pFrMat, pFrMat, 0, 1);
//cvMorphologyEx( pFrMat, pFrMat, 0, 0, CV_MOP_OPEN, 1 );
//cvMorphologyEx( pFrMat, pFrMat, 0, 0, CV_MOP_CLOSE, 2 );
//cvConvert(pFrMat, pFrImg);
//find_connected_components(pFrImg, 1, 4,NULL,NULL,NULL);
//cvConvert(pFrImg,pFrMat);
/*
// 空域小波分析
WaveletAnalysis(pFrameMatCur,pWaveletCur,1);
WaveletAnalysis(pBkMat,pWaveletBk,1);
float* energycur = new float[frame_width*frame_height/WSIZE/WSIZE];
float* energypre = new float[frame_width*frame_height/WSIZE/WSIZE];
int num = 0;
for(i=0;i<frame_height;i+=WSIZE)
for(j=0;j<frame_width;j+=WSIZE)
{
int ii,jj;
energycur[num] = 0;
energypre[num] = 0;
for(ii=i;ii<i+WSIZE;ii++)
for(jj=j;jj<j+WSIZE;jj++)
{
energycur[num] += cvmGet(pWaveletCur,ii,jj);
energypre[num] += cvmGet(pWaveletBk,ii,jj);
}
//if((energycur[num]>0.3*energypre[num])&&(energycur[num]<0.7*energypre[num]))
//{
if((energypre[num]-energycur[num])>=threshold_wavelet)
{
for(ii=i;ii<i+WSIZE;ii++)
for(jj=j;jj<j+WSIZE;jj++)
{
cvmSet(pWaveletCur,ii,jj,255);
}
}
else
{
for(ii=i;ii<i+WSIZE;ii++)
for(jj=j;jj<j+WSIZE;jj++)
{
cvmSet(pWaveletCur,ii,jj,0);
}
}
//}
num++;
}
delete []energycur;
delete []energypre;
//进行形态学滤波
//cvErode(pWaveletCur, pWaveletCur, 0, 1);
//cvDilate(pWaveletCur, pWaveletCur, 0, 1);
//cvMorphologyEx( pWaveletCur, pWaveletCur, 0, 0, CV_MOP_OPEN, 1);
//cvMorphologyEx( pWaveletCur, pWaveletCur, 0, 0, CV_MOP_CLOSE, 1 );
cvConvert(pWaveletCur, pImg);
//find_connected_components(pImg, 1, 4,NULL,NULL,NULL);
//cvConvert(pImg,pWaveletCur);
*/
// 时域小波分析
//颜色分析
for (i = 0; i<frame_height; i++)
for (j = 0; j<frame_width; j++)
{
if (cvmGet(pFrMat, i, j) == 255)
{
CvScalar color;
color = cvGet2D(pFrameMat, i, j);
float minI = (color.val[0]<color.val[1]) ? color.val[0] : color.val[1];
minI = (minI<color.val[2]) ? minI : color.val[2];
float sumI = color.val[0] + color.val[1] + color.val[2];
float s = 1 - 3 * minI / sumI;
if ((color.val[2]>color.
评论0