python - Matplotlib 中的图像处理

标签 python numpy matplotlib computer-vision

这是一个有点幼稚的问题,但我是数据科学的新手,因此提出了这个问题。 我正在学习读取 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/

相关文章:

python - 在散点图中,在 matplotlib 中围绕数据点绘制平滑多边形

python - 我可以在 Windows 32 上免费将 numpy 和 scipy 编译为 egg 吗?

Python/Gridspec height_ratio 问题

python - scipy.optimize()值错误:Shape mismatch for sum

python - 获取默认的 x 刻度标签文本用作索引,以使用其他文本更新刻度标签

python - matplotlib 中的多进程绘图

python - 尝试使用 whisper-merge 时出现错误 "TypeError: ' NoneType' object is not iterable"

python - 过滤数据帧值

python - 是否有内置函数可以执行 numpy.fromstring 的相反操作?

python - 如何将 pandas 数据帧列表转换为 3d numpy 数组?