首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用OpenCV2.0和Python2.6调整图像大小

如何使用OpenCV2.0和Python2.6调整图像大小
EN

Stack Overflow用户
提问于 2010-11-16 22:55:57
回答 2查看 548.9K关注 0票数 198

我想使用OpenCV2.0和Python2.6来显示调整大小的图像。我使用并采用了this示例,但不幸的是,这段代码是为OpenCV2.1编写的,似乎不适用于2.0。下面是我的代码:

代码语言:javascript
复制
import os, glob
import cv

ulpath = "exampleshq/"

for infile in glob.glob( os.path.join(ulpath, "*.jpg") ):
    im = cv.LoadImage(infile)
    thumbnail = cv.CreateMat(im.rows/10, im.cols/10, cv.CV_8UC3)
    cv.Resize(im, thumbnail)
    cv.NamedWindow(infile)
    cv.ShowImage(infile, thumbnail)
    cv.WaitKey(0)
    cv.DestroyWindow(name)

因为我不能使用

代码语言:javascript
复制
cv.LoadImageM

我用过

代码语言:javascript
复制
cv.LoadImage

相反,这在其他应用程序中是没有问题的。然而,cv.iplimage没有属性行、列或大小。谁能给我一个提示,如何解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2011-11-21 06:23:33

您可以使用GetSize函数来获取这些信息,cv.GetSize(im)将返回一个带有图像宽度和高度的元组。您还可以使用im.depth和img.nChan获取更多信息。

为了调整图像的大小,我会使用一个略有不同的过程,使用另一个图像而不是矩阵。最好尝试使用相同类型的数据:

代码语言:javascript
复制
size = cv.GetSize(im)
thumbnail = cv.CreateImage( ( size[0] / 10, size[1] / 10), im.depth, im.nChannels)
cv.Resize(im, thumbnail)

希望这能有所帮助;)

朱利安

票数 9
EN

Stack Overflow用户

发布于 2018-07-21 06:49:50

代码语言:javascript
复制
def rescale_by_height(image, target_height, method=cv2.INTER_LANCZOS4):
    """Rescale `image` to `target_height` (preserving aspect ratio)."""
    w = int(round(target_height * image.shape[1] / image.shape[0]))
    return cv2.resize(image, (w, target_height), interpolation=method)

def rescale_by_width(image, target_width, method=cv2.INTER_LANCZOS4):
    """Rescale `image` to `target_width` (preserving aspect ratio)."""
    h = int(round(target_width * image.shape[0] / image.shape[1]))
    return cv2.resize(image, (target_width, h), interpolation=method)
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4195453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档