我正在查看一些绘制GUI的视图(屏幕)构建代码,例如将变形的镜头视图转换为平面视图。我碰到过术语正向LUT和反向LUT,但我不明白它是什么以及为什么使用它? 有人可以给我解释一下,或者给我一些指导以帮助我了解它们吗?
答案 0 :(得分:0)
一个“查找表” ,或LUT是一个小表,通常其中有256个条目。它用于对图像应用“点处理” ,即每个像素处理后的新值仅取决于该点上的先前值(而不是任何相邻像素)。
您无需为图像中的1200万像素中的每一个进行大量数学运算或if
语句,而只需将每个像素的当前8位值用作查找表的索引即可查找该像素的新值。通常,它只是停止CPU执行if
语句的速度,因为它只是对表的索引操作。在硬件中高速实现也非常简单。
您可以使用它来阈值图像,或更改图像的对比度,或节省空间。在这最后一种技术中,您基本上是在创建具有256色调色板的图像,然后只存储1个字节并将该字节用于,而不是为每个像素(即R,G和B)存储3个字节。”查找” 颜色-就像是魔术一样,您的图片大小是图片的1/3。
这里是一个小例子,我制作了一个LUT,将所有低于64的元素设为黑色,并将高于该值的所有元素设为白色,然后将其应用于灰度图像。之后,我添加了红色边框,以便您可以在Stack Overflow的白色背景上看到图像的范围:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open the input image as numpy array, convert to greyscale
npImage=np.array(Image.open("grey.png").convert("L"))
# Make a LUT (Look-Up Table) to translate image values
LUT=np.zeros(256,dtype=np.uint8)
for idx in range(64,255):
# All pixels > 64 become white
LUT[idx]=255
# Apply LUT
npImage = LUT[npImage]
# Apply LUT and save resulting image
Image.fromarray(npImage).save('result.png')
开始图像:
结果图片:
这是另一个示例,其中我使LUT向后运行,因此它使图像反转。
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open the input image as numpy array, convert to greyscale
npImage=np.array(Image.open("grey.png").convert("L"))
# Make a LUT (Look-Up Table) to translate image values to their inverse/negative
# i.e. 0 input maps to 255 output
# 1 input maps to 254 output
LUT = np.arange(255,-1,-1,dtype=np.uint8)
# Apply LUT
npImage = LUT[npImage]
# Apply LUT and save resulting image
Image.fromarray(npImage).save('result.png')
关键字:Python,Numpy,图像,图像处理,LUT,查找表,查找,取反,取反,阈值