1

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)
alkasm
  • 22,094
  • 5
  • 78
  • 94
Streem
  • 628
  • 2
  • 12
  • 26
  • Possible duplicate of [How to Split Image Into Multiple Pieces in Python](https://stackoverflow.com/questions/5953373/how-to-split-image-into-multiple-pieces-in-python) – moritzg Jun 18 '17 at 12:54
  • I do not want to split image. And I do not want to apply function to image at once, I want to apply function in image to every 8x8 block. – Streem Jun 18 '17 at 13:02

1 Answers1

7

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

alkasm
  • 22,094
  • 5
  • 78
  • 94