In Image Processing, projection profile refers to projection of sum of hits/positives along an axis from bi-dimensional image. Projection profile method is majorly used for segmentation of text objects present inside text documents.
Solution:
Note: Projection profile is calculated for a thresholded image or binarized image where a thresholded image is a grayscale image with pixel values as 0 or 255. Image pixels are replaced by 1 and 0 for pixel values 0 and 255 respectively.
Projection profile is calculated separately for different axis. Projection profile along vertical axis is called Vertical Projection profile. Vertical projection profile is calculated for every column as sum of all row pixel values inside the column. Horizontal Projection profile is the projection profile of a image along horizontal axis. Horizontal Projection profile is calculated for every row as sum of all column pixel values inside the row.
Code Implementation for Horizontal Projection Profile:
C++
#include <bits/stdc++.h> using namespace std; // Function to generate horizontal projection profile vector< int > getHorizontalProjectionProfile( vector<vector< int > > image, int rows, int cols) { for ( int i = 0; i < rows; i++) { for ( int j = 0; j < cols; j++) { // Convert black spots to ones if (image[i][j] == 0) { image[i][j] = 1; } // Convert white spots to zeros else if (image[i][j] == 255) { image[i][j] = 0; } } } vector< int > horizontal_projection(rows, 0); // Calculate sum of 1's for every row for ( int i = 0; i < rows; i++) { // Sum all 1's for ( int j = 0; j < cols; j++) { horizontal_projection[i] += image[i][j]; } } return horizontal_projection; } // Driver Function int main() { int rows = 5, cols = 3; vector<vector< int > > image = { { 0, 0, 0 }, { 0, 255, 255 }, { 0, 0, 0 }, { 0, 255, 255 }, { 0, 0, 0 } }; vector< int > horizontal_projection = getHorizontalProjectionProfile( image, rows, cols); for ( auto it : horizontal_projection) { cout << it << " " ; } return 0; } |
Python3
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
Code Implementation for Vertical Projection Profile:
C++
#include <bits/stdc++.h> using namespace std; // Function to generate vertical projection profile vector< int > getVerticalProjectionProfile( vector<vector< int > > image, int rows, int cols) { for ( int i = 0; i < rows; i++) { for ( int j = 0; j < cols; j++) { // Convert black spots to ones if (image[i][j] == 0) { image[i][j] = 1; } // Convert white spots to zeros else if (image[i][j] == 255) { image[i][j] = 0; } } } vector< int > vertical_projection(cols, 0); // Calculate sum of 1's for every column for ( int j = 0; j < cols; j++) { // Sum all 1's for ( int i = 0; i < rows; i++) { vertical_projection[j] += image[i][j]; } } return vertical_projection; } // Driver Function int main() { int rows = 5, cols = 3; vector<vector< int > > image = { { 0, 0, 0 }, { 0, 255, 255 }, { 0, 0, 0 }, { 0, 255, 255 }, { 0, 0, 0 } }; vector< int > vertical_projection = getVerticalProjectionProfile( image, rows, cols); for ( auto it : vertical_projection) { cout << it << " " ; } return 0; } |
Python3
import numpy as np # Function to generate vertical projection profile 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
Time Complexity: O(rows*columns)
Space Complexity: O(rows*columns)