问题描述
使用opencv时出现:cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgproc/src/resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’
原因
待读取图片路径错误
具体错误代码
实现功能:将 imgs_dir 下的所有图片 resize 为 (1920,1080)并保存至 save_dir
报错代码:
import os
import cv2
imgs_dir = "/home/shares/dataset/Images"
save_dir = "/home/shares/dataset/result"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
imgs_files = [x for x in os.listdir(imgs_dir) if x.endswith('.jpg')]
for img_path in imgs_files:
img = cv2.imread(img_path) # !! img_path 此时不是路径,而是图片名字 eg."1.jpg"
wt_img_path = os.path.join(save_dir, img_path.spalit('/'[-1])
rs_img = cv2.resize(img, (1920, 1080))
cv2.imwrite(wt_img_path, rs_img)
修改为:
import os
import cv2
imgs_dir = "/home/shares/dataset/Images"
save_dir = "/home/shares/dataset/result"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
imgs_files = [x for x in os.listdir(imgs_dir) if x.endswith('.jpg')]
for img_file in imgs_files:
img_path = os.path.join(imgs_dir, img_file) # 此时拼接后才是图片完整路径
img = cv2.imread(img_path)
wt_img_path = os.path.join(save_dir, img_file)
rs_img = cv2.resize(img, (1920, 1080))
cv2.imwrite(wt_img_path, rs_img)