I'm working with a binary image in Python and I want to plot a histogram that shows/returns the number of black pixels per row, not the total number. So far this is not working:
hist = cv2.calcHist([binary_image], [0], None, [height], [0, weight])
plt.title("Histogram")
plt.plot(hist)
plt.xlim([0,weight])
plt.show()
I did it in MATLAB and this is works fine
im_hist = hist(t_image, 2);
plot(1:size(im_hist,2),im_hist(2,:))
Assuming that binary_image
is a 2D array, this will work:
counts = np.sum(binary_image==0, axis=1)
plt.plot(counts)
Your MATLAB function, however, does not do what you describe in a general way. It divides your data into two ranges, then counts how many data points fall into each range. This will only work properly if your image only has two levels. If not, then your "black" will include values other than black. The equivalent python function is this is:
counts = np.sum(binary_image<binary_image.max()/2., axis=1)
plt.plot(counts)