【问题标题】:how to fix this issue ? cv2.error: OpenCV(4.1.2) ... error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'如何解决这个问题? cv2.error: OpenCV(4.1.2) ... 错误: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
【发布时间】:2020-07-29 20:18:44
【问题描述】:

我正在尝试对文件夹中的多个图像进行旋转,但是当我在调整大小函数中放置大于 0.2 的 fx、fy 值时出现此错误 (cv2.error: OpenCV(4.1.2) ... 错误: (-215:Assertion failed) !ssize.empty() in function 'cv::resize')

虽然,当我尝试旋转单个图像并将 fx 和 fy 的值设置为 0.5 时,它工作得非常好。

有没有办法解决这个问题,因为一张一张地增加图像非常忙碌?加上这里附加的代码旋转的多张图像,fx 和 fy 值等于 0.2,具有不合需要的尺寸,即照片非常小并且它们的质量也降低了。

多张图片的旋转部分代码如下:

for imag in os.listdir(source_folder):
img = cv2.imread(os.path.join(source_folder,imag))
img = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
width = img.shape[1]
height =  img.shape[0]
M = cv2.getRotationMatrix2D((width/2,height/2),5,1.0)
rotated_img = cv2.warpAffine(img,M,(img.shape[1],img.shape[0]))
cv2.imwrite(os.path.join(destination_right_folder, "v4rl" + str(a) + '.jpg') , rotated_img)
#cv2.imshow("rotated_right",rotated_img)
#cv2.waitKey(0)
a += 1

【问题讨论】:

    标签: numpy opencv operating-system rotation data-augmentation


    【解决方案1】:

    阅读图片后添加检查以查看它是否为无:

    img = cv2.imread(os.path.join(source_folder,imag))
    if img is None: continue
    

    调用 cv2.resize() 函数时发生错误。可能正在读取不是图像的文件。

    【讨论】:

    • 这有帮助。我删除了一个不是图像的额外文件,现在代码可以正常工作了
    猜你喜欢
    • 2020-10-13
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2022-08-12
    • 2021-04-09
    • 2021-07-30
    相关资源
    最近更新 更多