目录

  • 图像内插
    • 放大图像

图像内插

内插通常在图像放大、缩小、旋转和几何校正等任务中使用。内插并用它来调整图像的大小(缩小和放大),缩小和放大基本上采用图像重取样方法

最近邻内插,这种方法将原图像中最近邻的灰度赋给了每个新位置,这种方法简单,但会产生我们不想要的人为失真,如严重的直边失真。更合适的方法是双线性内插,它使用4个最近邻的灰度来计算给定位置的灰度。令(x,y)(x, y)(x,y)表示待赋灰度值的位置(可将它相像为前面描述的网格点)的坐标,令v(x,y)v(x, y)v(x,y)表示灰度值。对于双线性内插方法,所赋的值由如下公式得到:
v(x,y)=ax+by+cxy+d(2.17)v(x, y) = ax + by + cxy + d \tag{2.17} v(x,y)=ax+by+cxy+d(2.17)
4 个系数可由点(x, y)的4个最近邻点写出的4个未知方程求出。双线性内插的结果要比最近邻内插的结果好得多,但计算量会随之增大。

def nearest_neighbor_interpolation(img, new_h, new_w):"""get nearest_neighbor_interpolation for image, can up or down scale image into any ratioparam: img: input image, grady image, 1 channel, shape like [512, 512]param: new_h: new image height param: new_w: new image widthreturn a nearest_neighbor_interpolation up or down scale image"""new_img = np.zeros([new_h, new_w])src_height, src_width = img.shape[:2]r = new_h / src_heightl = new_w / src_widthfor i in range(new_h):for j in range(new_w):x0 = int(i / r)y0 = int(j / l)new_img[i, j] = img[x0, y0]return new_img
# 最近邻插值法处理一通道的图像
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif', 0)img_up = nearest_neighbor_interpolation(img, 1000, 1000)plt.figure(figsize=(16, 8))
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.imshow(img_up, 'gray')
plt.tight_layout()
plt.show()

# 最近邻插值法处理RGB 3通过的图像
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif', -1)
img = img[..., ::-1]
img_up_1 = nearest_neighbor_interpolation(img[..., 0], 800, 800)
img_up_2 = nearest_neighbor_interpolation(img[..., 1], 800, 800)
img_up_3 = nearest_neighbor_interpolation(img[..., 2], 800, 800)
img_up = np.uint8(np.dstack((img_up_1, img_up_2, img_up_3)))plt.figure(figsize=(16, 8))
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.imshow(img_up, 'gray')
plt.tight_layout()
plt.show()

双线性插值

又称为双线性内挺。在数学上,双线性插值是有两个变量的插值函数的线性插值扩展,其核心思想是在两个方向分别进行一次线性插值。

假设我们想得到未知函数f在点P=(x,y)f在点 P=(x,y)f在点P=(x,y)的值,假设我们已知函数f在Q11=(x1,y1),Q12=(x1,y2),Q21=(x2,y1),Q22=(x2,y2)f在Q_{11} = (x_1, y_1),Q_{12}=(x_1, y_2), Q_{21} = (x_2, y_1),Q_{22} = (x_2, y_2)f在Q11​=(x1​,y1​),Q12​=(x1​,y2​),Q21​=(x2​,y1​),Q22​=(x2​,y2​)四个点的值。首先在x方向进行线性插值,得到:

f(R1)≈x2−xx2−x1f(Q11)+x−x1x2−x1f(Q21),whereR1=(x,y1)f(R_1) \approx \frac{x_2 - x}{x_2 - x_1} f(Q_{11}) + \frac{x-x_1}{x_2 - x_1} f(Q_{21}), where R_1 = (x, y_1) f(R1​)≈x2​−x1​x2​−x​f(Q11​)+x2​−x1​x−x1​​f(Q21​),whereR1​=(x,y1​)
f(R2)≈x2−xx2−x1f(Q12)+x−x1x2−x1f(Q22),whereR2=(x,y2)f(R_2) \approx \frac{x_2 - x}{x_2 - x_1} f(Q_{12}) + \frac{x-x_1}{x_2 - x_1} f(Q_{22}), where R_2 = (x, y_2) f(R2​)≈x2​−x1​x2​−x​f(Q12​)+x2​−x1​x−x1​​f(Q22​),whereR2​=(x,y2​)

