how to fix this issue ? cv2.error: OpenCV(4.1.2) ... error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

how to fix this issue ? cv2.error: OpenCV(4.1.2) ... error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

我正在尝试对一个文件夹中的多个图像进行旋转,但是当我在调整大小函数中输入大于 0.2 的 fx、fy 值时出现此错误 (cv2.error: OpenCV(4.1.2) ... 错误: (-215:断言失败) !ssize.empty() 在函数 '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

阅读图片后添加检查,看它是否 None:

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

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