【问题标题】:Stuck with the issue: src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'遇到问题: src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'
【发布时间】:2021-08-07 21:34:42
【问题描述】:

我知道这个问题还有其他帖子,但没有一个对我有帮助:

import cv2 as cv
import numpy as np

widthImg = 640
heightImg = 480

frameWidth = 640
frameHeight = 480
cap = cv.VideoCapture(2)
cap.set(3, widthImg)
cap.set(4, heightImg)
cap.set(10,150)


def preProcessing(img):
    imgGray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    imgBlur = cv.GaussianBlur(imgGray,(5,5),1)
    imgCanny = cv.Canny(imgBlur,200,200)
    kernel = np.ones((5,5))
    imgDial = cv.dilate(imgCanny,kernel,iterations=2)
    imgThres = cv.erode(imgDial,kernel,iterations=1)
    return imgThres

def getContours(img):
    biggest = np.array([])
    maxArea = 0
    contours,hierarchy = cv.findContours(img,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
    for cnt in contours:
        area = cv.contourArea(cnt)
        if area>5000:
            #cv.drawContours(imgContour, cnt, -1, (255,0,0), 3)
            peri = cv.arcLength(cnt,True)
            approx = cv.approxPolyDP(cnt,0.02*peri,True)
            if area >maxArea and len(approx) == 4:
                biggest = approx
                maxArea = area
    cv.drawContours(imgContour, biggest, -1, (255, 0, 0), 20)
    return biggest

def getWarp(img,biggest):

    pts1 = np.array(biggest,np.float32)
    pts2 = np.array([[0, 0], [widthImg, 0], [0, heightImg], [widthImg, heightImg]],np.float32)
    matrix = cv.getPerspectiveTransform(pts1, pts2)
    imgOutput = cv.warpPerspective(img, matrix, (widthImg, heightImg))

    return imgOutput


while True:
    success, img = cap.read()
    img = cv.resize(img,(widthImg,heightImg))
    imgContour = img.copy()


    imgThres = preProcessing(img)
    biggest = getContours(imgThres)
    print(biggest)
    imgWarped = getWarp(img,biggest)


    cv.imshow("Result", imgWarped)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

这是我在 Pycharm 中的代码。我知道当我在第 44 行使用“最大”时出现问题(pts1 = np.array(biggest,np.float32) 我知道如果我写 4 个二维点而不是“最大”它会起作用。但是在我正在关注的视频,程序员使用“最大”没有任何问题。我知道它已经说过“最大”有 4 分(在 def getContours(img):) 中。我不知道为什么不工作在我的情况下,在其他情况下是的。

这是我收到的错误消息:

Traceback (most recent call last):
  File "/home/nvidia/PycharmProjects/OpencvPython/Resources/Project2.py", line 61, in <module>
    imgWarped = getWarp(img,biggest)
  File "/home/nvidia/PycharmProjects/OpencvPython/Resources/Project2.py", line 46, in getWarp
    matrix = cv.getPerspectiveTransform(pts1, pts2)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-q3gzfcr4/opencv/modules/imgproc/src/imgwarp.cpp:3392: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'

对不起,如果这是一个愚蠢的问题,但我是 python 编程的新手,我花了几个小时试图解决它。

【问题讨论】:

  • 如果没有面积超过 5000 的轮廓,则 biggest 将为空,并且您的代码中没有任何内容可以阻止它使用此类无效输入调用 getPerspectiveTransform
  • 在旁注中,您错误地使用了drawContours。第二个参数应该是一个轮廓列表,但你只给它一个轮廓(点列表),所以它最终只会画一堆点。见stackoverflow.com/q/36311398/3962537
  • @DanMašek 你在这个地区是对的。该死的,我没有注意到,只要我将 5000 更改为 500,它就开始工作了

标签: python numpy opencv pycharm


【解决方案1】:

使用这就是我解决它的方法。

import cv2
import numpy as np

cap = cv2.VideoCapture(1)
#widht id:3, height id:4, brightness id:10.
cap.set(3,640)
cap.set(4,480)
width,height = 640,480
def preprocessing(img):
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgBlur = cv2.GaussianBlur(img,(5,5),1)
    imgCanny = cv2.Canny(imgBlur,150,150)
    kernel = np.ones((5,5))
    imgDial = cv2.dilate(imgCanny,kernel,iterations=2)
    imgErode = cv2.erode(imgDial,kernel,iterations=1)

    return imgErode

def contour(img):
    biggest = np.array([])
    maxArea = 0
    contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 5000:
            #cv2.drawContours(imgcontour,cnt,-1,(255,0,0),3)
            peri = cv2.arcLength(cnt,True)
            approx = cv2.approxPolyDP(cnt,0.02*peri,True)
            if area > maxArea and len(approx) == 4:
                biggest = approx
                maxArea = area
    cv2.drawContours(imgcontour,biggest,-1,(255,0,0),20)
    return biggest

def reorder(myPoints):
    pass

def warp(img,biggest):
    pts1 = np.float32(biggest)
    pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    imgout = cv2.warpPerspective(img,matrix,(width,height))
    return imgout

while True:
    success, img = cap.read()
    cv2.resize(img,(width,height))
    imgcontour = img.copy()
    imgThres = preprocessing(img)
    biggest = contour(imgThres)
    print(biggest)
    if biggest.size != 0:
        imgwarp = warp(img,biggest)
        cv2.imshow('video', imgwarp)
    else:
        cv2.imshow('video',imgThres)
    if cv2.waitKey(1) & 0xFF == 27:
        break

【讨论】:

    猜你喜欢
    • 2017-10-08
    • 2020-06-16
    • 2022-10-17
    • 2019-11-11
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多