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