2015-05-14 133 views
2

我已经计算出了一个用于kinect深度图像的Otsu阈值,现在我想指出直方图上的最优阈值,例如在opencv2中使用pyplot的axvline。 我与Python和编程太初学者,这是我的代码的特定部分:直方图中的垂直线与pyplot

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

也许你应该呼吁''ax2'代替axvline'的调用'pyplot'方法? – xnx

+0

是的,我已经完成了ax2.axvline ...但我获得一条无形的线,一条白色的线,而不是红色的。为什么? – Giulio

回答

2

作品对我来说:

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