Opencv + python火焰分割加速版

之前在CSDN上面看到不少基于火焰颜色检测的文章,比如https://blog.csdn.net/qq_27569955/article/details/51564887
https://blog.csdn.net/coldplayplay/article/details/70212483

这些博主对于火焰的分割都是基于颜色进行阈值分割的,分割的条件来自于台湾某大学教授在十多年前一片论文上提出的火焰像素点的判别条件
其中RT表示R的阈值,ST表示S的阈值,S为饱和度

但是这几位博主提取火焰像素点使用的方法都是遍历图像的像素点,一一判别其是否为火焰像素点,这样做使得程序在运行的时候很慢,特别是对于处理视频时将产生极大的延迟。在VS上运行时间还勉强能接受,但在pyhton上简直就不能用,因此我尝试使用python中的numpy直接对整张图片的像素进行处理,极大的提高了运行速度,保持了在使用视频时检测的实时性。

def contrast_brightness_demo(image, c, b):  #其中c为对比度,b为每个像素加上的值(调节亮度)
    blank = np.zeros(image.shape, image.dtype)   #创建一张与原图像大小及通道数都相同的黑色图像
    dst = cv.addWeighted(image, c, blank, 1-c, b) #c为加权值,b为每个像素所加的像素值
    ret, dst = cv.threshold(dst, 25, 255, cv.THRESH_BINARY)
    return dst


capture = cv.VideoCapture("C:\\Users\\xxx\\Desktop\\2.mp4")
redThre = 105
saturationTh = 42
while(True):
    ret, frame = capture.read()
    cv.imshow("frame", frame)
    B = frame[:, :, 0]
    G = frame[:, :, 1]
    R = frame[:, :, 2]
    minValue = np.array(np.where(R <= G, np.where(G <= B, R, np.where(R <= B, R, B)), np.where(G <= B, G, B)))
    S = 1 - 3.0 * minValue / (R + G + B + 1)
    fireImg = np.array(np.where(R > redThre, np.where(R >= G, np.where(G >= B, np.where(S >= 0.2, np.where(S >= (255 - R)*saturationTh/redThre, 255, 0), 0), 0), 0), 0))
    gray_fireImg = np.zeros([fireImg.shape[0], fireImg.shape[1], 1], np.uint8)
    gray_fireImg[:, :, 0] = fireImg
    gray_fireImg = cv.GaussianBlur(gray_fireImg, (7, 7), 0)
    gray_fireImg = contrast_brightness_demo(gray_fireImg, 5.0, 25)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    gray_fireImg = cv.morphologyEx(gray_fireImg, cv.MORPH_CLOSE, kernel)
    dst = cv.bitwise_and(frame, frame, mask=gray_fireImg)
    cv.imshow("fire", dst)
    cv.imshow("gray_fireImg", gray_fireImg)
    c = cv.waitKey(40)
    if c == 27:
        break
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值