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

后端 未结 5 635
北恋
北恋 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:04

    To divide a square image into square blocks (same number of blocks per axis), I used this method (full repository here):

    def blockDivide(img, blockNumber):
    
        imgArray = np.array(Image.open(img))
    
        # Define dimension of image
        dimension = imgArray.shape[0]
    
        # Set number of slices per axis
        axisSlice = int(math.sqrt(blockNumber))
    
        # Size of each block
        arraySize = int(dimension / axisSlice)
    
        # Shape of numpy array to be filled
        blocksArray = np.zeros((arraySize, arraySize, blockNumber))
    
        # Split the image into vertical blocks
        split_a = np.split(imgArray, axisSlice, axis = 0)
    
        # Set counter to zero
        counter = 0
    
        for i in range(axisSlice):
            for j in range(axisSlice):
    
                # Split vertical blocks into square blocks
                split_b = np.split(split_a[i], axisSlice, axis = 1)
    
                # Fill array with blocks
                blocksArray[:, :, counter] = split_b[j]
    
                # Increase counter
                counter += 1
    
        return blocksArray
    

提交回复
热议问题