【问题标题】:How to change color pixels to white pixels如何将彩色像素更改为白色像素
【发布时间】:2019-03-02 13:22:29
【问题描述】:

我有一个彩色图像作为输入,我想检查方差的颜色信息(如 [0, 0, 0] - [255, 255, 255])。因此,如果方差超过某个点,我想将其更改为白色。

类似这样的:

for y in range(img.shape[0]):
    for x in range(img.shape[1]):
        if numpy.var(img[y][x]) > 1000:
            img[y][x] = [255, 255, 255]

但我需要良好的表现。于是我用 numpy.where() 函数试了一下,但没找到解决办法。

【问题讨论】:

  • 不太明白,是不是像你要找的灰度图???
  • @Inder 不。我不想将颜色像素更改为灰度。我想将红色 [255, 0, 0] 或绿色 [0, 255,0] 等颜色像素更改为白色 [255, 255, 255]。所以我在颜色信息上使用了方差。

标签: python numpy computer-vision


【解决方案1】:

您可以为此使用numpy 的索引:

import numpy as np
import matplotlib.pyplot as plt

img = (np.random.rand(100,100,3)*255).astype(int)

img2 = np.copy(img)
img2[np.var(img, 2)>1000] = np.array([255, 255, 255])

fig, ax = plt.subplots(ncols=2)

ax[0].imshow(img)
ax[1].imshow(img2)

np.var的第二个参数是你要计算方差的轴;在这种情况下是颜色。

结果:

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 2023-01-13
    • 2021-01-27
    • 2019-12-13
    • 2015-05-05
    • 2020-10-24
    • 2012-10-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多