Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

python - OpenCV-(-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

My full code:

import cv2 as cv
import numpy as np


cap = cv.VideoCapture(0 + cv.CAP_DSHOW)
imgTarget = cv.imread('photosTargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photosvideo.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:ciopencv-suite_1573470242804workmodulesimgprocsrcimgwarp.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.

question from:https://stackoverflow.com/questions/65919661/opencv-215assertion-failed-src-total-0-in-function-cvwarpperspective

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('photosTargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photosvideo.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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...