Divide an image into 5x5 blocks in python and compute histogram for each block

后端 未结 5 599
北恋
北恋 2020-12-28 19:32

Using Python, I have to:

  • Divide a Test_Image and Reference_image into 5x5 blocks,
  • Compute a histogram for each block, and
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 20:23

    This worked for me. It has the ability to divide into n*m chunks. Pad your image accordingly.

    def chunkify(img, block_width=4, block_height=4):
      shape = img.shape
      x_len = shape[0]//block_width
      y_len = shape[1]//block_height
    
      chunks = []
      x_indices = [i for i in range(0, shape[0]+1, block_width)]
      y_indices = [i for i in range(0, shape[1]+1, block_height)]
    
      shapes = list(zip(x_indices, y_indices))
    
      for i in range(len(shapes)):
          try:
            start_x = shapes[i][0]
            start_y = shapes[i][1]
            end_x = shapes[i+1][0]
            end_y = shapes[i+1][1]
            chunks.append( shapes[start_x:end_x][start_y:end_y] )
          except IndexError:
            print('End of Array')
    
      return chunks
    

    https://github.com/QuantumNovice/ImageProcessing/blob/master/image_chunkify.py

提交回复
热议问题