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

Bas*_*ian 147 python opencv resize image image-processing

我想使用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)
Run Code Online (Sandbox Code Playgroud)

既然我不能用

cv.LoadImageM
Run Code Online (Sandbox Code Playgroud)

我用了

cv.LoadImage
Run Code Online (Sandbox Code Playgroud)

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

eme*_*mem 316

如果您想使用CV2,则需要使用该resize功能.

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

small = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 
Run Code Online (Sandbox Code Playgroud)

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

resized_image = cv2.resize(image, (100, 50)) 
Run Code Online (Sandbox Code Playgroud)

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

small = scipy.misc.imresize(image, 0.5)
Run Code Online (Sandbox Code Playgroud)

显然,您可以在这些函数的文档中阅读更多选项(cv2.resize,scipy.misc.imresize).


更新:
根据SciPy文档:

imresize弃用的SciPy的1.0.0,并且将在1.2.0被删除.
skimage.transform.resize改用.

请注意,如果您希望按因子调整大小,则可能实际需要skimage.transform.rescale.

  • 是的,您无法在不丢失信息的情况下缩小图像大小. (7认同)

Joã*_*cho 54

将图像尺寸加倍的示例

有两种方法可以调整图像大小.可以指定新大小:

  1. 手动;

    height, width = src.shape[:2]

    dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)

  2. 通过缩放因子.

    dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC),其中fx是沿水平轴的比例因子和沿垂直轴的fy.

要缩小图像,通常使用INTER_AREA插值看起来效果最佳,而放大图像时,使用INTER_CUBIC(慢速)或INTER_LINEAR(速度更快但看起来还不错)通常看起来最佳.

缩小图像以适应最大高度/宽度(保持纵横比)

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()
Run Code Online (Sandbox Code Playgroud)

使用cv2代码

import cv2 as cv

im = cv.imread(path)

height, width = im.shape[:2]

thumbnail = cv.resize(im, (width/10, height/10), interpolation = cv.INTER_AREA)

cv.imshow('exampleshq', thumbnail)
cv.waitKey(0)
cv.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)


nat*_*ncy 8

这是一个按所需宽度或高度放大或缩小图像同时保持纵横比的功能

# 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)
Run Code Online (Sandbox Code Playgroud)

用法

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()
Run Code Online (Sandbox Code Playgroud)

使用此示例图像

在此处输入图片说明

只需缩小到width=100(左)或放大到width=300(右)

在此处输入图片说明 在此处输入图片说明


jle*_*and 7

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

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

size = cv.GetSize(im)
thumbnail = cv.CreateImage( ( size[0] / 10, size[1] / 10), im.depth, im.nChannels)
cv.Resize(im, thumbnail)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 ;)

朱利安


And*_*dyP 6

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)
Run Code Online (Sandbox Code Playgroud)