python基于图像颜色的火焰识别

本文基于图像颜色,主要结合RGB和HSI两种判断依据进行火焰识别。
判断依据参考了以下文章,实在是非常感谢!:
OpenCV学习记录之视频中的火焰检测识别
python版基于颜色的火焰识别

判断条件如下:

  1. R>redThre
  2. R>=G>=B
  3. S>0
  4. S>(255-R)/20
  5. S>=((255-R)*sThre/redThre)

具体代码实现:

img = cv2.imread('fire/fire4.jpeg')
redThre = 115  # 115~135红色分量阈值
sThre = 60  # 55~65饱和度阈值

B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]

B1 = img[:, :, 0] / 255
G1 = img[:, :, 1] / 255
R1 = img[:, :, 2] / 255
minValue = np.array(
    np.where(R1 <= G1, np.where(G1 <= B1, R1, np.where(R1 <= B1, R1, B1)), np.where(G1 <= B1, G1, B1)))
sumValue = R1 + G1 + B1
# HSIS分量计算公式
S = np.array(np.where(sumValue != 0, (1 - 3.0 * minValue / sumValue), 0))
Sdet = (255 - R) / 20
SThre = ((255 - R) * sThre / redThre)
#判断条件
fireImg = np.array(
    np.where(R > redThre, np.where(R >= G, np.where(G >= B, np.where(S > 0, np.where(S > Sdet, np.where(
        S >= SThre, 255, 0), 0), 0), 0), 0), 0))

gray_fireImg = np.zeros([fireImg.shape[0], fireImg.shape[1], 1], np.uint8)
gray_fireImg[:, :, 0] = fireImg
meBImg = cv2.medianBlur(gray_fireImg, 5)
kernel = np.ones((5, 5), np.uint8)
ProcImg = cv2.dilate(meBImg, kernel)
#绘制矩形框
contours, _ = cv2.findContours(ProcImg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ResImg = img.copy()
for c in range(0, len(contours)):
    # 获取矩形的左上角坐标(x,y),以及矩形的宽和高w、h
    x, y, w, h = cv2.boundingRect(contours[c])
    l_top = (x, y)
    r_bottom = (x + w, y + h)
    cv2.rectangle(ResImg, l_top, r_bottom, (255, 0, 0), 2)
cv2.imshow("RESULT", ResImg)
c = cv2.waitKey(0)

输出结果如下:
在这里插入图片描述
基于颜色的火焰识别,如果图片中除了火焰还有一些橘色、红色等颜色相近的地方,识别效果就会差很多,例如下面,手指也识别出来了。

在这里插入图片描述

针对这个问题,目前我没有找到合适的解决方法,所以接下来准备通过机器学习的方式进行火焰识别(ง •̀_•́)ง

Python基于OpenCV的火焰识别可以通过以下步骤实现: 1.导入OpenCV库和必要的模块 ```python import cv2 import numpy as np import imutils ``` 2.读取视频文件或者摄像头 ```python # 视频文件 video = cv2.VideoCapture("video.mp4") # 摄像头 camera = cv2.VideoCapture(0) ``` 3.定义火焰检测函数 ```python def fire_detection(frame): # 转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 高斯滤波 blur = cv2.GaussianBlur(gray, (21, 21), 0) # 计算平均值和标准差 (mean, std) = cv2.meanStdDev(blur) # 判断是否为火焰 if std[0][0] > 10: return True else: return False ``` 4.循环读取每一帧图像,进行火焰检测 ```python while True: # 读取每一帧图像 (grabbed, frame) = camera.read() # 判断是否读取到图像 if not grabbed: break # 缩小图像 frame = imutils.resize(frame, width=500) # 进行火焰检测 if fire_detection(frame): cv2.putText(frame, "Fire Detected!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # 显示图像 cv2.imshow("Fire Detection", frame) # 等待按键 key = cv2.waitKey(1) & 0xFF # 按下q键退出 if key == ord("q"): break ``` 5.释放摄像头或者关闭视频文件 ```python camera.release() video.release() ``` 完整代码实现: ```python import cv2 import numpy as np import imutils def fire_detection(frame): # 转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 高斯滤波 blur = cv2.GaussianBlur(gray, (21, 21), 0) # 计算平均值和标准差 (mean, std) = cv2.meanStdDev(blur) # 判断是否为火焰 if std[0][0] > 10: return True else: return False # 视频文件 video = cv2.VideoCapture("video.mp4") # 摄像头 camera = cv2.VideoCapture(0) while True: # 读取每一帧图像 (grabbed, frame) = camera.read() # 判断是否读取到图像 if not grabbed: break # 缩小图像 frame = imutils.resize(frame, width=500) # 进行火焰检测 if fire_detection(frame): cv2.putText(frame, "Fire Detected!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # 显示图像 cv2.imshow("Fire Detection", frame) # 等待按键 key = cv2.waitKey(1) & 0xFF # 按下q键退出 if key == ord("q"): break camera.release() video.release() cv2.destroyAllWindows() ``` 注意:该方法只能作为基础的火焰检测,不能用于实际应用中,因为它存在一定的误检率和漏检率。如果需要进行更加精确的火焰检测,需要使用更加复杂的算法和模型。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rena要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值