0

THIS IS MY CODE:

import cv2 as cv
import numpy as np


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


detection = False
frameCounter = 0

success, Video = Vid.read()
h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.
Video = cv.resize(Video, (w, h))

Guys, this is the part of my code. I am trying to resize my image but ıt gives the following error:

error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\resize.cpp:3784: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

Do you have any suggestions to solve this?

  • Does this answer your question? [I am having trouble with this error (-215:Assertion failed) !ssize.empty() in function 'resize' in opencv](https://stackoverflow.com/questions/52162004/i-am-having-trouble-with-this-error-215assertion-failed-ssize-empty-in-fu) – funie200 Jan 25 '21 at 07:26

2 Answers2

1

First off the video capturing is mostly done in a loop where you check the "success" of grabbing a frame then proceed to show the frame or do any kind of processing, a very simple example would be:

import cv2

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    if ret:
        cv2.imshow('camera', frame)
        k = cv2.waitKey(1)
    else:
        cap.release()
        cv2.destroyAllWindows()

Where the capture is stopped if frames are not being received.

Secondly i see that you opened two seperate captures:

cap = cv.VideoCapture(0)
Vid = cv.VideoCapture('photos\video.mp4')

One for the device camera and another for a video path you provided. If you are two handle to parallel captures its also recommended to check whatever they capture frames correctly or not using the method above.

In the end what i guess here is the video path being provided incorrectly, so what i suggest is to apply the above and then provide the full path to the video you are trying to open.

Same goes with the image. As mentioned in this question You should first check and see if the image was loaded.

KiLJ4EdeN
  • 206
  • 2
  • 6
1

If I'm not mistaken, you want to resize the video frames with the same size of imgTarget

You can solve with two-steps


    1. Check whether the video is opened
    1. If the video is opened then resize

First you should be checking whether your video can be opened

h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.

while Vid.isOpened():
    success, Video = Vid.read()

Now you need to check whether the current frame returns

h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.

while Vid.isOpened():
    success, Video = Vid.read()
    if success:

Now resize

h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.

while Vid.isOpened():
    success, Video = Vid.read()
    if success:
        Video = cv.resize(Video, (w, h))

If you want to display you can use imshow

h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.

while Vid.isOpened():
    success, Video = Vid.read()
    if success:
        Video = cv.resize(Video, (w, h))
        cv2.imshow("Video")
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

Make sure to release the cap and Vid variables at the end of the code.

h,w,c = imgTarget.shape #burada resmimizin yüksekliğini, kalınlığını genişliğini falan alıyoruz.

while Vid.isOpened():
    success, Video = Vid.read()
    if success:
        Video = cv.resize(Video, (w, h))
        cv2.imshow("Video")
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

Vid.release()
cap.release()
Ahmet
  • 7,527
  • 3
  • 23
  • 47