cv2.resize 使用教程和实例

Syntax

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst

interpolation 选项所用的插值方法
INTER_NEAREST最近邻插值
INTER_LINEAR双线性插值(默认设置)
INTER_AREA使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。
INTER_CUBIC4x4像素邻域的双三次插值
INTER_LANCZOS48x8像素邻域的Lanczos插值

具体示例

原图像: 
这里写图片描述

缩放后的图像: 
这里写图片描述

Code

附上自己写的实验代码:

import cv2
pic = cv2.imread('./Elegent_Girl.jpg')
pic = cv2.resize(pic, (400, 400), interpolation=cv2.INTER_CUBIC)
cv2.imshow('', pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Note:

使用cv2.resize时,参数输入是 宽×高×通道 ,与以往操作不同,需要注意。具体参见opencv: cv2.resize 探究(源码)


我们 习惯的坐标表示 是 先 x 横坐标,再 y 纵坐标。在图像处理中,这种惯性思维尤其需要担心。

  因为在计算机中,图像是以矩阵的形式保存的,先行后列。所以,一张 宽×高×颜色通道=480×256×3 的图片会保存在一个 256×480×3 的三维张量中。图像处理时也是按照这种思想进行计算的(其中就包括 OpenCV 下的图像处理),即 高×宽×颜色通道


  但是问题来了,cv2.resize这个api却是个小例外。因为它的参数输入却是 宽×高×颜色通道


  查看官方文档 Geometric Image Transformations :

resize 
Resizes an image. 
C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )¶ 
Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst 
C: void cvResize(const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR ) 
Python: cv.Resize(src, dst, interpolation=CV_INTER_LINEAR) → None 
Parameters
src – input image. 
dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src. 
dsize – 
output image size; if it equals zero, it is computed as: 
\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))} 
Either dsize or both fx and fy must be non-zero.

  由以下语段可知, cv2.resize 的 dsize 的参数输入是 x轴×y轴,即 宽×高

dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.


  自己写了一个代码实例来验证它:

import cv2
import numpy as np
import random


seq = [random.randint(0, 255) for _ in range(256*480*3)]
mat = np.resize(seq, new_shape=[256, 480, 3])
print ('mat.shape = {}'.format(mat.shape))
cv2.imwrite('origin_pic.jpg', mat)
origin_pic = cv2.imread('./origin_pic.jpg')
print ('origin_pic.shape = {}'.format(origin_pic.shape))
resize_pic = cv2.resize(src=origin_pic,
                        dsize=(int(origin_pic.shape[1] * 2),
                               int(origin_pic.shape[0] * 1))
                        )
print ('resize_pic.shape = {}'.format(resize_pic.shape))
cv2.imshow('resize_pic', resize_pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

  Output:

mat.shape = (256, 480, 3)
origin_pic.shape = (256, 480, 3)
resize_pic.shape = (256, 960, 3)
  • 1
  • 2
  • 3

  成功应验了文档里的参数说明。



版权声明:转载请注明出处 https://blog.csdn.net/JNingWei/article/details/76019940

版权声明:转载请注明出处 https://blog.csdn.net/JNingWei/article/details/78218837
  • 18
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值