OpenCV-(-215:Assertion failed) _src.total() >函数 'cv::warpPerspective' 中的 0 [英] OpenCV-(-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

查看:210
本文介绍了OpenCV-(-215:Assertion failed) _src.total() >函数 'cv::warpPerspective' 中的 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的完整代码:

将 cv2 导入为 cv将 numpy 导入为 np帽 = cv.VideoCapture(0 + cv.CAP_DSHOW)imgTarget = cv.imread('photos\TargetImage.jpg') #bu resmimizmyVid = cv.VideoCapture('照片\视频.mp4')检测=假帧计数器 = 0imgVideo = cap.read()成功,imgVideo = myVid.read()hT,wT,cT = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.'''而 myVid.isOpened():成功,视频 = myVid.read()如果成功:Video = cv.resize(Video, (wT, hT))'''#burada 关键点 yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.orb = cv.ORB_create(nfeatures=1000)kp1, des1 = orb.detectAndCompute(imgTarget,None)while True:#burada 网络摄像头 ile fotoğrafları birleştircez.成功,imgWebcam = cap.read()imgAug = imgWebcam.copy()#burada 关键点 yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.kp2, des2 = orb.detectAndCompute(imgWebcam, None)#imgwebcami myvid yapıp değiştir o zaman oldu .d tek seferliktir belki?# imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)如果检测==假:myVid.set(cv.CAP_PROP_POS_FRAMES,0)帧计数器 = 0别的:如果 frameCounter == myVid.get(cv.CAP_PROP_FRAME_COUNT):myVid.set(cv.CAP_PROP_POS_FRAMES, 0)帧计数器 = 0成功,imgVideo = myVid.read()imgVideo = cv.resize(imgVideo, (wT, hT))#burada ise 匹配 yani 网络摄像头 resmi ile fotoğrafımız arasındaki benzerlikleri alıyoruz.bf = cv.BFMatcher()匹配 = bf.knnMatch(des1,des2,k=2)好 =[]对于匹配中的 m,n:如果 m.distance <0.75 *n.距离:good.append(m)打印(len(好))imgFeatures = cv.drawMatches(imgTarget,kp1,imgWebcam,kp2,good,None,flags=2)如果 len(good) >20: #burada eğer anahtar bölgelerimiz 20 den fazla şekilde uyuşuyorsa, tanımlama(detection) tamamlanmış oluyor.检测 = 真srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)矩阵,掩码 = cv.findHomography(srcPts,dstPts,cv.RANSAC,5)打印(矩阵)#eşleşine resmin etrafını çiziyoruzpts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2)dst = cv.perspectiveTransform(pts,matrix)img2 = cv.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3)#burada videomuzu,网络摄像头 resmimiz ile aynı boyuta getiriyoruz.imgWarp = cv.warpPerspective(imgVideo,matrix, (imgWebcam.shape[1],imgWebcam.shape[0]))

.....大家好,我正在做一个项目.在我的代码中,我尝试使用 warpPerspective 参数.但在那一行中,它给出了以下错误:

<块引用>

错误:OpenCV(4.0.1)C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\imgwarp.cpp:2903:错误:(-215:断言失败)_src.total() >0 功能'cv::warpPerspective'

这是我的相关代码错误:

imgWarp = cv.warpPerspective(imgVideo,matrix, (imgWebcam.shape[1],imgWebcam.shape[0]))

我还将我的所有代码复制到我的页面顶部,因为它可能被需要.

我的网络摄像头有问题吗?但它适用于其他代码,或者也适用于应用程序.为什么会出现这个错误,我该如何解决?

我正在等待您的帮助...任何建议都将有助于解决此问题.

解决方案

让我先对你的代码做一个小改动.

当您使用 \ 分隔符进行初始化时,您的代码将仅适用于 Windows.

imgTarget = cv.imread('photos\TargetImage.jpg') #bu resmimizmyVid = cv.VideoCapture('照片\视频.mp4')

如果你使用 os.pathsep,它将适用于所有操作系统.

from os.path import sepimgTarget = cv.imread('photos' + sep + 'TargetImage.jpg') #bu resmimizmyVid = cv.VideoCapture('照片' + sep + 'video.mp4')

我建议您使用默认分辨率并将 cap 变量初始化为:

cap = cv.VideoCapture(0)

您还可以使用 imshow

显示 imgWarp

cv.imshow("imgWarp", imgWarp)cv.waitKey(0)

示例输出:

代码:


将 cv2 导入为 cv将 numpy 导入为 np从 os.path 导入 sep帽 = cv.VideoCapture(0)imgTarget = cv.imread('photos' + sep + 'TargetImage.jpg') # bu resmimizmyVid = cv.VideoCapture('照片' + sep + 'video.mp4')检测=假帧计数器 = 0# imgVideo = cap.read()成功,imgVideo = myVid.read()hT, wT, cT = imgTarget.shape # burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.'''而 myVid.isOpened():成功,视频 = myVid.read()如果成功:Video = cv.resize(Video, (wT, hT))'''# burada 关键点 yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.orb = cv.ORB_create(nfeatures=1000)kp1, des1 = orb.detectAndCompute(imgTarget, None)而 myVid.isOpened(): # burada 网络摄像头 ile fotoğrafları birleştircez.成功,imgWebcam = cap.read()imgAug = imgWebcam.copy()# burada 关键点 yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.kp2, des2 = orb.detectAndCompute(imgWebcam,无)# imgwebcami myvid yapıp değiştir o zaman oldu .d tek seferliktir belki?# imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)如果检测==假:myVid.set(cv.CAP_PROP_POS_FRAMES, 0)帧计数器 = 0别的:如果 frameCounter == myVid.get(cv.CAP_PROP_FRAME_COUNT):myVid.set(cv.CAP_PROP_POS_FRAMES, 0)帧计数器 = 0成功,imgVideo = myVid.read()imgVideo = cv.resize(imgVideo, (wT, hT))# burada ise 匹配 yani 网络摄像头 resmi ile fotoğrafımız arasındaki benzerlikleri alıyoruz.bf = cv.BFMatcher()匹配 = bf.knnMatch(des1, des2, k=2)好 = []对于 m, n 匹配项:如果 m.distance <0.75 * n.距离:good.append(m)打印(len(好))imgFeatures = cv.drawMatches(imgTarget, kp1, imgWebcam, kp2, good, None, flags=2)如果 len(good) >20: # burada eğer anahtar bölgelerimiz 20 den fazla şekilde uyuşuyorsa, tanımlama(detection) tamamlanmış oluyor.检测 = 真srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)矩阵,掩码 = cv.findHomography(srcPts, dstPts, cv.RANSAC, 5)打印(矩阵)# eşleşine resmin etrafını çiziyoruzpts = np.float32([[0, 0], [0, hT], [wT, hT], [wT, 0]]).reshape(-1, 1, 2)dst = cv.perspectiveTransform(pts, 矩阵)img2 = cv.polylines(imgWebcam, [np.int32(dst)], True, (255, 0, 255), 3)# burada videomuzu,网络摄像头 resmimiz ile aynı boyuta getiriyoruz.imgWarp = cv.warpPerspective(imgVideo, matrix, (imgWebcam.shape[1], imgWebcam.shape[0]))cv.imshow(imgWarp", imgWarp)cv.waitKey(0)

<块引用>

我的网络摄像头有问题吗?但它适用于其他代码,或者也适用于应用程序.为什么会出现这个错误,我该如何解决?

如果你想连接你的默认网络摄像头,通常你初始化为:

myVid = cv.VideoCapture(0)

如果您想连接到外部设备,您可以使用 1、2 等.

My full code:

import cv2 as cv
import numpy as np


cap = cv.VideoCapture(0 + cv.CAP_DSHOW)
imgTarget = cv.imread('photos\TargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photos\video.mp4')


detection = False
frameCounter = 0
imgVideo = cap.read()
success, imgVideo = myVid.read()
hT,wT,cT = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.
'''while myVid.isOpened():
    success, Video = myVid.read()
    if success:
        Video = cv.resize(Video, (wT, hT))'''

#burada key points yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.
orb = cv.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(imgTarget,None)

while True: #burada webcam ile fotoğrafları birleştircez.
 

    sucess,imgWebcam = cap.read()
    imgAug = imgWebcam.copy()
    #burada key points yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.
    kp2, des2 = orb.detectAndCompute(imgWebcam, None)#imgwebcami myvid yapıp değiştir o zaman oldu .d tek seferliktir belki?
    # imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)
 
    if detection == False:
        myVid.set(cv.CAP_PROP_POS_FRAMES,0)
        frameCounter = 0
    else:
        if frameCounter == myVid.get(cv.CAP_PROP_FRAME_COUNT):
            myVid.set(cv.CAP_PROP_POS_FRAMES, 0)
            frameCounter = 0
        success, imgVideo = myVid.read()
        imgVideo = cv.resize(imgVideo, (wT, hT))
 
    #burada ise matches yani webcam resmi ile fotoğrafımız arasındaki benzerlikleri alıyoruz.
    bf = cv.BFMatcher()
    matches = bf.knnMatch(des1,des2,k=2)
    good =[]
    for m,n in matches:
        if m.distance < 0.75 *n.distance:
            good.append(m)
    print(len(good))
    imgFeatures = cv.drawMatches(imgTarget,kp1,imgWebcam,kp2,good,None,flags=2)
 
    if len(good) > 20: #burada eğer anahtar bölgelerimiz 20 den fazla şekilde uyuşuyorsa, tanımlama(detection) tamamlanmış oluyor.
        detection = True
        srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
        matrix, mask = cv.findHomography(srcPts,dstPts,cv.RANSAC,5)
        print(matrix)

        #eşleşine resmin etrafını çiziyoruz
        pts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2)
        dst = cv.perspectiveTransform(pts,matrix)
        img2 = cv.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3)

        #burada videomuzu, webcam resmimiz ile aynı boyuta getiriyoruz.
        imgWarp = cv.warpPerspective(imgVideo,matrix, (imgWebcam.shape[1],imgWebcam.shape[0]))

