如何使用OpenCV2.0和Python2.6调整图像大小 [英] How to resize an image with OpenCV2.0 and Python2.6

查看:585
本文介绍了如何使用OpenCV2.0和Python2.6调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenCV2.0和Python2.6来显示已调整大小的图像。我在 http://opencv.willowgarage.com/documentation/python/上使用并采用了该示例cookbook.html 但遗憾的是这段代码适用于OpenCV2.1,似乎不适用于2.0。这里是我的代码:

I want to use OpenCV2.0 and Python2.6 to show resized images. I used and adopted the example at http://opencv.willowgarage.com/documentation/python/cookbook.html but unfortunately this code is for OpenCV2.1 and seem not to be working on 2.0. Here my code:

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)

因为我无法使用

cv.LoadImageM

我用过

cv.LoadImage

相反,这在其他应用程序中没有问题。然而,cv.iplimage没有属性行,列或大小。任何人都可以给我一个提示,如何解决这个问题?谢谢。

instead, which was no problem in other applications. Nevertheless cv.iplimage has no attribute rows, cols or size. Can anyone give me a hint, how to solve this problem? Thanks.

推荐答案

如果你想使用CV2,你需要使用 resize function。

If you wish to use CV2, you need to use the resize function.

例如,这会将两个轴的大小调整一半:

For example, this will resize both axes by half:

small = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 

这将调整图像大小为100 cols(宽度)和50行(height):

and this will resize the image to have 100 cols (width) and 50 rows (height):

resized_image = cv2.resize(image, (100, 50)) 

另一种选择是使用 scipy 模块,使用:

Another option is to use scipy module, by using:

small = scipy.misc.imresize(image, 0.5)

你可以在这些函数的文档中看到更多的选项( cv2.resize sci py.misc.imresize )。

There are obviously more options you can read in the documentation of those functions (cv2.resize, scipy.misc.imresize).

更新:

根据 SciPy文档


imresize 在SciPy 1.0中已弃用。 0和
将在1.2.0中删除。
使用 反而是skimage.transform.resize

imresize is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use skimage.transform.resize instead.

请注意,如果您正在寻找调整因子大小,您实际上可能需要 skimage.transform.rescale

Note that if you're looking to resize by a factor, you may actually want skimage.transform.rescale.

这篇关于如何使用OpenCV2.0和Python2.6调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