【问题标题】:Image manipulation in MatplotlibMatplotlib 中的图像处理
【发布时间】:2018-11-12 16:39:39
【问题描述】:

这是一个有点幼稚的问题,但我是数据科学的新手,因此是这个问题。 我正在学习一个读取 2D 图像并执行以下操作的课程,

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]

这行代码的解释是,

结果,副本,是一个图像,其中像素高于 阈值已被保留,低于阈值的像素已被保留 变黑了。

我只是不明白怎么做? 有人可以稍微分解一下并帮助我了解这里发生了什么吗?

【问题讨论】:

  • 我建议使用最小的代表性数据并运行发布的代码,并分别进一步研究| 的每个部分。
  • 这不是 3D 图像,它是具有 3 个通道的 2D 图像。一些软件将这样的图像表示为 3D 矩阵,但这并不能使其成为 3D 图像。
  • @CrisLuengo 已编辑和更正。
  • 无论如何,这个例子在我看来有点错误,你是从哪里复制粘贴的?

标签: python numpy matplotlib computer-vision


【解决方案1】:

上面的表达式,

thresholds = (image[:,:,0] < rgb_threshold[0]) | (image[:,:,1] < rgb_threshold[1]) | (image[:,:,2] < rgb_threshold[2])

展开:

image[:,:,0] 

这里是第3个索引,0是RGB图像的通道,因此image[:,:,0], image[:,:,1], image[:,:,2]分别是RGB通道的像素。

image[:,:,0] < rgb_threshold[0]) 

表示,我们只需要低于实际通道阈值值的像素,这里是0。

在这种情况下,我们打算使用 bitwise or 运算符 | 获得阈值颜色通道值的 numpy 数组的总和,例如:

import numpy as np

a = np.array([26,0,46,])
b = np.array([0,55,1,])

print(a | b)

输出:

[26 55 47]

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多