然后在y方向进行线性插值,得到
f(P)≈y2−yy2−y1f(R1)+y−y1y2−y1f(R2)f(P) \approx \frac{y_2 - y}{y_2 - y_1} f(R_{1}) + \frac{y-y_1}{y_2 - y_1} f(R_{2})f(P)≈y2​−y1​y2​−y​f(R1​)+y2​−y1​y−y1​​f(R2​)

def bilinear_interpolation(img, new_h, new_w):"""get nearest_neighbor_interpolation for image, can up or down scale image into any ratioparam: img: input image, grady image, 1 channel, shape like [512, 512]param: new_h: new image height param: new_w: new image widthreturn a nearest_neighbor_interpolation up or down scale image"""src_height, src_width = img.shape[:2]if new_h == src_height and new_w == src_width:return img.copy()new_img = np.zeros([new_h, new_w])for i in range(new_h):for j in range(new_w):# 首先要找到在原图中对应点的(x, y)x = (i+0.5) * src_height / new_h - 0.5y = (j+0.5) * src_width / new_w - 0.5# find the coordinates of the points which will be used to compute the interpolationsrc_x0 = int(np.floor(x))src_x1 = min(src_x0 + 1 , src_height - 1)src_y0 = int(np.floor(y))src_y1 = min(src_y0 + 1, src_width - 1)# calculate the interpolationtemp0 = (src_x1 - x) * img[src_x0, src_y0] + (x - src_x0) * img[src_x1, src_y0]temp1 = (src_x1 - x) * img[src_x0, src_y1] + (x - src_x0) * img[src_x1, src_y1]new_img[i, j] = int((src_y1 - y) * temp0 + (y - src_y0) * temp1)return new_img
# 最近邻插值法处理一通道的图像
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif', 0)img_up = bilinear_interpolation(img, 800, 800)plt.figure(figsize=(16, 8))
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.imshow(img_up, 'gray')
plt.tight_layout()
plt.show()

双三次内插

双三次插值的目的就是通过找到一种关系,或者说系数,可以把这16个像素对于P处像素值的影响因子找出来,从而根据这个影响因子来获得目标图像对应点的像素值,达到图像缩放的目的。

