python - 将中心像素值复制到 block 中的多数值

标签 python numpy image-processing

我有一个大小为 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/

相关文章:

python - 诗歌虚拟环境已经激活

python - 如何在 font.render 行中包含此变量?

python - 使用快捷方式打开 tkinter 窗口

python - numpy 特征值正确但特征向量错误

python - 为什么 X.dot(X.T) 在 numpy 中需要这么多内存?

android - 在 Android 中显示 YUV 图像

php - 通过合并其他 SVG 文件创建 SVG 图像

python - 在 virtualenv 中使用 pip 安装时出现“权限被拒绝”错误

python - Pandas 按多列分组时组合键

c++ - 他们如何将 IR-Lock 像素转换到镜头前 1 米的法平面上的位置