在 python 中使用 openCV 调整图像大小

Resize images using openCV in python

我有以下方法从目录中获取所有图像:

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])

它不起作用..我该怎么做才能调整所有这些图像的大小?感谢您的帮助。

您可能必须将形状传递给 cv2resize() 函数。

这会将图像调整为 605 列(宽度)和 700 行(高度):

imgR = cv2.resize(ImageCV[Image], (605, 700)) 

有关更多选项,您可以阅读 cv2.resize 的文档。

此外,我建议使用 python 编码约定,例如 imgR -> img_rImage -> imageImageCV -> image_cv 等。希望这对您有所帮助。