我有以下方法从目录中获取所有图像:
def ReadImages(Path):
LabelList = list()
ImageCV = list()
classes = ["nonPdr", "pdr"]
width = 605
height = 700
dim = (width, height)
# Get all subdirectories
FolderList = os.listdir(Path)
# Loop over each directory
for File in FolderList:
if(os.path.isdir(os.path.join(Path, File))):
for Image in os.listdir(os.path.join(Path, File)):
# Convert the path into a file
ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
# Add a label for each image and remove the file extension
LabelList.append(classes.index(os.path.splitext(File)[0]))
else:
ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
# Add a label for each image and remove the file extension
LabelList.append(classes.index(os.path.splitext(File)[0]))
return ImageCV, LabelList
但我的图像更大,想将其 wxh 减小到 605x700,然后我尝试做这样的事情:
imgR = cv2.resize(ImageCV[Image])
它不起作用..我该怎么做才能调整所有这些图像的大小?感谢您的帮助。
最佳答案
您可能必须将形状传递给 cv2
的 resize()
函数。
这会将图像调整为 605 列(宽度)和 700 行(高度):
imgR = cv2.resize(ImageCV[Image], (605, 700))
有关更多选项,您可以阅读 cv2.resize 的文档.
此外,我建议使用 python 编码约定,如 imgR -> img_r
、Image -> image
、ImageCV -> image_cv
等。希望这会有所帮助。
关于python - 在 python 中使用 openCV 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57761243/