. . . . . Hi guys, I am doing a project. And in my code I tried to use warpPerspective parameter. But in that line it gives the error of:

error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\imgwarp.cpp:2903: error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

And this is my related code of error:

imgWarp = cv.warpPerspective(imgVideo,matrix, (imgWebcam.shape[1],imgWebcam.shape[0]))

I also copied my all code to top of my page because it can be needed.

Is there any problem about my webcam? But it works in other codes, or in applications also. Why that this error occures and how can I solve it?

I am waiting your help... Any suggestions will be juch a big benefit to solve this issue.

解决方案

Let me start with a minor change with your code.

When you initialized using \ separator, your code will work only for Windows.

imgTarget = cv.imread('photos\TargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photos\video.mp4')

If you use os.path's sep, it will work on all OS.

from os.path import sep

imgTarget = cv.imread('photos' + sep + 'TargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photos' + sep + 'video.mp4')

I would like to suggest you to use the default resolution and initialize your cap variable as:

cap = cv.VideoCapture(0)

You can also display the imgWarp using imshow

cv.imshow("imgWarp", imgWarp)
cv.waitKey(0)

An example output:

Code:


import cv2 as cv
import numpy as np

from os.path import sep

cap = cv.VideoCapture(0)
imgTarget = cv.imread('photos' + sep + 'TargetImage.jpg')  # bu resmimiz
myVid = cv.VideoCapture('photos' + sep + 'video.mp4')

