OpenCV图像缩放resize函数

API

  • c++

void cv::resize	(	InputArray 	src,
OutputArray 	dst,
Size 	dsize,
double 	fx = 0,
double 	fy = 0,
int 	interpolation = INTER_LINEAR 
)	

函数resize将图像src的大小调整到指定的大小或调整到指定的大小。注意,没有考虑初始dst类型或大小。相反,大小和类型派生自src、dsize、fx和fy。如果你想调整src的大小

// 显式地指定dsize = dst.size ();fx和fy将从这里计算
resize(src, dst, dst.size(), 0, 0, interpolation);

如果你想要在每个方向上把图像抽取2倍,你可以这样调用函数:

// 指定fx和fy并让函数计算目标图像的大小。
resize(src, dst, Size(), 0.5, 0.5, interpolation);

要缩小一个图像,通常使用INTER_AREA插值效果最好,而要放大一个图像,通常使用c::INTER_CUBIC(慢)或INTER_LINEAR(更快,但看起来还可以)效果最好。

  • 参数解释
    在这里插入图片描述

  • python

	
Python:
dst	=	cv.resize(	src, dsize[, dst[, fx[, fy[, interpolation]]]]	)

python示例代码

可以通过dsize = (x,y)手动设置缩放的尺度。(samll2就是这么缩放的)

import  cv2 as cv

file_name = "./data/src/1 (69).jpg"
img = cv.imread(file_name)
img_illumination = cv.GaussianBlur(img, (15, 15), 0)
get_img_reflect = (img + 0.001) / (img_illumination + 0.001)
dsize =[200,200]
#  横纵缩放
small = cv.resize(get_img_reflect, (0, 0), fx=0.5, fy=0.5,interpolation=cv.INTER_NEAREST)
# 固定尺寸缩小
small2 = cv.resize(get_img_reflect, (300, 400))
# 原尺寸缩放
# 获得图像的宽高
x, y = img.shape[0:2]
small3 = cv.resize(get_img_reflect, (int(x/2), int(y/2)))
print(x,y)

cv.imshow("50%", small3)

cv.waitKey(0)

  • small缩放效果
    在这里插入图片描述
  • small2缩放效果
    在这里插入图片描述
  • small3参数缩放效果
    在这里插入图片描述

python的resize函数参数分析


def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None): # real signature unknown; restored from __doc__
    """
    resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst
    .   @brief Resizes an image.
    .   
    .   The function resize resizes the image src down to or up to the specified size. Note that the
    .   initial dst type or size are not taken into account. Instead, the size and type are derived from
    .   the `src`,`dsize`,`fx`, and `fy`. If you want to resize src so that it fits the pre-created dst,
    .   you may call the function as follows:
    .   @code
    .       // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
    .       resize(src, dst, dst.size(), 0, 0, interpolation);
    .   @endcode
    .   If you want to decimate the image by factor of 2 in each direction, you can call the function this
    .   way:
    .   @code
    .       // specify fx and fy and let the function compute the destination image size.
    .       resize(src, dst, Size(), 0.5, 0.5, interpolation);
    .   @endcode
    .   To shrink an image, it will generally look best with #INTER_AREA interpolation, whereas to
    .   enlarge an image, it will generally look best with c#INTER_CUBIC (slow) or #INTER_LINEAR
    .   (faster but still looks OK).
    .   
    .   @param src input image.
    .   @param 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.
    .   @param dsize output image size; if it equals zero, it is computed as:
    .    \f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f]
    .    Either dsize or both fx and fy must be non-zero.
    .   @param fx scale factor along the horizontal axis; when it equals 0, it is computed as
    .   \f[\texttt{(double)dsize.width/src.cols}\f]
    .   @param fy scale factor along the vertical axis; when it equals 0, it is computed as
    .   \f[\texttt{(double)dsize.height/src.rows}\f]
    .   @param interpolation interpolation method, see #InterpolationFlags
    .   
    .   @sa  warpAffine, warpPerspective, remap
    """
    pass
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早睡的叶子

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值