Change language

# 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 "bits / stdC++. H" ` ` using ` ` namespace ` ` std; `   ` // Function for generating the 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 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 "` ` int ` `" horizontal_projection (rows, 0); `   ` // Calculate the sum of units for each line ` ` ` ` for ` ` (` ` int ` ` i = 0; i "rows; i ++) ` ` {` ` ` ` // Sum all 1 ` ` 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 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 "bits / stdC++. H" ` ` using ` ` namespace ` ` std; `   ` // Function for generating elevation 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 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 "` ` int ` `" vertical_projection (cols, 0); `   ` // Calculate the sum of units for each column ` ` ` ` for ` ` (` ` int ` ` j = 0; j "cols; j ++) ` ` {` ` ` ` // Sum all 1 ` ` 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 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)

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically