【发布时间】:2017-10-29 17:24:24
【问题描述】:
问题:imshow() 不显示任何内容。
import cv2
import numpy as np
img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('opencv-template-for-matching.jpg',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
cv2.imshow('Detected',img_rgb)
另一个显示图像的示例也无法正常工作。无法退出,只显示静止画面,不显示视频。
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
cv2.startWindowThread()
while(1):
_, frame = cap.read()
cv2.imshow('frame',frame)
k = cv2.waitKey(0) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
我正在使用:
- python 3.6.1
- opencv 3.3.1
- Windows 10
【问题讨论】:
-
在第一个 sn-p 中缺少
cv2.waitKey(),而在第二个 sn-p 中应该是cv2.waitKey(1)