问:将图像划分为块Python

时间:2017-06-18 12:51:26

标签: python numpy

我想知道是否可以将图像划分为块,例如8x8块(64 pixels per block),并为每个块执行直方图功能,并将结果保存到新图像中,而不是单独存储图像?

def apply_histogram(block):
    h, b = np.histogram(block.flatten(), 256, normed=True)
    cdf = h.cumsum()
    cdf = 255 * cdf / cdf[-1]
    return np.interp(block.flatten(), b[:-1], cdf).reshape(block.shape)

1 个答案:

答案 0 :(得分:2)

为什么不循环遍历图像中的所有8x8块?

image = ...
block_img = np.zeros(image.shape)
im_h, im_w = image.shape[:2]
bl_h, bl_w = 8, 8

for row in np.arange(im_h - bl_h + 1, step=bl_h):
    for col in np.arange(im_w - bl_w + 1, step=bl_w):
        block_img[row:row+bl_h, col:col+bl_w] = apply_histogram(image[row:row+bl_h, col:col+bl_w])

image

Cameraman image

block_img

Histogram applied to blocks