Bicubic基函数形式如下:
W(x)={(a+2)∣x∣3−(a+3)∣x∣2+1,∣x∣≤1a∣x∣3−5a∣x∣2+8a∣x∣−4a,1<∣x∣<20,otherwiseW(x) =\begin{cases} (a+2) |x|^3 - (a+3)|x|^2 + 1, & |x| \leq 1 \\a|x|^3 -5a|x|^2 + 8a|x| -4a, & 1 < |x| < 2 \\ 0, & \text{otherwise}\end{cases} W(x)=⎩⎪⎨⎪⎧​(a+2)∣x∣3−(a+3)∣x∣2+1,a∣x∣3−5a∣x∣2+8a∣x∣−4a,0,​∣x∣≤11<∣x∣<2otherwise​

a=−1a=-1a=−1
a=−0.5a=-0.5a=−0.5才能完美的实现内插

插值计算公式:
∑i=03∑j=03aijxiyi\sum_{i=0}^3 \sum_{j=0}^3 a_{ij}x^iy^i i=0∑3​j=0∑3​aij​xiyi

def bicubic(x):"""BiCubic primary fuction"""x = abs(x)a = -0.5if x <= 1:return (a + 2) * (x**3) - (a + 3) * (x**2) + 1elif x < 2:return a * (x**3) - 5 * a * (x**2) +  (8 * a * x) - (4 * a)else:return 0
def bicubic_interpolation(img, new_h, new_w):src_height, src_width = img.shape[:2]new_img = np.zeros([new_h, new_w])for h in range(new_h):for w in range(new_w):src_x = h * (src_height / new_h)src_y = w * (src_width / new_w)x = int(np.floor(src_x))y = int(np.floor(src_y))u = src_x - xv = src_y - ytemp = 0for i in range(-1, 2):for j in range(-1, 2):if x + i < 0 or y + j < 0 or x + i >= src_height or y + j >= src_width:continuetemp += img[x+i, y+j] * bicubic(i - u) * bicubic(j - v)new_img[h, w] = np.clip(temp, 0, 255)return new_img
# 最近邻插值法处理一通道的图像
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif', 0)img_up = bicubic_interpolation(img, 800, 800)
# img_up = cv2.resize(img, (800, 800), cv2.INTER_CUBIC)plt.figure(figsize=(16, 8))
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.imshow(img_up, 'gray')
plt.tight_layout()
plt.show()

# 先裁剪图像,然后把裁剪的图像缩小,再进行最近邻、双线内插、双三次内插放大,对比效果
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH02/Fig0220(a)(chronometer 3692x2812  2pt25 inch 1250 dpi).tif', 0)img = img[1100:3500, 200:2600]
img = cv2.resize(img, (200, 200), interpolation=cv2.INTER_CUBIC)new_h, new_w = 2400, 2400
img_nearest = nearest_neighbor_interpolation(img, new_h, new_w)
img_bilinear = bilinear_interpolation(img, new_h, new_w)
img_bicubic = bicubic_interpolation(img, new_h, new_w)plt.figure(figsize=(18, 6))
plt.subplot(131), plt.imshow(img_nearest, 'gray'), plt.title('Nearest'), #plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(img_bilinear, 'gray'), plt.title('Bilinear'), #plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_bicubic, 'gray'), plt.title('Bicubic'), #plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()

放大图像

def up_sample_2d(img):"""up sample 2d image, if your image is RGB, you could up sample each channel, then use np.dstack to merge a RGB imageparam: input img: it's a 2d gray imagereturn a 2x up sample image"""height, width = img.shape[:2]temp = np.zeros([height*2, width*2])temp[::2, ::2] = imgtemp[1::2, 1::2] = imgtemp[0::2, 1::2] = imgtemp[1::2, 0::2] = imgreturn temp
# up sample image using Numpy
img = np.random.random([12, 12])
up = up_sample_2d(img)
down = np.zeros([12, 12])
down = up[::2, ::2]
plt.figure(figsize=(15, 5))
plt.subplot(1,3,1), plt.imshow(img),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2), plt.imshow(up),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3), plt.imshow(down),# plt.xticks([]), plt.yticks([])
plt.show()

# 等比2倍放大
img_ori = cv2.imread("DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif")
img_ori = img_ori[:, :, ::-1]temp = []
for i in range(img_ori.shape[-1]):temp.append(up_sample_2d(img_ori[:, :, i]))
up1 = np.uint8(np.dstack(temp))temp = []
for i in range(up1.shape[-1]):temp.append(up_sample_2d(up1[:, :, i]))
up2 = np.uint8(np.dstack(temp))plt.figure(figsize=(21, 7))
plt.subplot(1,3,1), plt.imshow(img_ori), plt.title("Original"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2), plt.imshow(up1), plt.title("2X"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3), plt.imshow(up2), plt.title("4X"),# plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()

