# Projection profile method

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.

The projection profile is calculated separately for different axes. The projection profile along the vertical axis is called the elevation profile. The elevation profile is calculated for each column as the sum of all the pixel values ​​of the row within the column. Horizontal projection profile — this is the projection profile of the image along the horizontal axis. The ground profile is calculated for each row as the sum of all the pixel values ​​of the column within the row.

Code implementation for the ground profile:

C++

 ` # include & lt; bits / stdC++. H & gt; ` ` using ` ` namespace ` ` std; `   ` // Function for generating the horizontal projection profile ` ` vector & lt; ` ` int ` ` & gt; getHorizontalProjectionProfile (` ` vector & lt; vector & lt; ` ` int ` ` & gt; & gt; image, ` ` int ` ` rows, ` ` int ` ` cols) ` ` {`   ` for ` ` (` ` int ` ` i = 0; i & lt; rows; i ++) ` ` {` ` for ` ` (` ` int ` ` j = 0; j & lt; cols; j ++) ` ` { ` ` // Convert black spots to one ` ` 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 & lt; ` ` int ` ` & gt; horizontal_projection (rows, 0); `   ` // Calculate the sum of units for each line ` ` ` ` for ` ` (` ` int ` ` i = 0; i & lt; rows; i ++) ` ` {` ` ` ` // Sum all 1 ` ` for ` ` (` ` int ` ` j = 0; j & lt; cols; j ++) ` ` {` ` ` ` horizontal_projection [i] + = image [i] [j]; ` `} ` `} ` ` `  ` return ` ` horizontal_projection; ` `} ` ` // Driver function ` ` int ` ` main () ` ` {` ` int ` ` rows = 5, cols = 3; ` ` vector & lt; vector & lt; ` ` int ` ` & gt; & gt; image = {{0, 0, 0}, ` ` {0, 255, 255} , ` ` {0, 0, 0}, ` ` {0, 255, 255}, ` ` ` ` {0, 0, 0} ` `}; `   ` vector & lt; ` ` int ` ` & gt; horizontal_projection = getHorizontalProjectionProfile (` ` image, rows, cols); `   ` for ` ` (` ` auto ` ` it: horizontal_projection) ` ` ` ` {` ` ` ` cout & lt; & lt; it & lt; & lt; ` ` "" ` `; ` `} ` ` return ` ` 0; ` `} `

python3

 ` import ` ` numpy as np `   ` # Function for creating a horizontal projection profile ` ` def ` ` getHorizontalProjectionProfile (image): `   ` # Convert black spots to one ` ` 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) `

Output :

` 3 1 3 1 3 `

Code implementation for elevation profile:

C++

 ` # include & lt; bits / stdC++. H & gt; ` ` using ` ` namespace ` ` std; `   ` // Function for generating elevation profile ` ` vector & lt; ` ` int ` ` & gt; getVerticalProjectionProfile (` ` vector & lt; vector & lt; ` ` int ` ` & gt; & gt; image, ` ` int ` ` rows, ` ` int ` ` cols) ` ` {`   ` for ` ` (` ` int ` ` i = 0; i & lt; rows; i ++) ` ` {` ` for ` ` (` ` int ` ` j = 0; j & lt; cols; j ++) ` ` { ` ` ` ` // Convert black spots to one ` ` 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 & lt; ` ` int ` ` & gt; vertical_projection (cols, 0); `   ` // Calculate the sum of units for each column ` ` ` ` for ` ` (` ` int ` ` j = 0; j & lt; cols; j ++) ` ` {` ` ` ` // Sum all 1 ` ` for ` ` (` ` int ` ` i = 0; i & lt; rows; i ++) ` ` {` ` ` ` vertical_projection [j] + = image [i] [j]; ` `} ` `} ` ` `  ` return ` ` vertical_projection; ` `} `   ` // Driver function ` ` int ` ` main () ` ` {` ` int ` ` rows = 5, cols = 3; ` ` vector & lt; vector & lt; ` ` int ` ` & gt; & gt; image = {{0, 0, 0}, ` ` {0, 255, 255} , ` ` {0, 0, 0}, ` ` {0, 255, 255}, ` ` ` ` {0, 0, 0} ` `}; `   ` vector & lt; ` ` int ` ` & gt; vertical_projection = getVerticalProjectionProfile (` ` image, rows, cols); `   ` for ` ` (` ` auto ` ` it: vertical_projection) ` ` ` ` {` ` ` ` cout & lt; & lt; it & lt; & lt; ` ` "" ` `; ` `} ` ` return ` ` 0; ` `} `

python3

 ` import ` ` numpy as np `   ` # Function for generating elevation profile ` ` def ` ` getVerticalProjectionProfile (image): `   ` # Convert black spots to one ` ` 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) `

Exit :

` 5 3 3 `

Time complexity : O (rows * columns)
Space complexity : O (rows * columns)