cv2.imshow()和plt.show()不能一起使用Python opencv

cv2.imshow()和plt.show()不能一起使用Python opencv,python,opencv,Python,Opencv,我有这个剧本: import numpy as np import cv2 from matplotlib import pyplot as plt im = cv2.imread('sun0016.bmp') height, width, depth = im.shape print height, width, depth thresh = 132 imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(i

我有这个剧本:

import numpy as np
import cv2
from matplotlib import pyplot as plt


im = cv2.imread('sun0016.bmp')
height, width, depth = im.shape
print height, width, depth
thresh = 132
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray,(5,5),0)
edges = cv2.Canny(blur,thresh,thresh*2)
contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
cv2.drawContours(im,contours,-1,(0,255,0),-1)

#centroid_x = M10/M00 and centroid_y = M01/M00
M = cv2.moments(cnt)
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
print x,y
print width/2.0,height/2.0
print width/2-x,height/2-y


cv2.circle(im,(x,y),1,(0,0,255),2)
cv2.putText(im,"x[px]    y[px]",(10,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,255))
cv2.putText(im,"center of Sun",(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.putText(im,str(x)+","+str(y),(10,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.circle(im,(width/2,height/2),1,(255,0,0),2)
cv2.putText(im,"center of image",(width/2,height/2), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,str(width/2)+","+str(height/2), (10,150), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,"difference:"+str(width/2-x)+","+str(height/2-y),(400,50),cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,255))

cv2.imshow('contour',im)

plt.hist(im.ravel(),255,[0,255])
plt.grid(True)

cv2.waitKey(0)
plt.show()
现在我想同时看到两个窗口,但不知道怎么做。有什么想法吗?现在当我运行脚本时,我首先得到窗口“轮廓”,然后关闭这个窗口,我得到窗口“直方图”


谢谢

倒转最后两行就行了?不,是一样的。我试过也许这会有帮助: