我有一个大小为 9085x10852 的图像像素预测数组。我想在每个像素周围得到一个 10x10 的 block 。如果中心像素值与 block 中的多数像素值不同,则用多数值替换中心像素值。谁能帮帮我
最佳答案
我在 scikit-image
中四处寻找今天的其他东西,如果你深入挖掘 scikit-image.filters
然后进一步深入 rank
,你遇到了 modal()
!请参阅文档 here .
我使用与@Tonechas 相同的随机种子来生成可比较的结果:
import numpy as np
from skimage.morphology import rectangle # for Structuring Elements (e.g. disk, rectangle)
from skimage.filters.rank import modal # the puppy we want
# Same seed for directly comparable results
np.random.seed(329)
# Sample array/image
arr = np.random.randint(low=0, high=10, size=(6, 8), dtype=np.uint8)
# Run the filter with a 5x5 rectangular Structuring Element
result = modal(arr,rectangle(5,5))
print(result)
array([[9, 2, 0, 0, 0, 2, 4, 5],
[1, 1, 0, 0, 0, 2, 5, 2],
[1, 1, 0, 5, 5, 5, 5, 5],
[1, 1, 1, 5, 5, 5, 4, 5],
[1, 1, 1, 1, 5, 5, 4, 5],
[1, 1, 5, 5, 5, 5, 4, 4]], dtype=uint8)
关键字:Python、numpy、scikit、skimage、图像处理、处理图像、中值、模式、模态、结构元素、形态学、过滤器、过滤器、等级。
关于python - 将中心像素值复制到 block 中的多数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049865/