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

时间:2021-01-28 11:06:12

标签: python opencv computer-vision opencv3.0 opencv-python

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

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

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

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

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

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

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

输入图片:
Input Image

垂直直方图投影
enter image description here

去除乘数后*width/height

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

新的直方图投影
Top to Bottom

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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题