图像使用resize()缩小并重新恢复原始大小质量变化

您所在的位置:网站首页 图片放大比例怎么计算 图像使用resize()缩小并重新恢复原始大小质量变化

图像使用resize()缩小并重新恢复原始大小质量变化

2024-07-09 21:21| 来源: 网络整理| 查看: 265

目录

实验目标

代码实现

导入CV库

载入实验图像

创建显示窗口

图像缩放

实验结论

实验目标

 

使用基于python3.8.5下的Opencv库对一幅图像进行缩放,比较缩放前后图像质量差异。

代码实现 导入CV库 import cv2 as cv 载入实验图像 original_img = cv.imread("../Project/images/test.jpg")

要注意这里要使用斜线 "/" 而不是反斜线 "\" 。 

创建显示窗口 cv.namedWindow("Original", cv.WINDOW_AUTOSIZE)

里面第2标志位的注释如下:

. Qt backend supports additional flags: . - **WINDOW_NORMAL or WINDOW_AUTOSIZE:** WINDOW_NORMAL enables you to resize the . window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the . displayed image, and you cannot change the window size manually. . - **WINDOW_FREERATIO or WINDOW_KEEPRATIO:** WINDOW_FREERATIO adjusts the image . with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio. . - **WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED:** WINDOW_GUI_NORMAL is the old way to draw the window . without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI. . By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED 图像缩放

使用cv.resize()函数对原始图像进行缩放。

此函数具有可以缺省的参数。

def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None):

其中的“dst”(目标图像),“fx”(x方向缩放系数),“fy”(y方向缩放系数)以及“interpolation”(插值标志位)都可以缺省,对于此函数官方注释如下:

""" 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 """

解释里面红色标注的地方会引起误解,在函数定义的时候第2位参数应该是"dsize",也即目标图像大小,而注释中第2位变成了"dst",即目标图像,与第3位dst.size()位置颠倒。笔者在此做了一个试验:

import cv2 as cv import numpy as np # Create new window for original image, read and show the image. original_img = cv.imread("../Project/images/test.jpg") cv.namedWindow("Original", cv.WINDOW_AUTOSIZE) cv.imshow("Original", original_img) # test des_img = np.zeros((100, 100, 3), np.uint8) cv.resize(original_img, (100, 100), des_img, cv.INTER_AREA) cv.namedWindow("test", cv.WINDOW_AUTOSIZE) cv.imshow("test", des_img) cv.waitKey(0) cv.destroyAllWindows()

 导入原始图像之后,调用numpy库函数定义一个目标图像des_img,大小为(100, 100, 3)。在调用resize()函数之后故意不输入图像大小(100, 100),只使用des_img作为输出图像,程序报错。可以说明,图像大小应该作为第2位参数不可缺省。之后又将第2位参数(100, 100)改为其他值,例如(160,160),但是des_img大小不做相应改变,函数不报错,但生成的"test"窗口并不正确显示图像。因此可以得出结论:第2位为不可缺省的输出图像大小(如果其不为0),第3位为输出图像,可以缺省,如果想省去定义输出图像的麻烦,可以手动缺省,使用如下函数即可定义输出图像:

des_img = cv.resize(original_img, (100, 100), cv.INTER_AREA)

 最后一位插值方式的标志位有如下几种可供选择:

INTER_NEAREST最近邻插值法INTER_LINEAR双线性插值法(默认)INTER_AREA基于局部像素的重采样(resampling using pixel area relation)。对于图像抽取(image decimation)来说,这可能是一个更好的方法。但如果是放大图像时,它和最近邻法的效果类似。INTER_CUBIC基于4x4像素邻域的3次插值法INTER_LANCZOS4基于8x8像素邻域的Lanczos插值

 

笔者这里使用了INTER_AREA,最后实验代码如下:

""" Target: 1. import one image from matrix camera. 2. Take out the corresponding pixels every other row to construct a new image. 3. Use interpolation to restore the omitted rows, and the interpolation value is the average of two adjacent pixel values. 4. Reconstruct the image. """ import cv2 as cv # Create new window for original image, read and show the image. original_img = cv.imread("../Project/images/test.jpg") cv.namedWindow("Original", cv.WINDOW_AUTOSIZE) cv.imshow("Original", original_img) # Resize the original image into image with only half number of lines and show it. resize_img_width = int(original_img.shape[1] * 0.5) resize_img_length = int(original_img.shape[0]) resize_img = cv.resize(original_img, (resize_img_width, resize_img_length), cv.INTER_AREA) cv.namedWindow("Resized", cv.WINDOW_AUTOSIZE) cv.imshow("Resized", resize_img) # Resize the resized image above into original size and show it. resize_to_org_img = cv.resize(resize_img, (original_img.shape[1], original_img.shape[0]), cv.INTER_LINEAR) cv.namedWindow("Resized_to_Original", cv.WINDOW_AUTOSIZE) cv.imshow("Resized_to_Original", resize_to_org_img) cv.waitKey(0) cv.destroyAllWindows() 实验结论

产生的图像如下: 

在使用resize()函数并恢复之后,图像变模糊,部分细节缺失。笔者还使用更大图像重复实验(大小为 7168x7168) ,细节缺失并不明显,但依旧有缺失。之后的实验将比较内插值法之间的区别。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3