Ask Your Question

Revision history [back]

How to give the correct datatype input to the function cv2.getPerspectiveTransform?

I am trying to apply perspective transform on a receipt by obtaining the corner points of the receipt via contours. However, I get the following error, while executing this code. I think it's something to do with data type mistamatch in the cv2.getPerspectiveTransform function. I would greatly appreciate any help as I'm not able to figure what I'm doing wrong!

import numpy as np
import cv2
import matplotlib.image as plt

# Reading the input image
image = cv2.imread("costco_bw.jpg")
aspect_ratio = (image.shape[0]) / (image.shape[1])
width = int((image.shape[0]) * (20 / 100) * aspect_ratio)
height = int((image.shape[1]) * (20 / 100) * aspect_ratio)
img_Copy = cv2.resize(image, (width, height))
img_dup = img_Copy.copy()

# Converting the input image to grayscale
gray = cv2.cvtColor(img_Copy, cv2.COLOR_BGR2GRAY)

# Applying Gaussian Blur on the grayscale image
smooth_img = cv2.GaussianBlur(gray, (5, 5), 0)

# Canny edge detection
edged = cv2.Canny(smooth_img, 65, 255)

# Contours of the edged detected image and drawing the contours
(_, contours, _) = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
    peri = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
    if len(approx) == 4:
        screenCnt = approx
        break

cv2.drawContours(img_dup, [screenCnt], -1, (255, 0, 0), 2)
cv2.imshow('draw contours', img_dup)

# Perspective transform
pts2 = np.float32([[0,0],[299,0],[0, 299],[299,299]])
Matrix = cv2.getPerspectiveTransform(screenCnt, pts2)

The following is the error in pycharm:

Traceback (most recent call last):   File "C:/Users/xxx/Documents/PycharmProjects/Test/Test.py", line 40, in <module>
    Matrix = cv2.getPerspectiveTransform(screenCnt, pts2) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:3159: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'cv::getPerspectiveTransform'

How to give the correct datatype input to the function cv2.getPerspectiveTransform?

I am trying to apply perspective transform on a receipt by obtaining the corner points of the receipt via contours. However, I get the following error, while executing this code. I think it's something to do with data type mistamatch in the cv2.getPerspectiveTransform function. I would greatly appreciate any help as I'm not able to figure what I'm doing wrong!

import numpy as np
import cv2
import matplotlib.image as plt

# Reading the input image
image = cv2.imread("costco_bw.jpg")
aspect_ratio = (image.shape[0]) / (image.shape[1])
width = int((image.shape[0]) * (20 / 100) * aspect_ratio)
height = int((image.shape[1]) * (20 / 100) * aspect_ratio)
img_Copy = cv2.resize(image, (width, height))
img_dup = img_Copy.copy()

# Converting the input image to grayscale
gray = cv2.cvtColor(img_Copy, cv2.COLOR_BGR2GRAY)

# Applying Gaussian Blur on the grayscale image
smooth_img = cv2.GaussianBlur(gray, (5, 5), 0)

# Canny edge detection
edged = cv2.Canny(smooth_img, 65, 255)

# Contours of the edged detected image and drawing the contours
(_, contours, _) = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
    peri = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
    if len(approx) == 4:
        screenCnt = approx
        break

cv2.drawContours(img_dup, [screenCnt], -1, (255, 0, 0), 2)
cv2.imshow('draw contours', img_dup)

# Perspective transform
pts2 = np.float32([[0,0],[299,0],[0, 299],[299,299]])
Matrix = cv2.getPerspectiveTransform(screenCnt, pts2)

The following is the error in pycharm:

Traceback (most recent call last):   File "C:/Users/xxx/Documents/PycharmProjects/Test/Test.py", line 40, in <module>
    Matrix = cv2.getPerspectiveTransform(screenCnt, pts2) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:3159: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'cv::getPerspectiveTransform'

How to give the correct Help needed in understanding the datatype of the input argument to the function cv2.getPerspectiveTransform?

I am trying to apply perspective transform on a receipt by obtaining the corner points of the receipt via contours. However, I get the following error, while executing this code. I think it's something to do with data type mistamatch in the cv2.getPerspectiveTransform function. I would greatly appreciate any help as I'm not able to figure what I'm doing wrong!

import numpy as np
import cv2
import matplotlib.image as plt

# Reading the input image
image = cv2.imread("costco_bw.jpg")
aspect_ratio = (image.shape[0]) / (image.shape[1])
width = int((image.shape[0]) * (20 / 100) * aspect_ratio)
height = int((image.shape[1]) * (20 / 100) * aspect_ratio)
img_Copy = cv2.resize(image, (width, height))
img_dup = img_Copy.copy()

# Converting the input image to grayscale
gray = cv2.cvtColor(img_Copy, cv2.COLOR_BGR2GRAY)

# Applying Gaussian Blur on the grayscale image
smooth_img = cv2.GaussianBlur(gray, (5, 5), 0)

# Canny edge detection
edged = cv2.Canny(smooth_img, 65, 255)

# Contours of the edged detected image and drawing the contours
(_, contours, _) = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
    peri = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
    if len(approx) == 4:
        screenCnt = approx
        break

cv2.drawContours(img_dup, [screenCnt], -1, (255, 0, 0), 2)
cv2.imshow('draw contours', img_dup)

# Perspective transform
pts2 = np.float32([[0,0],[299,0],[0, 299],[299,299]])
Matrix = cv2.getPerspectiveTransform(screenCnt, pts2)

The following is the error in pycharm:

Traceback (most recent call last):   File "C:/Users/xxx/Documents/PycharmProjects/Test/Test.py", line 40, in <module>
    Matrix = cv2.getPerspectiveTransform(screenCnt, pts2) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:3159: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'cv::getPerspectiveTransform'