'Iam getting this error and i dont know how to fix it: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'warpPerspective'

OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'warpPerspective'

Overload resolution failed:

  • src is not a numpy array, neither a scalar
  • Expected Ptr<cv::UMat> for argument 'src'
import numpy as np

img = cv2.VideoCapture(0)

while True:

    # Step 1: Define 4 corner points , use any photo editor like paint to get pixel location of the points
    pts1 = np.float32([[85,30],[150,30],[86,125],[150,125]])

    # Step 2: Define location of the points
    width, height = 250, 350 # getting required size by taking care of height & width ratio of card
    pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])

    # Step 3: Make matrix
    matrix = cv2.getPerspectiveTransform(pts1,pts2)

    # Get Output image based on the above matrix
    imgOutput = cv2.warpPerspective(img, matrix, (width, height))

    cv2.imshow("Image",img)
    cv2.imshow("Output", imgOutput)

    if cv2.waitKey(1) == ord('q'):
            break

img.release()
cv2.destroyAllWindows()```


Solution 1:[1]

Why don't you pay attention @Jeru Luke. You're missing one extra cv2.cvtColor. I changed in line #24 with gray instead of img.

import cv2
import numpy as np

img = cv2.VideoCapture(0)

while img.isOpened():
    ret, frame = img.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Step 1: Define 4 corner points , use any photo editor like paint to get pixel location of the points
    pts1 = np.float32([[85,30],[150,30],[86,125],[150,125]])

    # Step 2: Define location of the points
    width, height = 250, 250 # getting required size by taking care of height & width ratio of card
    pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])

    # Step 3: Make matrix
    matrix = cv2.getPerspectiveTransform(pts1,pts2)

    # Get Output image based on the above matrix
    
    imgOutput = cv2.warpPerspective(gray, matrix, (width, height))

    cv2.imshow("Image", gray)
    cv2.imshow("Output", imgOutput)

    if cv2.waitKey(1) == ord('q'):
            break

img.release()
cv2.destroyAllWindows()

I'm using Raspberry pi4/8gb, linux 4.5.5, Bullseye v11.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 toyota Supra