这是一个有点幼稚的问题,但我是数据科学的新手,因此提出了这个问题。 我正在学习读取 2D 图像并执行以下操作的类(class),
image = mpimg.imread('test.jpg')
duplicate = np.copy(image)
red_threshold = green_threshold = blue_threshold = 0
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
具体是这一行
thresholds = (image[:,:,0] < rgb_threshold[0]) | (image[:,:,1] < rgb_threshold[1]) | (image[:,:,2] < rgb_threshold[2])
duplicate[thresholds] = [0,0,0]
这行代码的解释是,
The result, duplicate, is an image in which pixels that were above the threshold have been retained, and pixels below the threshold have been blacked out.
我就是不明白怎么办? 有人可以稍微分解一下并帮助我理解这里发生了什么吗?
最佳答案
上面的表达式,
thresholds = (image[:,:,0] < rgb_threshold[0]) | (image[:,:,1] < rgb_threshold[1]) | (image[:,:,2] < rgb_threshold[2])
展开:
image[:,:,0]
这里是第三个索引,0 是来自 RGB 的图像 channel ,因此 image[:,:,0], image[:,:,1], image[: ,:,2]
分别是RGB channel 的像素。
image[:,:,0] < rgb_threshold[0])
意味着我们只需要低于实际 channel 阈值值(此处为 0)的像素。
在本例中,我们打算获取阈值颜色 channel 值的 numpy 数组的总和 bitwise or运算符 |
例如:
import numpy as np
a = np.array([26,0,46,])
b = np.array([0,55,1,])
print(a | b)
输出:
[26 55 47]
关于python - Matplotlib 中的图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53266496/