第2章 Python 数字图像处理(DIP) --数字图像基础3 - 图像内插 - 最近邻内插 - 双线性插值 - 双三次内插 - 图像放大相关推荐

  1. 第2章 Python 数字图像处理(DIP) --数字图像基础5 -- 算术运算、集合、几何变换、傅里叶变换等

    目录 数字图像处理所有的基本数字工具介绍 算术运算 集合运算和逻辑运算 空间运算 向量与矩阵运算 图像变换 图像和随机变量 数字图像处理所有的基本数字工具介绍 算术运算 # 相加 img_ori = ...

  2. 第2章 Python 数字图像处理(DIP) --数字图像基础1 - 视觉感知要素 - 亮度适应与辨别

    数字图像基础1 视觉感知要素 亮度适应与辨别 import sys import numpy as np import cv2 import matplotlib import matplotlib. ...

  3. 第2章 Python 数字图像处理(DIP) --数字图像基础4 -- 像素间的一些基本关系 - 邻域 - 距离测试

    目录 像素间的一些基本关系 像素的相信像素 距离测试 import sys import numpy as np import cv2 import matplotlib import matplot ...

  4. 第2章 Python 数字图像处理(DIP) --数字图像基础2 - 图像感知要素 - 图像取样和量化 - 空间分辨率和灰度分辨率

    目录 图像感知与获取 一个简单的成像模型 图像取样和量化 空间分辨率和灰度分辨率 图像感知与获取 一个简单的成像模型 我们用形如 f(x,y)f(x,y)f(x,y) 的二维函数来表示图像.在空间坐标 ...

  5. 第10章 Python 数字图像处理(DIP) - 图像分割 基础知识 标准差分割法

    This Chapter is all about image segmentation. I still not finished whole chapter, but here try to pu ...

  6. 第1章 Python 数字图像处理(DIP) --绪论

    Python 数字图像处理 关于本专栏 此专栏为 Python 数字图像处理(DIP)(冈萨雷斯版),专栏里文章的内容都是来自书里,全部手打,非OCR,因为很多公式,都是用LaTex输入,力求更好看的 ...

  7. 第5章 Python 数字图像处理(DIP) - 图像复原与重建1 - 高斯噪声

    本章主要讲图像复原与重建,首先是了解一下各种噪声的特点与模型,还有形成的方法.一些重点的噪声,如高斯噪声,均匀噪声,伽马噪声,指数噪声,还有椒盐噪声等. 本章主要的噪声研究方法主要是加性噪声. 标题 ...

  8. 第4章 Python 数字图像处理(DIP) - 频率域滤波5 - 二变量函数的傅里叶变换、图像中的混叠、二维离散傅里叶变换及其反变换

    目录 二变量函数的傅里叶变换 二维冲激及其取样性质 二维连续傅里叶变换对 二维取样和二维取样定理 图像中的混叠 二维离散傅里叶变换及其反变换 二变量函数的傅里叶变换 二维冲激及其取样性质 两个连续变量 ...

  9. 第6章 Python 数字图像处理(DIP) - 彩色图像处理2 - 灰度分层(灰度分割)和彩色编码,灰度值到彩色变换,Gray to RGB

    第6章主要讲的是彩色图像处理,一些彩色模型如RGB,CMK,CMYK,HSI等色彩模型:彩色模型的变换关系:还包含由灰度图像怎样处理成假彩色图像:使用彩色分割图像等.本章比较少理论还有变换的描述,主要 ...

最新文章

  1. iOS 之 二维码生成与扫描(LBXScan)
  2. Java JDK11快速下载地址
  3. 气温常年在25度的地方_最低调的海滨城市,物价便宜,常年25度,沙滩细白,不输三亚!...
  4. 如何基于Restful ABAP Programming模型开发并部署一个支持增删改查的Fiori应用
  5. 手把手教你用直方图、饼图和条形图做数据分析(Python代码)
  6. python程序扩展名主要有-python文件的后缀名都有哪些?
  7. 滑轮滚动到页面底部ajax加载数据的实例
  8. vue 两个日期比较大小
  9. P2P网络借贷系统-核心功能-用户投标-业务解说
  10. WCF中DBNull序列化的问题
  11. 最新WIN10系统封装教程2019系列(六)——常规软件安装
  12. 还原 对于 服务器“DESKTOP-BNNIISU\SQLEXPRESS”失败。  (Microsoft.SqlServer.SmoExtended)
  13. 伯朗特机器人编程语言_机器人十大流行编程语言
  14. 嵌入式分享合集106
  15. python 从大到小循环_Python循环小实例----猜大小
  16. MOS管-传输特性曲线的细微之处
  17. mysql 集群搭建(Centos7) for Galera
  18. 快上车,老司机带你实现后台录像功能
  19. 姿态估计之3D 人体姿态估计 - 总结(1)【转】
  20. 字符串哈希--聪聪的加法等式

热门文章

  1. Kali Linux2018 上安装open-vm-tools实现虚拟机交互
  2. Python【01】【基础部分】- A
  3. Vue项目启动webpack报错Module build failed: Error: No PostCSS Config found in......
  4. github中的watch、star、fork的作用
  5. jquery --- 使用when方法等待2个异步事件结束后执行某一个函数.
  6. css --- 浮动元素与 块框/行内框重叠时的细节
  7. ES6-24 生成器与迭代器的应用
  8. flutter image boxfit
  9. 由单例模式造成的内存泄漏
  10. UVA 1156 - Pixel Shuffle(模拟+置换)