天天看點

OpenCV學習記錄之視訊中的火焰檢測識别

作者:coldplayplay

主要完成兩個視訊中火焰的檢測,主要結合RGB判據和HIS判據,設定合适的門檻值條件,檢測出火焰對應像素的區域,将原圖二值化,經過中值濾波以及數學形态學的膨脹運算等圖像處理,消除一些噪聲及離散點,連通一些遺漏的區域。基于OpenCV的開源庫,在VS2013平台上,實作了兩個視訊中火焰的檢測。

利用OpenCV有強大的圖像處理庫,直接将圖像分離為RGB三通道,設定條件限制,找到火焰的像素位置,将原圖處理成二值圖像。對于火焰檢測,本文結合RGB判據和HIS判據,分割出火焰的區域。一般用于人眼觀看的顔色模型是RGB模型,對于火焰而言,紅色分量(R)和綠色分量(G)會很大,并且綠色分量(G)會大于藍色分量(B)。HIS顔色模型分别用H(色度)S(飽和度)I(亮度)描述顔色特性,與人們感受顔色的方式緊密相連。考慮到單一顔色模型的判據準确性不夠高,在RGB判據基礎上,添加HIS限制條件。具體條件[1]為:

OpenCV學習記錄之視訊中的火焰檢測識别

其中,Rt是紅色分量門檻值,St是飽和度門檻值,火焰像素主要取決于紅色分量(R)的色度和飽和度。若滿足式(1),則判斷該位置為火焰像素,顯示為白色,否則顯示為黑色。判據中門檻值的選擇對于火焰檢測是至關重要的,一般靠經驗設定,為了擷取火焰識别最好的效果,設定兩個滑動條,改變門檻值Rt和St的大小,選取最合适的值。

由于(1)中隻需要用到HIS中的S分量,是以不需要用到顔色模型轉換函數,直接計算S分量即可。

擷取二值圖像後,需要對其預處理,找到遺漏的點,剔除異常的點。由于存在噪聲及離散點,對圖像進行平滑濾波,本文采用的是中值濾波,中值濾波是典型的非線性濾波,用像素點鄰域灰階值的中值來代替該像素點的灰階值,非常利于消除一些誤判斷為火焰的像素點。

由于部分火焰的顔色不是介于紅黃之間,無法識别,需要實作區域的連通,是以對二值圖像進行數學形态學操作。形态學是一種強大的圖像處理工具,它可以實作圖像去噪、圖像分割等功能,最基本的形态學操作有兩種,分别是膨脹與腐蝕。它們可以衍生出很多強大的形态學算法,實作我們想要的功能。采用形态學處理的最基礎的膨脹操作,作用于火焰的二值圖像中。

編寫CheckColor函數,将以上3個功能實作。

為了表示出視訊中火焰的區域,在預處理過後,将火焰輪廓用矩形框标記,編寫了畫矩形框的函數DrawFire,其中使用了OpenCV的尋找輪廓的函數findContours,由于作業中test2的火焰位置是分散在不同地方的,是以對整張圖像進行區域的劃分,分别用不同矩形标記不同區域出現的火焰。

基于OpenCV的庫,在VS2013上實作算法,由于視訊中的火焰檢測是實時動态的,下面截取幾幀畫面用于展示實驗結果:

OpenCV學習記錄之視訊中的火焰檢測識别

本文采用RGB判據和HIS判據結合的方法,按照經驗法和不斷地調試,選擇合适的門檻值,基于OpenCV在VS2013上實作算法,從test1實驗結果可以看出,在背景比較單調且與火焰差别較大時,效果良好,幾乎沒有任何噪聲對其造成幹擾。從test2實驗結果可以看出,當背景複雜或與火焰顔色比較相似時,會不時出現噪聲和誤判,需要進一步提高算法。

列出處理test2視訊的具體代碼:

