OpenCV小例程——火焰检测(完整代码)

火焰检测小程序

前几天,偶然看到了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<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);
}


另附几个其他的效果图:



  • 16
    点赞
  • 193
    收藏
    觉得还不错? 一键收藏
  • 42
    评论
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值