Projection Profile Method in image processing in Python

In this tutorial, we will learn the implementation of the Projection profile method for image processing in Python. It refers to the process of projection of the sum of positives along with bi-directional image i.e grayscale image (two dimensional). This method is also used for the segmentation of the text documents based on words, sentences, and topics. This method is generally helpful in image analysis and some other similar use cases.

Projection profile method for image processing

Shall we start?

The projection profile basically converts the grayscale image to a thresholded image or binarized image. The pixel range of the grayscale image is between 0 to 255. Binarized images are the image with the pixel value of 0 replaced with 1 and a value of 255 with 0. Hence the modified image is called binarized image or thresholded image.

Here in this tutorial, we will implement this method on both the horizontal and vertical axis. In the horizontal axis, we add all the values of each row and return the output.

import numpy as np 
  
# Function to generate horizontal projection profile 
def getHorizontalProjectionProfile(image): 
  
    # Convert black spots to ones 
    image[image == 0]   = 1
    # Convert white spots to zeros 
    image[image == 255] = 0
  
    horizontal_projection = np.sum(image, axis = 1)  
  
    return horizontal_projection 
  
  
# Driver Function 
if __name__ == '__main__': 
  
    rows = 5
    cols = 3
    image = np.array([[0, 0, 0], 
            [0, 255, 255], 
            [0, 0, 0], 
            [0, 255, 255], 
            [0, 0, 0]]) 
      
    horizontal_projection = getHorizontalProjectionProfile(image.copy()) 
  
    print(*horizontal_projection)
3 1 3 1 3

Similarly, for the vertical axis, we will add all the row values of each column.

def getVerticalProjectionProfile(image): 
  
    # Convert black spots to ones  
    image[image == 0]   = 1
    # Convert white spots to zeros  
    image[image == 255] = 0
  
    vertical_projection = np.sum(image, axis = 0) 
  
    return vertical_projection 
  
# Driver Function 
if __name__ == '__main__': 
  
    rows = 5
    cols = 3
    image = np.array([[0, 0, 0], 
            [0, 255, 255], 
            [0, 0, 0], 
            [0, 255, 255], 
            [0, 0, 0]]) 
  
    vertical_projection = getVerticalProjectionProfile(image.copy()) 
  
    print(*vertical_projection)
5 3 3

Hence, we have successfully implemented the Projection profile method for image processing.

Leave a Reply

Your email address will not be published. Required fields are marked *