简体   繁体   English

cv2.error: OpenCV(4.5.4):-1: error: (-5:Bad argument) in function 'warpPerspective'

[英]cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'warpPerspective'

I'm wandering because the code is blocked.我在徘徊,因为代码被阻止了。 I ask for your help.我请求你的帮助。

It occurred at...它发生在...

  • python: 3.8.9 (tags/v3.8.9:a743f81, Apr 6 2021, 14:02:34) [MSC v.1928 64 bit (AMD64)] python: 3.8.9 (tags/v3.8.9:a743f81, Apr 6 2021, 14:02:34) [MSC v.1928 64 位 (AMD64)]
  • opencv: 4.5.4 opencv:4.5.4

error feedback is...错误反馈是...

Traceback (most recent call last):
  File "D:/001_DataAnalysisTools/pythonProject3/ex_opencv/main.py", line 517, in <module>
    auto_scan_image()
  File "D:/001_DataAnalysisTools/pythonProject3/ex_opencv/main.py", line 490, in auto_scan_image
    warped = cv2.warpPerspective(orig, M, (maxWidth, maxHeight), flags=cv2.INTER_LINEAR)

cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'warpPerspective'
> Overload resolution failed:
>  - Can't parse 'dsize'. Sequence item with index 0 has a wrong type
>  - Can't parse 'dsize'. Sequence item with index 0 has a wrong type

full code is....完整的代码是....

import numpy as np
import cv2


def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype="float32")

    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    # return the ordered coordinates
    return rect


def auto_scan_image():
    # load the image and compute the ratio of the old height
    # to the new height, clone it, and resize it
    # document.jpg ~ docuemnt7.jpg
    image = cv2.imread('images/document.jpg')
    orig = image.copy()
    r = 800.0 / image.shape[0]
    dim = (int(image.shape[1] * r), 800)
    image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

    # convert the image to grayscale, blur it, and find edges
    # in the image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (3, 3), 0)
    edged = cv2.Canny(gray, 75, 200)

    # show the original image and the edge detected image
    print("STEP 1: Edge Detection")
    cv2.imshow("Image", image)
    cv2.imshow("Edged", edged)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # cv2.waitKey(1)

    # find the contours in the edged image, keeping only the
    # largest ones, and initialize the screen contour
    cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]

    # loop over the contours
    for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        # if our approximated contour has four points, then we
        # can assume that we have found our screen
        if len(approx) == 4:
            screenCnt = approx
            break

    # show the contour (outline) of the piece of paper
    print("STEP 2: Find contours of paper")
    cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
    cv2.imshow("Outline", image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.waitKey(1)

    # apply the four point transform to obtain a top-down
    # view of the original image
    rect = order_points(screenCnt.reshape(4, 2) / r)
    (topLeft, topRight, bottomRight, bottomLeft) = rect

    w1 = abs(bottomRight[0] - bottomLeft[0])
    w2 = abs(topRight[0] - topLeft[0])
    h1 = abs(topRight[1] - bottomRight[1])
    h2 = abs(topLeft[1] - bottomLeft[1])
    maxWidth = max([w1, w2])
    maxHeight = max([h1, h2])

    dst = np.float32([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]])

    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(orig, M, (maxWidth, maxHeight), flags=cv2.INTER_LINEAR)

    # show the original and scanned images
    print("STEP 3: Apply perspective transform")
    cv2.imshow("Warped", warped)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.waitKey(1)


if __name__ == '__main__':
    auto_scan_image()

As the version of openCV changes, I think it may be necessary to change the option setting.随着 openCV 版本的变化,我认为可能需要更改选项设置。 I found several documents and tried them, but they didn't work out properly.我找到了几个文件并尝试了它们,但它们没有正常工作。

what am I do???我在做什么??? what am I do???我在做什么??? what am I do???我在做什么??? what am I do???我在做什么??? what am I do???我在做什么???

Thank you.谢谢你。 I solved the problem.我解决了这个问题。 I thought the cause of the problem was origin or M. Thanks to your help, we solved it safely.我认为问题的原因是起源或M。感谢您的帮助,我们安全地解决了它。 Thank you!谢谢!

The reason is that as openCV was versioned up, it no longer supports 'int type'.原因是随着 openCV 的版本升级,它不再支持“int 类型”。 Therefore, I have to change the input variable to 'unsigned integer' in the code.因此,我必须在代码中将输入变量更改为“无符号整数”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“cv2.error: OpenCV(4.5.4):-1: error: (-5:Bad argument) in function 'imshow'” - How to solve "cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'imshow'" “cv2.error:OpenCV(4.5.5):-1:错误:(-5:错误参数)在函数&#39;line&#39;中” - "cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'line'" cv2.error: opencv(4.5.3):-1: 错误: (-5:bad argument) in function 'resize' - cv2.error: opencv(4.5.3) :-1: error: (-5:bad argument) in function 'resize' cv2.error: OpenCV(4.5.2) 👎 错误: (-5:Bad argument) in function &#39;cvtColor&#39; - cv2.error: OpenCV(4.5.2) 👎 error: (-5:Bad argument) in function 'cvtColor' cv2.error:OpenCV(4.6.0):-1:错误:(-5:错误参数) - cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) 我如何修复 cv2.error: OpenCV(4.5.4-dev) - How can i fix cv2.error: OpenCV(4.5.4-dev) cv2.error: OpenCV(4.5.2).error: (-215:Assertion failed)._src:empty() in function 'cv::cvtColor' - cv2.error: OpenCV(4.5.2) .error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' OpenCV 代码中的错误:cv2.error: OpenCV(4.5.1) - Error in OpenCV code: cv2.error: OpenCV(4.5.1) cv2.error:函数setSize中的(-215)s&gt; = 0 - cv2.error: (-215) s >= 0 in function setSize Laplacian opencv 因 cv2.error 失败:OpenCV(4.1.2) - Laplacian opencv fails with cv2.error: OpenCV(4.1.2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM