1.輸入是經(jīng)過(guò)Jet處理的偽彩色圖像,輸出是深度圖
def Jet2Gray(false_color_image):
gray_values = np.arange(256, dtype=np.uint8)
color_values = map(tuple, cv2.applyColorMap(gray_values, cv2.COLORMAP_JET).reshape(256, 3))
color_to_gray_map = dict(zip(color_values, gray_values))
# apply the inverse map to the false color image to reconstruct the grayscale image
gray_image = np.apply_along_axis(lambda bgr: color_to_gray_map[tuple(bgr)], 2, false_color_image)
return gray_image
2.有時(shí)候偽彩色圖像經(jīng)過(guò)編碼等會(huì)破壞原來(lái)的映射關(guān)系,直接用上述方法處理行不通,所以需要經(jīng)過(guò)濾波處理,方法如下:
import cv2
import numpy as np
# create an inverse from the colormap to gray values
gray_values = np.arange(256, dtype=np.uint8)
color_values = map(tuple, cv2.applyColorMap(gray_values, cv2.COLORMAP_JET).reshape(256, 3))
color_to_gray_map = dict(zip(color_values, gray_values))
keys = list(color_to_gray_map.keys())
pass
pic_path = "test.png" # 圖片路徑
img = cv2.imread(pic_path)
for x in range(img.shape[0]): # 圖片的高
print(x)
for y in range(img.shape[1]): # 圖片的寬
px = img[x, y]
# print(px) # 這樣就能得到每個(gè)點(diǎn)的bgr值
d_min = 100000
best_val = px
for key in keys:
d = 0
for i in range(3):
d += abs(float(px[i])-float(key[i]))
if d < d_min:
best_val = np.array(key)
# temp = img[x, y]
d_min = d
img[x, y] = best_val