【问题标题】:Slide a window through an image, and change the value of center pixel in window based on the average value of the sliding window在图像中滑动一个窗口,并根据滑动窗口的平均值改变窗口中心像素的值
【发布时间】:2022-01-20 21:52:46
【问题描述】:

我正在尝试在灰色图像上滑动 5 by 5 2D 窗口。 如果该 2D 窗口内的所有像素都等于 200,则将窗口内的中心像素值替换为 100。

import cv2
import numpy as np
from PIL import Image
from scipy.signal import convolve2d as conv2

image = cv2.imread("testImage.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

output = (conv2(gray, np.ones((5, 5), dtype=int), 'valid') == 200).astype(int)

我不知道从这里去哪里或如何做到这一点

【问题讨论】:

    标签: python image opencv scipy python-imaging-library


    【解决方案1】:

    你说得几乎是正确的。您的 convolve2d 事物的输出是一个布尔数组,该数组具有 True 您希望更改值的位置。您可以将其用作原始数组的精美索引,这是 numpy 的最佳功能之一。请注意,您需要“相同”而不是“有效”,以确保输出数组的大小正确。

    import cv2
    import numpy as np
    from scipy.signal import convolve2d as conv2
    
    image = cv2.imread("splat.png")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    box = np.ones((3,3),dtype=int)
    output = conv2(gray, box, 'same') == 200
    gray[output] = 100
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多