天天看点

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学习交流】