Reputation: 61
I have this simple OpenCV code to plot histogram of an Image.
from PIL import Image
from numpy import *
from pylab import *
import cv2
image=cv2.imread('/media/755784/New Volume/DATA/Images/opencv.jpg')
h,w=image.shape[:2]
cv2.imwrite('/media/755784/New Volume/DATA/Images/result.png',image)
print h,w
cv2.imshow("Image",image)
cv2.waitKey(0)## <--
print 'Plotting histogram'
hist=cv2.calcHist(image,[0],None,[256],[0,256])
plt.hist(image.ravel(),256,[0,256])
plt.show()
while True:
k=cv2.waitKey(30)
if k==27:
break
cv2.destroyAllWindows()
When I remove the waitKey(0) after the imshow(), the histogram gets plotted first and unless you close the plot, the image is not displayed. Is this a particular problem with python or matplotlib?
Upvotes: 2
Views: 3807
Reputation: 12711
Your plt.show()
blocks the code, so you don't get to the while loop.
But you can plot the image also with matplotlib. That makes it easier:
import matplotlib.pyplot as plt
import cv2
image=cv2.imread('/tmp/stinkbug.png')
fig = plt.figure(figsize=(10,3))
ax1 = plt.subplot(1,2,1)
ax1.imshow(image)
ax2 = plt.subplot(1,2,2)
ax2.hist(image.ravel(),256,[0,256])
plt.show()
(Note: I used a different image)
Upvotes: 3