OpenCV错误:cv :: imshow中的断言失败(size.width> 0& size.height> 0) - Python

时间:2017-08-26 02:42:24

标签: python opencv

我通过了很多像我这样的类似主题,但没有找到我的代码的解决方案。首先,我只是想校准我的网络摄像头。

这是捕获图像的代码。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

ii = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    #conv = cv2.cvtColor(frame, cv2.COLOR_BGR2BGR)
    cv2.imshow('frame',frame)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
    elif k == ord('s'):
        ii += 1
        cv2.imwrite('framecap{}.png'.format(ii),frame)
        img = cv2.imread('framecap{}.png'.format(ii),1)
        cv2.imshow('frame capture',img)

cap.release()
cv2.destroyAllWindows()

这是校准的代码。

import numpy as np
import cv2
import glob

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

objpoints = []
imgpoints = []

images = glob.glob('*.png')

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    ret, corners = cv2.findChessboardCorners(gray, (7,6), None)

    if ret == True:
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners2)

        img2 = cv2.drawChessboardCorners(img, (7,6), corners2, ret)

        if not (type(img2) is np.ndarray):
            print 'None'
        else:
            cv2.imshow('img', img2)
            cv2.waitKey(500)

cv2.destroyAllWindows()

我仍然收到此错误:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow (Python)

我不认为这会解决我的问题吗?有人有解决方案吗?

祝你好运

1 个答案:

答案 0 :(得分:-1)

由于不正确使用imread()函数,总是会发生这种问题。 当您纠正imread()的语法时,错误将永远消失。