gpt4 book ai didi

python - 图像抽取后的异常直方图

转载 作者:太空狗 更新时间:2023-10-30 00:17:10 26 4
gpt4 key购买 nike

使用img_decim_arr = img_arr[::2,::2] 进行简单图像抽取后,我获得了与原始图像直方图非常相似的直方图:enter image description here
抽取使用:skimage.measure.block_reduce(img_arr, block_size = (2,2), func=np.mean)(2x2 block 平均)这是下采样的推荐方法(在一些讨论中可在 stackoverflow 上找到) ) 产生非常有特色的直方图:enter image description here
每第二个垃圾桶都更大。我不确定这可能是由于某种混叠效应造成的。任何人都可以解释并提供一些关于下采样如何影响图像(2D 信号)直方图的理论提示吗?

最佳答案

问题是 np.mean 函数,因为它不舍入为整数并返回 float 。

import numpy as np
import skimage.measure

a = (10 * np.random.randn(10,10) + 127).astype(np.uint8)
a
Out[4]:
array([[121, 124, 139, 129, 130, 114, 127, 96, 114, 135],
[127, 132, 102, 142, 119, 107, 138, 130, 141, 132],
[113, 132, 132, 118, 121, 120, 142, 115, 124, 128],
[127, 121, 129, 129, 121, 119, 126, 113, 128, 116],
[144, 131, 123, 131, 130, 137, 140, 142, 127, 128],
[127, 126, 124, 115, 127, 125, 122, 126, 147, 132],
[118, 119, 117, 117, 133, 149, 122, 120, 116, 138],
[147, 147, 127, 117, 123, 123, 136, 121, 139, 129],
[142, 129, 113, 111, 130, 116, 137, 127, 106, 148],
[132, 141, 141, 142, 119, 132, 126, 115, 131, 122]], dtype=uint8)

b = skimage.measure.block_reduce(a, block_size = (2,2), func=np.mean)
b
Out[6]:
array([[ 126. , 128. , 117.5 , 122.75, 130.5 ],
[ 123.25, 127. , 120.25, 124. , 124. ],
[ 132. , 123.25, 129.75, 132.5 , 133.5 ],
[ 132.75, 119.5 , 132. , 124.75, 130.5 ],
[ 136. , 126.75, 124.25, 126.25, 126.75]])

这可能会给您自己的逻辑带来有趣的副作用。它肯定与 matplotlib 的直方图函数搞混了,因为有 float 会使它以不同的方式思考如何放置 bin 边界。

检查一下:

a = (10 * np.random.randn(200,200) + 127).astype(np.uint8)
b = skimage.measure.block_reduce(a, block_size = (2,2), func=np.mean)
hist(b.ravel(), bins=255)

enter image description here

hist 函数返回的数组中的白色位实际上为零。如果在我的玩具示例中强制四舍五入,情况会变得更糟:

hist(b.ravel().astype(np.uint8), bins=255)

Ugly histogram

给它 bins 和 range 解决了这个问题。即使你放大关闭

 hist(b.ravel().astype(np.uint8), bins=255, range=(0,255))

Good histogram

Zoomed good histogram

关于python - 图像抽取后的异常直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com