直方图中的垂直线与pyplot

时间:2015-05-14 16:52:18

标签: python-2.7 matplotlib histogram threshold

我为kinect深度图像计算了Otsu的阈值,现在我想指出直方图上的最佳阈值,例如在opencv2中使用带有pyplot的axvline。 我是python和编程的初学者,这是我的代码的特定部分:

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 2.5))
ax1.imshow(img)
ax1.set_title('Original')
ax1.axis('off')

ax2.hist(img)
ax2.set_title('Histogram')
plt.axvline(x=thresh, color='r', linestyle='dashed', linewidth=2)

ax3.imshow(binary, cmap=plt.cm.gray)
ax3.set_title('Thresholded')
ax3.axis('off')

plt.show()

但我不知道为什么,我在阈值图上获得垂直线

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:6)

适合我:

from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
f = r"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"
img = imread(f)[:, :, 0]
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 2.5))
ax1.imshow(img)
ax1.set_title('Original')
ax1.axis('off')

thresh = 100
ax2.hist(img)
ax2.set_title('Histogram')
ax2.axvline(x=thresh, color='r', linestyle='dashed', linewidth=2)

ax3.imshow(img, cmap=plt.cm.gray)
ax3.set_title('Thresholded')
ax3.axis('off')

plt.show()

enter image description here

相关问题