1. #include<opencv2/opencv.hpp>  
2. #include<cv.h>  
3. 
4. using namespace cv;  
5. int redThre =49; // 115~135    
6. int saturationTh = 7; //55~65    
7. Mat CheckColor(Mat &inImg);  
8. void DrawFire(Mat &inputImg, Mat foreImg);  
9. 
10. int main()  
11. {  
12.     VideoCapture capture("test2.avi");  
13. 
14.     while (1)  
15.     {  
16.         Mat frame;  
17. 
18.         capture >> frame;  
19.         if (frame.empty())  
20.             break;            
21.         namedWindow("Control", CV_WINDOW_AUTOSIZE);  
22.         cvCreateTrackbar("redThre", "Control", &redThre, 255);   
23.         cvCreateTrackbar("saturationTh", "Control", &saturationTh, 255);   
24.         CheckColor(frame);  
25.         waitKey(1);       
26.     }     
27.     return 0;  
28. }  
29. 
30. //The Color Check is According to "An Early Fire-Detection Method Based on Image Processing"    
31. //The Author is:Thou-Ho (Chao-Ho) Chen, Ping-Hsueh Wu, and Yung-Chuen Chiou    
32. 
33. Mat CheckColor(Mat &inImg)  
34. {  
35.     Mat fireImg;  
36.     fireImg.create(inImg.size(), CV_8UC1);    
37.     Mat multiRGB[3];  
38.     int a = inImg.channels();  
39.     split(inImg, multiRGB); //将圖檔拆分成R,G,B,三通道的顔色    
40. 
41.     for (int i = 0; i < inImg.rows; i++)  
42.     {  
43.         for (int j = 0; j < inImg.cols; j++)  
44.         {  
45.             float B, G, R;  
46.             B = multiRGB[0].at<uchar>(i, j); //每個像素的R,G,B值,動态位址計算法    
47.             G = multiRGB[1].at<uchar>(i, j);  
48.             R = multiRGB[2].at<uchar>(i, j);  
49. 
50.             float maxValue = max(max(B, G), R);  
51.             float minValue = min(min(B, G), R);  
52.             //與HSI中S分量的計算公式  
53.             double S = (1 - 3.0*minValue / (R + G + B));//  
54. 
55.             //R > RT  R>=G>=B  S>=((255-R)*ST/RT)    
56.             if (R > redThre &&R >= G && G>= B && S >((255 - R) * saturationTh / redThre))  
57.             {  
58.                 fireImg.at<uchar>(i, j) = 255;  
59.             }  
60.             else  
61.             {  
62.                 fireImg.at<uchar>(i, j) = 0;  
63.             }  
64.         }  
65.     }  
66. 
67.     //erode(fireImg, fireImg, Mat(3, 3, CV_8UC1));  
68.     //GaussianBlur(fireImg, fireImg, Size(5, 5), 0, 0);  
69.     medianBlur(fireImg, fireImg, 5);  
70.     dilate(fireImg, fireImg, Mat(5, 5, CV_8UC1));         
71.     imshow("Binary", fireImg);  
72.     DrawFire(inImg, fireImg);  
73.     return fireImg;  
74. }  
75. 
76. void DrawFire(Mat &inputImg, Mat foreImg)  
77. {  
78.     vector<vector<Point>> contours_set;//儲存輪廓提取後的點集及拓撲關系    
79.     findContours(foreImg, contours_set, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);      
80.     Point point1;  
81.     Point point2;     
82.     float a = 0.4, b = 0.75;  
83.     float xmin1 = a*inputImg.cols, ymin1 = inputImg.rows, xmax1 = 0, ymax1 = 0;  
84.     float xmin2 = b*inputImg.cols, ymin2 = inputImg.rows, xmax2 = a*inputImg.cols, ymax2 = 0;  
85.     float xmin3 = inputImg.cols, ymin3 = inputImg.rows, xmax3 = b*inputImg.cols, ymax3 = 0;  
86.     Rect finalRect1;  
87.     Rect finalRect2;  
88.     Rect finalRect3;      
89.     vector<vector<Point> >::iterator iter = contours_set.begin();  
90.     for (; iter != contours_set.end();)  
91.     {  
92.         Rect rect = boundingRect(*iter);  
93.         float radius;  
94.         Point2f center;  
95.         minEnclosingCircle(*iter, center, radius);  
96. 
97.         if (rect.area()> 0)  
98.         {             
99.             point1.x = rect.x;  
100.             point1.y = rect.y;  
101.             point2.x = point1.x + rect.width;  
102.             point2.y = point1.y + rect.height;  
103. 
104.             if (point2.x< a*inputImg.cols)  
105.             {  
106.                 if (point1.x < xmin1)                  
107.                     xmin1 = point1.x;  
108.                 if (point1.y < ymin1)  
109.                     ymin1 = point1.y;                 
110.                 if (point2.x > xmax1 && point2.x < xmax2)               
111.                     xmax1 = point2.x;  
112.                 if (point2.y > ymax1)  
113.                     ymax1 = point2.y;                 
114.             }  
115. 
116.             if (point2.x < b*inputImg.cols&&point2.x > a*inputImg.cols)  
117.             {  
118.                 if (point1.x < xmin2 && point1.x>xmin1)                 
119.                     xmin2 = point1.x;  
120.                 if (point1.y < ymin2)  
121.                     ymin2 = point1.y;  
122.                 if (point2.x > xmax2 && point2.x < xmax3)               
123.                     xmax2 = point2.x;  
124.                 if (point2.y > ymax2)  
125.                     ymax2 = point2.y;                 
126.             }  
127. 
128.             if (point2.x < inputImg.cols&&point2.x > b*inputImg.cols)  
129.             {  
130.                 if (point1.x < xmin3 && point1.x>xmin2)                 
131.                     xmin3 = point1.x;  
132.                 if (point1.y < ymin3)  
133.                     ymin3 = point1.y;                 
134.                 if (point2.x > xmax3)                  
135.                     xmax3 = point2.x;  
136.                 if (point2.y > ymax3)  
137.                     ymax3 = point2.y;                 
138.             }  
139. 
140.             ++iter;  
141.         }  
142.         else  
143.         {  
144.             iter = contours_set.erase(iter);  
145.         }  
146. 
147.     }  
148. 
149. 
150.     if (xmin1 == a*inputImg.cols&& ymin1 == inputImg.rows&&xmax1 == 0 && ymax1== 0)  
151.     {  
152.         xmin1 = ymin1 = xmax1 = ymax1 = 0;  
153.     }  
154.     if (xmin2 == b*inputImg.cols&& ymin2 == inputImg.rows&& xmax2 == a*inputImg.cols&& ymax2 == 0)  
155.     {  
156.         xmin2 = ymin2 = xmax2 = ymax2 = 0;  
157.     }  
158.     if (xmin3 == inputImg.cols&&ymin3 == inputImg.rows&& xmax3 == b*inputImg.cols&& ymax3 == 0)  
159.     {  
160.         xmin3 = ymin3 = xmax3 = ymax3 = 0;  
161.     }  
162.     finalRect1= Rect(xmin1, ymin1, xmax1 - xmin1, ymax1 - ymin1);  
163.     finalRect2 = Rect(xmin2, ymin2, xmax2 - xmin2, ymax2 - ymin2);  
164.     finalRect3 = Rect(xmin3, ymin3, xmax3 - xmin3, ymax3 - ymin3);  
165.     rectangle(inputImg, finalRect1, Scalar(0, 255, 0));  
166.     rectangle(inputImg, finalRect2, Scalar(0, 255, 0));  
167.     rectangle(inputImg, finalRect3, Scalar(0, 255, 0));  
168.     imshow("Fire_Detection", inputImg);   
169. }      

關注【OpenCV學習交流】