使用 for 循环创建图像(灰度)直方图

时间:2021-01-22 08:45:05

标签: python numpy

img = cv.imread('test.jpg')

    # displays the image
    cv2_imshow(img) 

    # initialize the array to store histogram
    h = np.zeros(256, np.int32)
    print(h)
    # get the row and column of the image
    row, col = img.shape[0], img.shape[1]

    # creates the histogram of the image
    for i in range(0, row):
      for j in range(0, col):
        h[img[i, j]] += 1
    plt.figure()
    plt.plot(h)
    plt.show()

this is the plot I get. it not correct this is how it suppose to look like

第一张图片是我使用 for 循环得到的直方图,第二张是我使用 numpy histogram 函数得到的,这是应该得到的。 我的问题是,代码有什么问题。有关如何解决此问题的任何提示? 提前致谢

0 个答案:

没有答案
相关问题