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

作者求助于如何在OpenCV 2.0中使用Python 2.6显示缩放后的图片,遇到cv.LoadImage不适用的问题。通过探讨,提到cv2.resize函数作为替代,并提供不同库如cv2和scipy的resize方法。注意SciPy的imresize已过时,推荐skimage.transform.resize。
摘要由CSDN通过智能技术生成

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)

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.

解决方案

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)

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

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

Another option is to use scipy module, by using:

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).

Update:

According to the SciPy documentation:

imresize is deprecated in SciPy 1.0.0, and

will be removed in 1.2.0.

Use skimage.transform.resize instead.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值