detection = False
frameCounter = 0
# imgVideo = cap.read()
success, imgVideo = myVid.read()
hT, wT, cT = imgTarget.shape  # burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.
'''while myVid.isOpened():
    success, Video = myVid.read()
    if success:
        Video = cv.resize(Video, (wT, hT))'''

# burada key points yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.
orb = cv.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(imgTarget, None)

while myVid.isOpened():  # burada webcam ile fotoğrafları birleştircez.
    sucess, imgWebcam = cap.read()
    imgAug = imgWebcam.copy()
    # burada key points yani böyle önemli bölgeler, köşeler gibi yerleri beliriliyoruz.
    kp2, des2 = orb.detectAndCompute(imgWebcam,
                                     None)  # imgwebcami myvid yapıp değiştir o zaman oldu .d tek seferliktir belki?
    # imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)

    if detection == False:
        myVid.set(cv.CAP_PROP_POS_FRAMES, 0)
        frameCounter = 0
    else:
        if frameCounter == myVid.get(cv.CAP_PROP_FRAME_COUNT):
            myVid.set(cv.CAP_PROP_POS_FRAMES, 0)
            frameCounter = 0
        success, imgVideo = myVid.read()
        imgVideo = cv.resize(imgVideo, (wT, hT))

    # burada ise matches yani webcam resmi ile fotoğrafımız arasındaki benzerlikleri alıyoruz.
    bf = cv.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)
    good = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good.append(m)
    print(len(good))
    imgFeatures = cv.drawMatches(imgTarget, kp1, imgWebcam, kp2, good, None, flags=2)

    if len(good) > 20:  # burada eğer anahtar bölgelerimiz 20 den fazla şekilde uyuşuyorsa, tanımlama(detection) tamamlanmış oluyor.
        detection = True
        srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
        matrix, mask = cv.findHomography(srcPts, dstPts, cv.RANSAC, 5)
        print(matrix)

        # eşleşine resmin etrafını çiziyoruz
        pts = np.float32([[0, 0], [0, hT], [wT, hT], [wT, 0]]).reshape(-1, 1, 2)
        dst = cv.perspectiveTransform(pts, matrix)
        img2 = cv.polylines(imgWebcam, [np.int32(dst)], True, (255, 0, 255), 3)

        # burada videomuzu, webcam resmimiz ile aynı boyuta getiriyoruz.
        imgWarp = cv.warpPerspective(imgVideo, matrix, (imgWebcam.shape[1], imgWebcam.shape[0]))
        cv.imshow("imgWarp", imgWarp)
        cv.waitKey(0)

Is there any problem about my webcam? But it works in other codes, or in applications also. Why that this error occures and how can I solve it?

If you want to connect your default webcam, usually you initialized as:

myVid = cv.VideoCapture(0)

If you want to connect to your external-device you can use 1, 2, etc.

这篇关于OpenCV-(-215:Assertion failed) _src.total() >函数 'cv::warpPerspective' 中的 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