我正在尝试一个可以在 python 中处理图像的脚本。 第一张图片可以正常工作,而第二张图片根本不起作用。
剧本是
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
image = mpimg.imread('test.jpg')
# Grab the x and y size and make a copy of the image
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
# Define color selection criteria
###### MODIFY THESE VARIABLES TO MAKE YOUR COLOR SELECTION
red_threshold = 200
green_threshold = 200
blue_threshold = 200
######
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Do a boolean or with the "|" character to identify
# pixels below the thresholds
thresholds = (image[:,:,0] < rgb_threshold[0]) \
| (image[:,:,1] < rgb_threshold[1]) \
| (image[:,:,2] < rgb_threshold[2])
color_select[thresholds] = [0,0,0]
plt.imshow(color_select)
然后我发现了第二张图片不起作用的原因
print("Red channel - Min:", np.min(image[:,:,0]), "Max:", np.max(image[:,:,0]))
print("Green channel - Min:", np.min(image[:,:,1]), "Max:", np.max(image[:,:,1]))
print("Blue channel - Min:", np.min(image[:,:,2]), "Max:", np.max(image[:,:,2]))
对于第一张图片
Red channel - Min: 0 Max: 255
Green channel - Min: 10 Max: 255
Blue channel - Min: 0 Max: 255
对于第二张不起作用的图片
Red channel - Min: 0.0 Max: 1.0
Green channel - Min: 0.0 Max: 1.0
Blue channel - Min: 0.0 Max: 1.0
有人可以向我解释一下为什么有些图像使用 0-255 而其他图像使用 0.0 到 1.0 的基本原理吗?
我确信有很多因素,对历史实践和影响的讨论不太适合 StackOverflow,但这是我的想法。我确信我遗漏了很多。
多年来相机传感器都是 8 位,为每个样本生成 0..255 范围内的图像。然后制造商开始生产 10 位传感器,具有更高的动态范围 0..1023。然后是 12 位,然后是 14 位,因此范围增加了。
标准 JPEG 仅支持 8 位。 PNG 在高端只能支持 8/16 位,而且仍然只能是整数。 TIFF 和 EXR 在高端支持 32/64 位,但最重要的是还支持浮点。
许多早期的图像处理算法被编码为期望 8 位样本,例如
pixel = 255 - pixel
用于图像反转。当然,当您的数据在 0..65535 范围内时,这种情况就会中断。
然后机器学习出现了,使用 0..1 范围和浮点数来处理所有内容更为明智,因此如果数据是 16 位浮点数、32 位浮点数或 64 位浮点数,则反转
pixel = 1 - pixel
可以工作。