计算MNIST数据库灰度图像的密度



使用此函数:

import numpy as np
def blockshaped(arr, nrows, ncols):
''' Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
'''
h, w = arr.shape
assert h % nrows == 0, "{} rows is not evenly divisble by {}".format(h, nrows)
assert w % ncols == 0, "{} cols is not evenly divisble by {}".format(w, ncols)
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1, 2)
.reshape(-1, nrows, ncols))

我能够将我的图像分成16像素的块。我要做的是计算每个块中黑色像素的密度。我知道像素的值从0到255。我想做black_density = numberof_zeros / 16,但我不确定。

如果你想知道图像中每个块的黑色密度,你只需输入

np.sum(block_shaped(img, 16, 16).reshape(-1, 16*16) <= black_threshold, axis=1)

如果你不确定阈值,你可以尝试Otsu方法,无论是块还是整个图像,这取决于你真正想要的。

最新更新