1. 程式人生 > >【OpenCV】森林火災檢測-1

【OpenCV】森林火災檢測-1

運動檢測

其實就是檢測背景,對背景建模然後提取前景中運動的物體作為候選火災樣本。嘗試了兩種簡單的背景演算法:高斯背景建模和背景相減,還是背景相減的效果較好。以下是程式碼:
  1. //背景相減
  2. void FireDetector:: CheckFireMove(IplImage *pImgFrame/*, IplImage* pInitBackground, IplImage *pImgMotion*/)  
  3. {  
  4.     int thresh_low = 80;//30
  5.     cvCvtColor(pImgFrame, pImgMotion, CV_BGR2GRAY);  
  6.     cvConvert(pImgMotion, pMatFrame);  
  7.     cvConvert(pImgMotion, pMatProcessed);  
  8.     cvConvert(pImgBackground, pMatBackground);  
  9.     cvSmooth(pMatFrame, pMatFrame, CV_GAUSSIAN, 3, 0, 0);  
  10.     //計算兩幅圖的差的絕對值
  11.     cvAbsDiff(pMatFrame, pMatBackground, pMatProcessed);  
  12.     //cvConvert(pMatProcessed,pImgProcessed);
  13.     //cvThresholdBidirection(pImgProcessed,thresh_low);
  14.     //對單通道陣列應用固定閾值操作,此處得到二值影象
  15.     cvThreshold(pMatProcessed, pImgMotion, thresh_low, 255.0, CV_THRESH_BINARY);  
  16.     //使用 Gaussian 金字塔分解對輸入影象向下取樣,再向上取樣
  17.     cvPyrDown(pImgMotion,pyrImage,CV_GAUSSIAN_5x5);  
  18.     cvPyrUp(pyrImage,pImgMotion,CV_GAUSSIAN_5x5);  
  19.     //腐蝕和膨脹操作
  20.     cvErode(pImgMotion, pImgMotion, 0, 1);  
  21.     cvDilate(pImgMotion, pImgMotion, 0, 1);   
  22.     //使用當前幀0.3的比例對背景影象更新
  23.     int pUpdate=0.3;//0.0003
  24.     cvRunningAvg(pMatFrame, pMatBackground, pUpdate, 0);                      
  25.     cvConvert(pMatBackground, pImgBackground);  
  26. }  
顏色檢測 顏色檢測最初用的是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之間。雖然簡單,確很有效。之後自己又增加了些亮度之類的資訊,並調整了閾值。
  1. //論文《An Early Fire-Detection Method Based on Image Processing》中的顏色模型
  2. void FireDetector::CheckFireColor2(IplImage *RGBimg)  
  3. {  
  4.     int RedThreshold=115;  //115~135 
  5.     int SaturationThreshold=45;  //55~65
  6.     for(int j = 0;j < RGBimg->height;j++)  
  7.     {  
  8.         for (int i = 0;i < RGBimg->widthStep;i+=3)  
  9.         {  
  10.             uchar B = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i];  
  11.             uchar G = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i+1];  
  12.             uchar R = (uchar)RGBimg->imageData[j*RGBimg->widthStep+i+2];  
  13.             uchar maxv=max(max(R,G),B);   
  14.             uchar minv=min(min(R,G),B);   
  15.             double S = (1 - 3.0*minv/(R+G+B));  
  16.             //火焰畫素滿足顏色特徵
  17.             //(1)R>RT   (2)R>=G>=B   (3)S>=( (255-R) * ST / RT )
  18.             if(R>RedThreshold&&R>=G&&G>=B&&S>0.20/*&&/*S>(255-R)/20&&S>=((255-R)*SaturationThreshold/RedThreshold)*/)  
  19.                 pImgFire->imageData[i/3+pImgFire->widthStep*j] = 255;  
  20.             else
  21.                 pImgFire->imageData[i/3+pImgFire->widthStep*j] = 0;  
  22.         }  
  23.     }  
  24. }  

經過兩部檢測後的備選畫素,大於一定值則判定為火,標框並報警,效果如下: