6

i've computed an Otsu's thresholding for a kinect depth image and now i want point out the optimal thresholding value on the histogram, using for example axvline with pyplot in opencv2. I'm a beginner with python and programming too, this is the specific part of my code:

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()

but i don't know why, i obtain the vertical line on the thresholded plot

what am i doing wrong?? thanks

Giulio
  • 83
  • 1
  • 2
  • 7
  • 1
    Perhaps you should be calling `axvline` on `ax2` instead of calling the `pyplot` method? – xnx May 14 '15 at 17:25
  • yes i've done ax2.axvline... but i obtain an invisible line, a white line instead red..why? – Giulio May 14 '15 at 18:31

1 Answers1

10

works for me:

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

Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42