【OpenCV】森林火災檢測-1
阿新 • • 發佈:2019-01-22
運動檢測
其實就是檢測背景,對背景建模然後提取前景中運動的物體作為候選火災樣本。嘗試了兩種簡單的背景演算法:高斯背景建模和背景相減,還是背景相減的效果較好。以下是程式碼:- //背景相減
- 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);
- }
其中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;
- }
- }
- }
經過兩部檢測後的備選畫素,大於一定值則判定為火,標框並報警,效果如下: