我正在使用Python中的二进制图像,我想绘制一个直方图,显示/返回每行的黑色像素数,而不是总数。到目前为止,这不起作用:
hist = cv2.calcHist([binary_image], [0], None, [height], [0, weight])
plt.title("Histogram")
plt.plot(hist)
plt.xlim([0,weight])
plt.show()
我是在MATLAB中做到的,这很好用
im_hist = hist(t_image, 2);
plot(1:size(im_hist,2),im_hist(2,:))
答案 0 :(得分:0)
假设binary_image
是一个2D数组,这将起作用:
counts = np.sum(binary_image==0, axis=1)
plt.plot(counts)
然而,您的MATLAB函数不能以一般方式执行您所描述的内容。它将您的数据划分为两个范围,然后计算每个范围中有多少数据点。只有当您的图像只有两个级别时,这才能正常工作。如果没有,那么你的"黑色"将包括黑色以外的值。等效的python函数是:
counts = np.sum(binary_image<binary_image.max()/2., axis=1)
plt.plot(counts)