OpenCV 错误:函数“warpAffine”中 src.cols > 0 && src.rows > 0

问题描述 投票:0回答:1

我正在使用下面的 opencv python 代码将图像旋转 5 度:

def rotate_image(image, angle):
    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, -angle, 1.0)
    rotated_image = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
    return rotated_image

它适用于大多数图像,但对于某些图像,它显示以下错误:

OpenCV(4.6.0) /io/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'

我已经检查了出现此异常的图像的大小,一切都很好。不知道为什么会出现这个错误。任何人都可以帮助理解这一点。谢谢

python opencv image-processing
1个回答
0
投票

编辑:使用OP的脚本。

您的代码可以正常运行,没有任何细微的变化。使用 OpenCV 4.6.0。

注释掉

#return rotated_image
函数中的
rotate_image()

片段:

import cv2 as cv


def rotate_image(image, angle):

    img = cv.imread(image, cv.IMREAD_GRAYSCALE)
    (h, w) = img.shape[:2]
    center = (w // 2, h // 2)
    M = cv.getRotationMatrix2D(center, -angle, 1.0)
    rotated_image = cv.warpAffine(img, M, (w, h), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REPLICATE)
    #return rotated_image
    #cv.imwrite('rotate_image.jpg', rotated_image)
    cv.imshow('img', rotated_image)
 
rotate_image('w2.jpg', 90)

cv.waitKey(0)
cv.destroyAllWindows()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.