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

标签 python opencv resize

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

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 等。希望这会有所帮助。

关于python - 在 python 中使用 openCV 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57761243/

相关文章:

python - 如何在 Opencv Python 中缩放网络摄像头?

c++ - 背景提取

android - 在自定义 ViewGroup 子类中调整旋转 subview 的大小

python - 属性错误: 'Workbook' object has no attribute 'write'

python - 执行一个函数以在循环中返回一个值,直到该函数返回 False - Python

python - 使用 asyncio 和 contextvars 在 python 中的两个异步程序之间共享状态

python - waitKey(0) 和 waitKey(1) 的输出差异

c++ - 禁用窗口大小调整 Win32

c# - 调整控件大小以匹配纵横比

python - PySide 中类似终端的应用程序