How to draw vertical histogram projection on x axis?

Viewed 721

Here is the example of image histogram horizontal projection from Article

for row in range(height):
    cv2.line(blankImage, (0,row), (int(horizontal_projection[row]*width/height),row), (255,255,255), 1)

I have created vertical projection accordingly:

vertical_projection = np.sum(binarizedImage, axis=0);

And changed the for loop to project values on blank Image

for col in range(width):
cv2.line(blankImage, (col,0), (col, int(myprojection[col]*width/height)),  (255,255,255), 1)

But the code does not produce expected result.

Input image:
Input Image

Vertical Histogram Projection
enter image description here

after removing the multiplier *width/height

    for col in range(width):
    cv2.line(blankImage, (col,0), (col, int(myprojection[col])),  (255,255,255), 1)

New Histogram Projection
Top to Bottom

Could you please advice how this for loop can be converted to draw vertical histogram projections on x-axis, bottom-up and scaled?

1 Answers

You can use this simpler approach

import cv2
import numpy as np

image = cv2.imread("image.png")
cv2.imshow("image", image)

height, width, _ = image.shape

gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold_image = cv2.threshold(gray_scale, 0, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("threshold_image", threshold_image)

vertical_pixel_sum = np.sum(threshold_image, axis=0)
myprojection = vertical_pixel_sum / 255

blankImage = np.zeros_like(image)
for i, value in enumerate(myprojection):
    cv2.line(blankImage, (i, 0), (i, height-int(value)), (255, 255, 255), 1)

cv2.imshow("New Histogram Projection", blankImage)

cv2.waitKey(0)

enter image description here

Related