如何解决这个问题?cv2.error: OpenCV(4.1.2) ...错误: (-215:断言失败) !ssize.empty() 在函数 'cv::resize'



我正试图对文件夹中的多个图像进行旋转,但当我在调整大小函数中输入大于0.2的fx,fy值时,我遇到了这个错误(cv2.error:OpenCV(4.1.2(…error:(-215:断言失败(!函数"cv::resize"中的ssize.empty(((

尽管如此,当我尝试旋转单个图像并将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((函数时会发生错误。可能正在读取的文件不是图像。

相关内容

最新更新