【问题标题】:Calculating the density of an MNIST database grayscale image计算 MNIST 数据库灰度图像的密度
【发布时间】:2021-05-23 14:08:29
【问题描述】:

使用此功能:

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,但我不确定。

【问题讨论】:

    标签: python image numpy pixel


    【解决方案1】:

    好吧,如果你想知道黑色的密度,你只需在图像的每个块中做

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2021-06-01
      • 2019-07-10
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多