python 3.6.1 opencv 3.3.1 cv2.imshow()不显示图像

时间:2017-10-29 17:24:25

标签: python opencv

问题: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

1 个答案:

答案 0 :(得分:0)

您需要为windowopen留出时间,并为closing提供方法。

使用openCV执行此操作的常用方法是使用.waitKey(n)。此函数将halt execution program n milliseconds 0 infinite user}或key 1}}按snippet

因此,在第一个snippet中,您只需要在末尾添加以下行:

cv2.waitKey(0)

在第二个line中,您需要更改k = cv2.waitKey(0) & 0xFF

k = cv2.waitKey(1) & 0xFF

loop

以便run可以继续0(因为import Crypto.Types.PubKey.ECDSA (Signature(..), PrivateKey(..), PublicKey(..)) import Crypto.Types.PubKey.ECC (Curve(CurveFP), CurvePrime(..), CurveCommon(..), Point(..)) import Codec.Crypto.ECC.Base (modinv) import Crypto.Random (genBytes, GenError, CryptoRandomGen) import Crypto.Util (bs2i, i2bs, i2bs_unsized) import ECDSA.Util Could not find module ‘Crypto.Types.PubKey.ECC’ Use -v to see a list of the files searched for. 等待永远)。