如何使用 OpenCV2.0 和 Python2.6 调整图像大小 [英] How to resize an image with OpenCV2.0 and Python2.6
问题描述
我想使用 OpenCV2.0 和 Python2.6 来显示调整大小的图像.我使用并采用了 this 示例,但不幸的是,此代码适用于 OpenCV2.1 和似乎不适用于 2.0.这是我的代码:
I want to use OpenCV2.0 and Python2.6 to show resized images. I used and adopted this example but unfortunately, this code is for OpenCV2.1 and does not seem 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?
推荐答案
如果你想使用CV2,你需要使用resize
函数.
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 列(宽度)和 50 行(高度):
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、scipy.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.改为调整大小
.
imresize
is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Useskimage.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屋!