转载请注明出处:http://blog.csdn.net/xiaowei_cqu/article/details/7522467
前段时间做了一个火灾检测的小程序,因为时间紧,实现的算法也简单。只用了两步处理:运动检测和颜色检测。日后还会再改进~
//背景相减 void FireDetector:: CheckFireMove(IplImage *pImgFrame/*, IplImage* pInitBackground, IplImage *pImgMotion*/) { int thresh_low = 80;//30 cvCvtColor(pImgFrame, pImgMotion, CV_BGR2GRAY); cvConvert(pImgMotion, pMatFrame); cvConvert(pImgMotion, pMatProcessed); cvConvert(pImgBackground, pMatBackground); cvSmooth(pMatFrame, pMatFrame, CV_GAUSSIAN, 3, 0, 0); //计算两幅图的差的绝对值 cvAbsDiff(pMatFrame, pMatBackground, pMatProcessed); //cvConvert(pMatProcessed,pImgProcessed); //cvThresholdBidirection(pImgProcessed,thresh_low); //对单通道数组应用固定阈值操作,此处得到二值图像 cvThreshold(pMatProcessed, pImgMotion, thresh_low, 255.0, CV_THRESH_BINARY); //使用 Gaussian 金字塔分解对输入图像向下采样,再向上采样 cvPyrDown(pImgMotion,pyrImage,CV_GAUSSIAN_5x5); cvPyrUp(pyrImage,pImgMotion,CV_GAUSSIAN_5x5); //腐蚀和膨胀操作 cvErode(pImgMotion, pImgMotion, 0, 1); cvDilate(pImgMotion, pImgMotion, 0, 1); //使用当前帧0.3的比例对背景图像更新 int pUpdate=0.3;//0.0003 cvRunningAvg(pMatFrame, pMatBackground, pUpdate, 0); cvConvert(pMatBackground, pImgBackground); }
//论文《An Early Fire-Detection Method Based on Image Processing》中的颜色模型 void FireDetector::CheckFireColor2(IplImage *RGBimg) { int RedThreshold=115; //115~135 int SaturationThreshold=45; //55~65 for(int j = 0;j < RGBimg->height;j++) { for (int i = 0;i < RGBimg->widthStep;i+=3) { uchar B = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i]; uchar G = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i+1]; uchar R = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i+2]; uchar maxv=max(max(R,G),B); uchar minv=min(min(R,G),B); double S = (1 - 3.0*minv/(R+G+B)); //火焰像素满足颜色特征 //(1)R>RT (2)R>=G>=B (3)S>=( (255-R) * ST / RT ) if(R>RedThreshold&&R>=G&&G>=B&&S>0.20/*&&/*S>(255-R)/20&&S>=((255-R)*SaturationThreshold/RedThreshold)*/) pImgFire->imageData[i/3+pImgFire->widthStep*j] = 255; else pImgFire->imageData[i/3+pImgFire->widthStep*j] = 0; } } }