Streem
Streem

Reputation: 628

How do you divide an image/array into blocks?

I am wondering is it possible to divide image into blocks for example 8x8 blocks (64 pixels per block) and perform histogram function for each block and save results into a new image not to separately images?

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)

Upvotes: 1

Views: 4158

Answers (1)

alkasm
alkasm

Reputation: 23032

Why not loop through all 8x8 blocks in the image?

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

Upvotes: 7

Related Questions