简体   繁体   English

使用 python 将 IR 图像转换为 RGB

[英]convert IR image to RGB with python

The code below is intended to take an infrared image (B&W) and convert it to RGB.下面的代码旨在获取红外图像 (B&W) 并将其转换为 RGB。 It does so successfully, but with significant noise.它成功地做到了,但噪音很大。 I have included a few lines for noise reduction but they don't seem to help.我已经包括了几行降噪,但它们似乎没有帮助。 I've included the starting/resulting photos below.我在下面包含了开始/生成的照片。 Any advice/corrections are welcome and thank you in advance!欢迎任何建议/更正,并在此先感谢您!

from skimage import io
import numpy as np
import glob, os
from tkinter import Tk
from tkinter.filedialog import askdirectory
import cv2

path = askdirectory(title='Select PNG Folder') # shows dialog box and return the path
outpath = askdirectory(title='Select SAVE Folder') 

# wavelength in microns
MWIR = 4.5

R = .642
G = .532
B = .44

vector = [R, G, B]
vectorsum = np.sum(vector)
vector = vector / vectorsum #normalize
vector = vector*255 / MWIR #changing this value changes the outcome significantly so I 
#have been messing with it in the hopes of fixing it but no luck so far.
vector = np.power(vector, 4)

for file in os.listdir(path):
  if file.endswith(".png"):
    imIn = io.imread(os.path.join(path, file))
    imOut = imIn * vector
    ret,thresh = cv2.threshold(imOut,64,255,cv2.THRESH_BINARY)
    kernel = np.ones((5, 5), np.uint8)
    erode = cv2.erode(thresh, kernel, iterations = 1)
    result = cv2.bitwise_or(imOut, erode)
    io.imsave(os.path.join(outpath, file) + '_RGB.png',imOut.astype(np.uint8))

起始图像 结果图像

Your noise looks like completely random values, so I suspect you have an error in your conversion from float to uint8 .您的噪音看起来像完全随机的值,所以我怀疑您在从floatuint8的转换中出错。 But instead of rolling everything for yourself, why don't you just use:但是,与其为自己滚动一切,不如直接使用:

  imOut = cv2.cvtColor(imIn,cv2.COLOR_GRAY2BGR)

Here is one way to do that in Python/OpenCV.这是在 Python/OpenCV 中执行此操作的一种方法。

Your issue is likely that your channel values are exceeding the 8-bit range.您的问题可能是您的通道值超出了 8 位范围。

Sorry, I do not understand the relationship between your R,G,B weights and your MWIR.抱歉,我不明白你的 R,G,B 重量和你的 MWIR 之间的关系。 Dividing by MWIR will do nothing if your weights are properly normalized.如果您的权重正确归一化,除以 MWIR 将无济于事。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# read image
img = cv2.imread('car.jpg')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# make color channels
red = gray.copy()
green = gray.copy()
blue = gray.copy()

# set weights
R = .642
G = .532
B = .44

MWIR = 4.5

# get sum of weights and normalize them by the sum
R = R**4
G = G**4
B = B**4
sum = R + G + B
R = R/sum
G = G/sum
B = B/sum
print(R,G,B)

# combine channels with weights
red = (R*red)
green = (G*green)
blue = (B*blue)
result = cv2.merge([red,green,blue])

# scale by ratio of 255/max to increase to fully dynamic range
max=np.amax(result)
result = ((255/max)*result).clip(0,255).astype(np.uint8)

# write result to disk
cv2.imwrite("car_colored.png", result)

# display it
cv2.imshow("RESULT", result)
cv2.waitKey(0)


Result结果

在此处输入图像描述

If the noise is coming from the sensor itself, like a grainy noise, you'll need to look into denoising algorithms.如果噪声来自传感器本身,例如颗粒状噪声,则需要研究去噪算法。 scikit-image and opencv provide some denoising algorithms you can try. scikit-imageopencv提供了一些你可以尝试的去噪算法。 Maybe take a look at this and this .也许看看这个这个

I recently learned about matplotlib.cm , which handles colormaps.我最近了解了处理颜色图的matplotlib.cm I've been using those to artificially color IR images, and made a brief example using the same black & white car image used above.我一直在使用它们对红外图像进行人工着色,并使用上面使用的相同黑白汽车图像做了一个简短的示例。 Basically, I create a colormap.csv file locally, then refer to it for RGB weights.基本上,我在本地创建了一个 colormap.csv 文件,然后参考它获取 RGB 权重。 You may have to pick and choose which colormap you prefer, but that's up to personal preference.您可能必须选择您喜欢的颜色图,但这取决于个人喜好。

Input image:输入图像:

黑白相间的汽车,“bwcar.jpg”

Python: Python:

import os
import numpy as np
import cv2
from matplotlib import cm

# Multiple colormap options are available- I've hardcoded viridis for this example.
colormaps = ["viridis", "plasma", "inferno", "magma", "cividis"] 

def CreateColormap():
    if not os.path.exists("viridis_colormap.csv"):
        # Get 256 entries from "viridis" or any other Matplotlib colormap
        colormap = cm.get_cmap("viridis", 256)
        # Make a Numpy array of the 256 RGB values
        # Each line corresponds to an RGB colour for a greyscale level
        np.savetxt("viridis_colormap.csv", (colormap.colors[...,0:3]*255).astype(np.uint8), fmt='%d', delimiter=',')

def RecolorInfraredImageToRGB(ir_image):       
    # Load RGB lookup table from CSV file
    lookup_table = np.loadtxt("viridis_colormap.csv", dtype=np.uint8, delimiter=",")
    # Make output image, same height and width as IR image, but 3-channel RGB
    result = np.zeros((*ir_image.shape, 3), dtype=np.uint8)
    # Take entries from RGB LUT according to greyscale values in image
    np.take(lookup_table, ir_image, axis=0, out=result)       
    return result

if __name__ == "__main__":
    CreateColormap()
    img = cv2.imread("bwcar.jpg")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    recolored = RecolorInfraredImageToRGB(gray)
    cv2.imwrite("car_recolored.png", recolored)
    cv2.imshow("Viridis recolor", recolored)
    cv2.waitKey(0)

Output: Output:

使用 matplotlib 的 Viridis 颜色图“car_recolored.png”重新着色的汽车

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM