转载请注明出处: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);
}
颜色检测
颜色检测最初用的是
Thou-Ho (Chao-Ho) Chen, Ping-Hsueh Wu, and Yung-Chuen Chiou 于2004年在ICIP发表的文章《An Early Fire-Detection Method Based on Image Processing》中建立的颜色模型:
其中R、G、B为RGB模型中的颜色分量S为HSI颜色模型中的饱和度;Rt为R分量的阈值经试验得到可设定在55~56之间;St为饱和度的阈值经试验得到可设定在115~135之间。虽然简单,确很有效。之后自己又增加了些亮度之类的信息,并调整了阈值。
//论文《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;
}
}
}
经过两部检测后的备选像素,大于一定值则判定为火,标框并报警,效果如下: