1
import cv2
import numpy as np
import matplotlib.pyplot as plt

def my_histogram(img, bins, range):
    hist = 
    return hist

I need to fill out hist without using np.histogram or other histogram function

img = cv2.imread('sample.jpg')
img_g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = my_histogram(img_g, 256, [0,256])

plt.plot(hist)
plt.xlim([0,256])
plt.show()
OTG JJJ
  • 13
  • 2

2 Answers2

0

np.unique is a less efficient histogram utility, since it sorts the data instead of processing it directly:

bins, hist = np.unique(img.ravel(), return_counts=True)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

I won't provide you with the exact answer since this looks like a homework. Basically each pixel in your grayscale image has a number associated to it that range from 0 to 255. What you want to do is sum the number of pixels that have the same value (how many have the value 0, how many have the value 1, how many have the value 2, etc.). You probably can use numpy to help you without calling the histogram function (take a look at this link for example).

You can then plot you bar graph with matplotlib, to show your histogram.

Luke B
  • 1,143
  • 1
  • 11
  • 22