第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 火焰识别python_OpenCV_火焰检测——完整代码

火焰识别python_OpenCV_火焰检测——完整代码

时间:2021-04-12 10:54:23

相关推荐

火焰识别python_OpenCV_火焰检测——完整代码

转:/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

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

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

检测图片为:

检测效果图为:

程序代码附下:

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(i,j); //每个像素的R,G,B值

G = multiRGB[1].at(i,j);

R = multiRGB[2].at(i,j);

/*B = inImg.at(i,inImg.channels()*j + 0); //另一种调用图片中像素RGB值的方法

G = inImg.at(i,inImg.channels()*j + 1);

R = inImg.at(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(i,j) = 255;

}

else

{

fireImg.at(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> contours_set;//保存轮廓提取后的点集及拓扑关系

findContours(foreImg,contours_set,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);

Mat result0;

Scalar holeColor;

Scalar externalColor;

vector >::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);

}

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(i,j); //每个像素的R,G,B值

G = multiRGB[1].at(i,j);

R = multiRGB[2].at(i,j);

/*B = inImg.at(i,inImg.channels()*j + 0); //另一种调用图片中像素RGB值的方法

G = inImg.at(i,inImg.channels()*j + 1);

R = inImg.at(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(i,j) = 255;

}

else

{

fireImg.at(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> contours_set;//保存轮廓提取后的点集及拓扑关系

findContours(foreImg,contours_set,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);

Mat result0;

Scalar holeColor;

Scalar externalColor;

vector >::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);

}

另附几个其他的效果图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。