Projection Profile method
Last Updated :
25 Jun, 2019
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;
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++)
{
if (image[i][j] == 0)
{
image[i][j] = 1;
}
else if (image[i][j] == 255)
{
image[i][j] = 0;
}
}
}
vector< int > horizontal_projection(rows, 0);
for ( int i = 0; i < rows; i++)
{
for ( int j = 0; j < cols; j++)
{
horizontal_projection[i] += image[i][j];
}
}
return horizontal_projection;
}
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
def getHorizontalProjectionProfile(image):
image[image = = 0 ] = 1
image[image = = 255 ] = 0
horizontal_projection = np. sum (image, axis = 1 )
return horizontal_projection
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)
|
Code Implementation for Vertical Projection Profile:
C++
#include <bits/stdc++.h>
using namespace std;
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++)
{
if (image[i][j] == 0)
{
image[i][j] = 1;
}
else if (image[i][j] == 255)
{
image[i][j] = 0;
}
}
}
vector< int > vertical_projection(cols, 0);
for ( int j = 0; j < cols; j++)
{
for ( int i = 0; i < rows; i++)
{
vertical_projection[j] += image[i][j];
}
}
return vertical_projection;
}
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
def getVerticalProjectionProfile(image):
image[image = = 0 ] = 1
image[image = = 255 ] = 0
vertical_projection = np. sum (image, axis = 0 )
return vertical_projection
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)
|
Time Complexity: O(rows*columns)
Space Complexity: O(rows*columns)
Share your thoughts in the comments
Please Login to comment...