如何使用 OpenCV2.0 和 Python2.6 调整图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4195453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize an image with OpenCV2.0 and Python2.6
提问by Bastian
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.htmlbut unfortunately this code is for OpenCV2.1 and seem not to be working on 2.0. Here my code:
我想使用 OpenCV2.0 和 Python2.6 来显示调整大小的图像。我使用并采用了http://opencv.willowgarage.com/documentation/python/cookbook.html 上的示例,但不幸的是,此代码适用于 OpenCV2.1,似乎不适用于 2.0。这是我的代码:
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)
Since I cannot use
由于我无法使用
cv.LoadImageM
I used
我用了
cv.LoadImage
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.
相反,这在其他应用程序中没有问题。然而 cv.iplimage 没有属性行、列数或大小。谁能给我一个提示,如何解决这个问题?谢谢。
回答by jlengrand
You could use the GetSize function to get those information, cv.GetSize(im) would return a tuple with the width and height of the image. You can also use im.depth and img.nChan to get some more information.
您可以使用 GetSize 函数来获取这些信息, cv.GetSize(im) 将返回一个包含图像宽度和高度的元组。您还可以使用 im.depth 和 img.nChan 来获取更多信息。
And to resize an image, I would use a slightly different process, with another image instead of a matrix. It is better to try to work with the same type of data:
并且要调整图像大小,我会使用稍微不同的过程,使用另一个图像而不是矩阵。最好尝试使用相同类型的数据:
size = cv.GetSize(im)
thumbnail = cv.CreateImage( ( size[0] / 10, size[1] / 10), im.depth, im.nChannels)
cv.Resize(im, thumbnail)
Hope this helps ;)
希望这可以帮助 ;)
Julien
于连
回答by emem
If you wish to use CV2, you need to use the resizefunction.
如果您想使用 CV2,则需要使用该resize功能。
For example, this will resize both axes by half:
例如,这会将两个轴的大小减半:
small = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
and this will resize the image to have 100 cols (width) and 50 rows (height):
这将调整图像大小,使其具有 100 列(宽度)和 50 行(高度):
resized_image = cv2.resize(image, (100, 50))
Another option is to use scipymodule, by using:
另一种选择是使用scipy模块,通过使用:
small = scipy.misc.imresize(image, 0.5)
There are obviously more options you can read in the documentation of those functions (cv2.resize, scipy.misc.imresize).
显然,您可以在这些函数的文档(cv2.resize、scipy.misc.imresize)中阅读更多选项。
Update:
According to the SciPy documentation:
更新:
根据SciPy 文档:
imresizeis deprecatedin SciPy 1.0.0, and will be removed in 1.2.0.
Useskimage.transform.resizeinstead.
imresize被弃用的SciPy的1.0.0,并且将在1.2.0被删除。
使用skimage.transform.resize来代替。
Note that if you're looking to resize by a factor, you may actually want skimage.transform.rescale.
请注意,如果您希望按因子调整大小,您实际上可能需要skimage.transform.rescale.
回答by Jo?o Cartucho
Example doubling the image size
将图像大小加倍的示例
There are two ways to resize an image. The new size can be specified:
有两种方法可以调整图像大小。可以指定新的大小:
Manually;
height, width = src.shape[:2]dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)By a scaling factor.
dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC), where fxis the scaling factor along the horizontal axis and fyalong the vertical axis.
手动;
height, width = src.shape[:2]dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)通过比例因子。
dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC),其中fx是沿水平轴的缩放因子,fy 是沿垂直轴的缩放因子。
To shrink an image, it will generally look best with INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with INTER_CUBIC (slow) or INTER_LINEAR (faster but still looks OK).
要缩小图像,通常使用 INTER_AREA 插值看起来最好,而要放大图像,通常使用 INTER_CUBIC(慢)或 INTER_LINEAR(更快但看起来仍然可以)看起来最好。
Example shrink image to fit a max height/width (keeping aspect ratio)
示例缩小图像以适应最大高度/宽度(保持纵横比)
import cv2
img = cv2.imread('YOUR_PATH_TO_IMG')
height, width = img.shape[:2]
max_height = 300
max_width = 300
# only shrink if img is bigger than required
if max_height < height or max_width < width:
# get scaling factor
scaling_factor = max_height / float(height)
if max_width/float(width) < scaling_factor:
scaling_factor = max_width / float(width)
# resize image
img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
cv2.imshow("Shrinked image", img)
key = cv2.waitKey()
Using your code with cv2
在 cv2 中使用您的代码
import cv2 as cv
im = cv.imread(path)
height, width = im.shape[:2]
thumbnail = cv.resize(im, (round(width / 10), round(height / 10)), interpolation=cv.INTER_AREA)
cv.imshow('exampleshq', thumbnail)
cv.waitKey(0)
cv.destroyAllWindows()
回答by AndyP
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)
回答by nathancy
Here's a function to upscale or downscale an image by desired width or height while maintaining aspect ratio
这是一个按所需宽度或高度放大或缩小图像同时保持纵横比的功能
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the width and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
Usage
用法
import cv2
image = cv2.imread('1.png')
cv2.imshow('width_100', maintain_aspect_ratio_resize(image, width=100))
cv2.imshow('width_300', maintain_aspect_ratio_resize(image, width=300))
cv2.waitKey()
Using this example image
使用此示例图像
Simply downscale to width=100(left) or upscale to width=300(right)
只需缩小到width=100(左)或放大到width=300(右)


