首页 技术 正文
技术 2022年11月7日
0 收藏 675 点赞 765 浏览 3942 个字

转:http://blog.csdn.net/xiao_lxl/article/details/43307993

火焰检测小程序

前几天,偶然看到了An Early Fire-Detection Method Based on Image Processing ,The Author is:Thou-Ho (Chao-Ho) Chen, Ping-Hsueh Wu, and Yung-Chuen Chiou

这篇文章,参照他的颜色模型做了一个火焰检测的小程序,以此记录并与大家分享。

针对视频,若是加上火焰背景建模,效果会更好。有兴趣的可以试一下。

检测图片为:

OpenCV_火焰检测——完整代码

检测效果图为:

OpenCV_火焰检测——完整代码

程序代码附下:

  1. int main()
  2. {
  3. string filepath = “F:\\video\\fire\\fire0.jpg”;
  4. Mat inputImg = imread(filepath,1);
  5. CheckColor(inputImg);
  6. return 0;
  7. }
  8. //////////////////////////////////
  9. //The Color Check is According to “An Early Fire-Detection Method Based on Image Processing”
  10. //The Author is:Thou-Ho (Chao-Ho) Chen, Ping-Hsueh Wu, and Yung-Chuen Chiou
  11. //////////////////////////////////////
  12. Mat CheckColor(Mat &inImg)
  13. {
  14. Mat fireImg;
  15. fireImg.create(inImg.size(),CV_8UC1);
  16. int redThre = 115; // 115~135
  17. int saturationTh = 45; //55~65
  18. Mat multiRGB[3];
  19. int a = inImg.channels();
  20. split(inImg,multiRGB); //将图片拆分成R,G,B,三通道的颜色
  21. for (int i = 0; i < inImg.rows; i ++)
  22. {
  23. for (int j = 0; j < inImg.cols; j ++)
  24. {
  25. float B,G,R;
  26. B = multiRGB[0].at<uchar>(i,j); //每个像素的R,G,B值
  27. G = multiRGB[1].at<uchar>(i,j);
  28. R = multiRGB[2].at<uchar>(i,j);
  29. /*B = inImg.at<uchar>(i,inImg.channels()*j + 0); //另一种调用图片中像素RGB值的方法
  30. G = inImg.at<uchar>(i,inImg.channels()*j + 1);
  31. R = inImg.at<uchar>(i,inImg.channels()*j + 2);*/
  32. int maxValue = max(max(B,G),R);
  33. int minValue = min(min(B,G),R);
  34. double S = (1-3.0*minValue/(R+G+B));
  35. //R > RT  R>=G>=B  S>=((255-R)*ST/RT)
  36. if(R > redThre && R >= G && G >= B && S >0.20 && S >((255 – R) * saturationTh/redThre))
  37. {
  38. fireImg.at<uchar>(i,j) = 255;
  39. }
  40. else
  41. {
  42. fireImg.at<uchar>(i,j) = 0;
  43. }
  44. }
  45. }
  46. dilate(fireImg,fireImg,Mat(5,5,CV_8UC1));
  47. imshow(“fire”,fireImg);
  48. waitKey(0);
  49. DrawFire(inImg,fireImg);
  50. return fireImg;
  51. }
  52. void DrawFire(Mat &inputImg,Mat foreImg)
  53. {
  54. vector<vector<Point>> contours_set;//保存轮廓提取后的点集及拓扑关系
  55. findContours(foreImg,contours_set,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  56. Mat result0;
  57. Scalar holeColor;
  58. Scalar externalColor;
  59. vector<vector<Point> >::iterator iter = contours_set.begin() ;
  60. for(; iter!= contours_set.end(); )
  61. {
  62. Rect rect = boundingRect(*iter );
  63. float radius;
  64. Point2f center;
  65. minEnclosingCircle(*iter,center,radius);
  66. if (rect.area()> 0)
  67. {
  68. rectangle(inputImg,rect,Scalar(0,255,0));
  69. ++ iter;
  70. }
  71. else
  72. {
  73. iter = contours_set.erase(iter);
  74. }
  75. }
  76. imshow(“showFire”,inputImg);
  77. waitKey(0);
  78. }

OpenCV_火焰检测——完整代码

int main()
{
string filepath = "F:\\video\\fire\\fire0.jpg";
Mat inputImg = imread(filepath,1); CheckColor(inputImg);
return 0;
}
//////////////////////////////////
//The Color Check is According to "An Early Fire-Detection Method Based on Image Processing"
//The Author is:Thou-Ho (Chao-Ho) Chen, Ping-Hsueh Wu, and Yung-Chuen Chiou
//////////////////////////////////////
Mat CheckColor(Mat &inImg)
{
Mat fireImg;
fireImg.create(inImg.size(),CV_8UC1);int redThre = 115; // 115~135
int saturationTh = 45; //55~65
Mat multiRGB[3];
int a = inImg.channels();
split(inImg,multiRGB); //将图片拆分成R,G,B,三通道的颜色for (int i = 0; i < inImg.rows; i ++)
{
for (int j = 0; j < inImg.cols; j ++)
{
float B,G,R;
B = multiRGB[0].at<uchar>(i,j); //每个像素的R,G,B值
G = multiRGB[1].at<uchar>(i,j);
R = multiRGB[2].at<uchar>(i,j);/*B = inImg.at<uchar>(i,inImg.channels()*j + 0); //另一种调用图片中像素RGB值的方法
G = inImg.at<uchar>(i,inImg.channels()*j + 1);
R = inImg.at<uchar>(i,inImg.channels()*j + 2);*/int maxValue = max(max(B,G),R);
int minValue = min(min(B,G),R);double S = (1-3.0*minValue/(R+G+B));//R > RT R>=G>=B S>=((255-R)*ST/RT)
if(R > redThre && R >= G && G >= B && S >0.20 && S >((255 - R) * saturationTh/redThre))
{
fireImg.at<uchar>(i,j) = 255;
}
else
{
fireImg.at<uchar>(i,j) = 0;
}
}
}dilate(fireImg,fireImg,Mat(5,5,CV_8UC1));
imshow("fire",fireImg);
waitKey(0);DrawFire(inImg,fireImg);return fireImg;
}void DrawFire(Mat &inputImg,Mat foreImg)
{
vector<vector<Point>> contours_set;//保存轮廓提取后的点集及拓扑关系findContours(foreImg,contours_set,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);Mat result0;
Scalar holeColor;
Scalar externalColor;vector<vector<Point> >::iterator iter = contours_set.begin() ;
for(; iter!= contours_set.end(); )
{
Rect rect = boundingRect(*iter );
float radius;
Point2f center;
minEnclosingCircle(*iter,center,radius); if (rect.area()> 0)
{rectangle(inputImg,rect,Scalar(0,255,0));
++ iter;}
else
{
iter = contours_set.erase(iter);
}
}imshow("showFire",inputImg);
waitKey(0);
}

另附几个其他的效果图:

OpenCV_火焰检测——完整代码

OpenCV_火焰检测——完整代码

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866