0


图片如何resize及使用opencv实现图片resize

cv2.resize函数说明

  1. resizeopencv库中的一个函数,主要起到对图片进行缩放的作用。

example: 以下代码就可以将原图片转化为宽和长分别为300,300的图片。width和height可以自己任意指定,不论大小。

  1. import cv2 as cv
  2. width =300
  3. height =300
  4. img = cv.imread('图片所在路径')#例如cv.imread("test/1.jpg")
  5. img = cv.resize(img,(width,height))# 默认使用双线性插值法
  6. cv.imshow("img",img)
  7. cv.waitKey(0)
  8. cv.destroyAllWindows()

参数说明

  1. resize(InputArray src, OutputArray dst, Size dsize,
  2. double fx=0, double fy=0,int interpolation=INTER_LINEAR )
  • InputArray src :输入,原图像,即待改变大小的图像;
  • OutputArray dst: 输出,改变后的图像。这个图像和原图像具有相同的内容,只是大小和原图像不一样而已;
  • dsize:输出图像的大小,如上面例子(300,300)其中,fx和fy就是下面要说的两个参数,是图像width方向和height方向的缩放比例。
  • fx:width方向的缩放比例
  • fy:height方向的缩放比例

如果fx=0.3,fy=0.7,则将原图片的x轴缩小为原来的0.3倍,将y轴缩小为原来的0.7倍,效果如下在这里插入图片描述

interpolation(插值):这个是指定插值的方式,图像缩放之后,肯定像素要进行重新计算的,就靠这个参数来指定重新计算像素的方式,有以下几种:

  • INTER_NEAREST - 最邻近插值
  • INTER_LINEAR - 双线性插值,如果最后一个参数你不指定,默认使用这种方法
  • INTER_CUBIC - 4x4像素邻域内的双立方插值
  • INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值

INTER_NEAREST | 最近邻插值

opencv使用:

  1. img = cv.resize(img,(width,height),interpolation=cv.INTER_NEAREST)

在一维空间中,最近点插值就相当于四舍五入取整。在二维图像中,像素点的坐标都是整数,该方法就是选取离目标点最近的点。会在一定程度上损失 空间对称性(Alignment)

  1. 123456----------->> 3*3的图片转换为5*57891、使用最近邻插值法,令原图片为n*n,目标图片为m*m,图片从0开始数
  2. 则目标图片(ij)位置的像素值为(n/m)*i,(n/m)*j这两个数四舍五入取整对应的原图片的像素
  3. 如目标图片(22)位置的像素为(3/5)*2,(3/5)*2,四舍五入取整也就分别是(11),而这对应原图片的像素值为5,即目标图片(22)的像素值为5[[12233][45566][45566][78899][78899]]

最近邻插值法代码实现:

  1. import cv2 as cv
  2. import numpy as np
  3. height =250
  4. width =250
  5. dst = np.zeros([width,height,3],dtype='uint8')
  6. img = cv.imread("test/1.jpg")for c inrange(3):for i inrange(width):for j inrange(height):
  7. x =(img.shape[0]/width)*i
  8. y =(img.shape[1]/height)*j
  9. dst[i,j,c]= img[round(x),round(y),c]# print (dst[i,j,c])print(type(dst))print(type(img))
  10. cv.imshow("img",img)
  11. cv.waitKey(0)
  12. cv.imshow("dst",dst)
  13. cv.waitKey(0)
  14. cv.destroyAllWindows()

INTER_LINEAR | 双线性插值(默认设置)

opencv使用:

  1. img = cv.resize(img,(width,height),interpolation=cv.INTER_LINEAR)

在两个方向分别进行一次线性插值,保证了 空间对称性
在这里插入图片描述

双线性插值法代码实现:

  1. import cv2 as cv
  2. import numpy as np
  3. import math
  4. height =250
  5. width =250
  6. dst = np.zeros([width,height,3],dtype='uint8')
  7. img = cv.imread("test/1.jpg")for c inrange(3):for i inrange(width):for j inrange(height):
  8. x =(img.shape[0]/width)*i
  9. y =(img.shape[1]/height)*j
  10. x_top = math.ceil((img.shape[0]/width)*i)
  11. x_bottom =int((img.shape[0]/width)*i)
  12. y_top = math.ceil((img.shape[1]/height)*j)
  13. y_bottom =int((img.shape[1]/height)*j)
  14. dst[i,j,c]= img[x_top,y_top,c]*(x-x_bottom)*(y-y_bottom)+img[x_bottom,y_top,c]*(x_top-x)*(y-y_bottom)+img[x_bottom,y_bottom,c]*(x_top-x)*(y_top-y)+img[x_top,y_bottom,c]*(x-x_bottom)*(y_top-y)print(type(dst))print(type(img))
  15. cv.imshow("img",img)
  16. cv.waitKey(0)
  17. cv.imshow("dst",dst)
  18. cv.waitKey(0)
  19. cv.destroyAllWindows()

本文转载自: https://blog.csdn.net/weixin_45277161/article/details/129792394
版权归原作者 githubcurry 所有, 如有侵权,请联系我们删除。

“图片如何resize及使用opencv实现图片resize”的评论:

还没有评论