I want to write codes that can show the histogram of an image without using built in Matplotlib hist function.
Here is my codes:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def manHist(img):
row, col = img.shape # img is a grayscale image
y = np.zeros((256), np.uint64)
for i in range(0,row):
for j in range(0,col):
y[img[i,j]] += 1
x = np.arange(0,256)
plt.bar(x,y,color="gray",align="center")
plt.show()
def main():
img = cv.imread("C:/Users/Kadek/Documents/MATLAB/2GS.jpg")
manHist(img)
main()
My question is, is there a more efficent way to make an array of pixel value frequency without using for loop?
y[img[i,j]]?collections.Counterfor this, although that might not be "manual" enough for you.