热图像处理

问题描述

我正在尝试使用“VideoCapture”和“cv2.imshow”脚本通过 Python OPENCV 流式传输 FLIR Lepton 3.5。然后,我会做一些检测和控制。这是我遇到的问题,我只能得到一个非常微弱的黑色/灰色视频流,在流的底部似乎有几行坏点。这是意料之中的,因为输出应该是 16 位 RAW 图像数据。所以,

  1. 我正在尝试转换为 RGB888 图像数据,以便流具有“颜色”。
  2. 为什么流视频是静态模式,它不像普通的嵌入式笔记本网络摄像头那样流视频?

我已经尝试了其他人共享的代码/脚本,甚至是 FLIR 应用笔记中的示例代码,但没有奏效。感谢您的帮助。

环境:Windows 10、Python 3.7.6、PyCharm、OpenCV(最新)、FLIR Lepton 3.5 相机/PureThermal2

代码

import cv2
import numpy as np


image_counter = 0
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter.fourcc('Y','1','6',' '))
video.set(cv2.CAP_PROP_CONVERT_RGB,0)

if video.isOpened(): # try to get the first frame
    rval,frame = video.read()
else:
    rval = False

while rval:
    normed = cv2.normalize(frame,None,65535,cv2.norM_MINMAX)

    nor=cv2.cvtColor(np.uint8(normed),cv2.COLOR_GRAY2BGR)
    cv2.imshow("preview",cv2.resize(nor,dsize= (640,480),interpolation = cv2.INTER_LINEAR))

    key = cv2.waitKey(1)
    if key == 27: # exit on ESC
        break

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

解决方法

在没有相机的情况下给出答案是很有挑战性的。
请注意,我无法验证我的解决方案。

我发现您的代码存在以下问题:

  • rval,frame = video.read() 必须在 while 循环内。
    代码抓取下一帧。
    如果你想抓取不止一帧,你应该循环执行。

  • normed = cv2.normalize(frame,None,65535,cv2.NORM_MINMAX)
    返回范围 [0,65535] 内的 uint16 值。
    uint8 转换为 np.uint8(normed) 时出现溢出。
    我建议标准化到范围 [0,255]。
    您还可以选择结果类型为 uint8

     normed = cv2.normalize(frame,255,cv2.NORM_MINMAX,cv2.CV_8U)
    

这是完整的更新代码(未测试):

import cv2
import numpy as np

image_counter = 0
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter.fourcc('Y','1','6',' '))
video.set(cv2.CAP_PROP_CONVERT_RGB,0)

if video.isOpened(): # try to get the first frame
    rval,frame = video.read()
else:
    rval = False

# Create an object for executing CLAHE.
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))

while rval:
    # Get a Region of Interest slice - ignore the last 3 rows.
    frame_roi = frame[:-3,:]

    # Normalizing frame to range [0,255],and get the result as type uint8.
    normed = cv2.normalize(frame_roi,cv2.CV_8U)

    # Apply CLAHE - contrast enhancement.
    # Note: apply the CLAHE on the uint8 image after normalize.
    # CLAHE supposed to work with uint16 - you may try using it without using cv2.normalize
    cl1 = clahe.apply(normed)

    nor = cv2.cvtColor(cl1,cv2.COLOR_GRAY2BGR)  # Convert gray-scale to BGR (no really needed).

    cv2.imshow("preview",cv2.resize(nor,dsize=(640,480),interpolation=cv2.INTER_LINEAR))
    key = cv2.waitKey(1)
    if key == 27: # exit on ESC
        break

    # Grab the next frame from the camera.
    rval,frame = video.read()

着色:

https://groups.google.com/g/flir-lepton/c/Cm8lGQyspmk

结果:
enter image description here

这是带有着色的代码示例(使用“铁黑”颜色图):

import cv2
import numpy as np

# https://groups.google.com/g/flir-lepton/c/Cm8lGQyspmk
def generateColourMap():
    """
    Conversion of the colour map from GetThermal to a numpy LUT:
        https://github.com/groupgets/GetThermal/blob/bb467924750a686cc3930f7e3a253818b755a2c0/src/dataformatter.cpp#L6
    """

    lut = np.zeros((256,1,3),dtype=np.uint8)

    colormapIronBlack = [
        255,253,251,249,247,245,243,241,239,237,235,233,231,229,227,225,223,221,219,217,215,213,211,209,207,205,203,201,199,197,195,193,191,189,187,185,183,181,179,177,175,173,171,169,167,165,163,161,159,157,155,153,151,149,147,145,143,141,139,137,135,133,131,129,126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,9,31,45,53,17,67,19,21,23,89,25,27,103,29,111,41,121,51,123,61,125,71,127,128,81,130,91,132,101,134,136,3,138,144,158,5,140,172,7,186,13,194,196,198,200,95,37,212,85,214,43,216,75,218,49,69,55,59,224,57,47,226,228,39,230,232,234,99,236,238,109,240,119,242,244,152,156,246,160,248,178,15,182,250,192,252,206,210,254,220,24]

    def colormapChunk(ulist,step):
        return map(lambda i: ulist[i: i + step],range(0,len(ulist),step))

    chunks = colormapChunk(colormapIronBlack,3)

    red = []
    green = []
    blue = []

    for chunk in chunks:
        red.append(chunk[0])
        green.append(chunk[1])
        blue.append(chunk[2])

    lut[:,0] = blue
    lut[:,1] = green
    lut[:,2] = red

    return lut

# Generate color map - used for colorizing the video frame.
colorMap = generateColourMap()


image_counter = 0
video = cv2.VideoCapture(0,cv2.COLOR_GRAY2BGR)  # Convert gray-scale to BGR (no really needed).
        
    colorized_img = cv2.LUT(nor,colorMap)  # Colorize the gray image with "false colors".

    cv2.imshow("preview",cv2.resize(colorized_img,frame = video.read()

注意:
红外传感器不是彩色传感器。
为框架着色使用“假色” - 着色可用于 eustatic 目的。
“假色”没有物理意义。
对红外图像进行着色的方法有很多种,并没有“标准着色”方法。