如何在 x 轴上绘制垂直直方图投影?

问题描述

这里是来自Article的图像直方图水平投影示例

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

我相应地创建了垂直投影:

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

并将 for 循环更改为在空白图像上投影值

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

但是代码没有产生预期的结果。

输入图片

Input Image

垂直直方图投影

enter image description here

去除乘数后*width/height

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

新的直方图投影

Top to Bottom


您能否建议如何将此 for 循环 转换为在 x 轴上、自下而上和按比例绘制垂直直方图投影?

解决方法

您可以使用这种更简单的方法

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,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),height-int(value)),(255,255),1)

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

cv2.waitKey(0)

enter image description here