测试scipy.interpolate.interp2d对于低分辨率图像插值处理结果
简 介: 利用scipy.interpolate.interp2d可以对图像进行插值,获得图像在各个方向的渐进过度过程。这可以为后面在一些特征点进行超像素处理中提供更加精确光滑的数据处理过程。
关键词
: 插值,interp2d,scipyinterpolateinterp2d
§01 interp2d函数
1.1 函数功能
根据 scipy.interpolate.interp2d 中对于interp2d的用法进行了介绍。
class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)
1.1.1 函数参数
- x, y : array_like
Arrays defining the data point coordinates.
If the points lie on a regular grid, x can specify the column coordinates and y the row coordinates, for example:
x = [0,1,2]; y = [0,3]; z = [[1,2,3], [4,5,6]]
Otherwise, x and y must specify the full coordinates for each point, for example:
x = [0,1,2,0,1,2]; y = [0,0,0,3,3,3]; z = [1,2,3,4,5,6]
If x and y are multi-dimensional, they are flattened before use.
-
z : array_like
The values of the function to interpolate at the data points. If z is a multi-dimensional array, it is flattened before use. The length of a flattened z array is either len(x)*len(y) if x and y specify the column and row coordinates or len(z) == len(x) == len(y) if x and y specify coordinates for each point. -
kind : {‘linear’, ‘cubic’, ‘quintic’}, optional
The kind of spline interpolation to use. Default is ‘linear’. -
copy : bool, optional
If True, the class makes internal copies of x, y and z. If False, references may be used. The default is to copy. -
bounds_error : bool, optional
If True, when interpolated values are requested outside of the domain of the input data (x,y), a ValueError is raised. If False, then fill_value is used. -
fill_value : number, optional
If provided, the value to use for points outside of the interpolation domain. If omitted (None), values outside the domain are extrapolated.
1.1.2 函数返回
- values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:]
Interpolated values at input coordinates.
1.2 举例
from scipy import interpolate
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')
▲ 图1.2.1 函数取值
▲ 图1.2.2 插值之后的图像
xnew = np.arange(-5.01, 5.01, 1e-2)
ynew = np.arange(-5.01, 5.01, 1e-2)
znew = f(xnew, ynew)
plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')
plt.show()
▲ 图1.2.3 对于x轴方面的插值结果
▲ 图1.2.4 插值前的函数
import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *
from scipy import interpolate
from tqdm import tqdm
gifpath = '/home/aistudio/GIF'
if not os.path.isdir(gifpath):
os.makedirs(gifpath)
gifdim = os.listdir(gifpath)
for f in gifdim:
fn = os.path.join(gifpath, f)
if os.path.isfile(fn):
os.remove(fn)
theta = linspace(0, 2*pi, 50)
x = arange(-5.01, 5.01, 0.25)
y = arange(-5.01, 5.01, 0.25)
xx,yy = meshgrid(x,y)
z = sin(xx**2+yy**2 + t)
f = interpolate.interp2d(x,y,z, kind='cubic')
xxx = arange(-5.01, 5.01, 0.01)
yyy = arange(-5.01, 5.01, 0.01)
id = 0
for t in tqdm(theta):
z = sin(xx**2+yy**2 + t)
f = interpolate.interp2d(x,y,z, kind='cubic')
zzz = f(xxx, yyy)
plt.figure(figsize=(8,8))
plt.imshow(zzz)
savefile = os.path.join(gifpath, '%03d.jpg'%id)
id += 1
plt.savefig(savefile)
plt.close()
▲ 图1.2.5 插值后的函数
§02 图像插值
2.1 插值图像
利用在 利用圆圈轮廓面积求取圆环半径:cv2.findContours, contourArea 抑菌圈金属模板扫描图片,通过 cv2.resize之后形成64×64的分辨率的图像。
2.1.1 原始图像
import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *
import cv2
from scipy import interpolate
filename = '/home/aistudio/work/Scanner/ScanDiagBlock/img258.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray32 = cv2.resize(gray, (64,64))
plt.figure(figsize=(10,10))
plt.imshow(gray32)
plt.savefig('/home/aistudio/stdout.jpg')
▲ 图2.1.1 设置成分辨率为64×64图片
2.1.2 插值图像
x = arange(64)
y = arange(64)
f = interpolate.interp2d(x,y,gray32, kind='cubic')
xx = arange(0, 64, 0.1)
yy = arange(0, 64, 0.1)
zz = f(xx, yy)
plt.figure(figsize=(10,10))
plt.imshow(zz)
plt.savefig('/home/aistudio/stdout.jpg')
(1)立方插值
▲ 图2.1.2 插值后的图像
(2)线性插值
▲ 图2.1.3 线性插值
(3)四次方插值
▲ 图2.1.4 四次方插值
※ 插值总结 ※
利用scipy.interpolate.interp2d可以对图像进行插值,获得图像在各个方向的渐进过度过程。这可以为后面在一些特征点进行超像素处理中提供更加精确光滑的数据处理过程。
■ 相关文献链接:
● 相关图表链接:
- 图1.2.1 函数取值
- 图1.2.2 插值之后的图像
- 图1.2.3 对于x轴方面的插值结果
- 图2.1.1 设置成分辨率为64×64图片
- 图2.1.2 插值后的图像
- 图2.1.3 线性插值
- 图2.1.4 四次方插值
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST3.PY -- by Dr. ZhuoQing 2022-01-27
#
# Note:
#============================================================
from headm import * # =
import cv2
from scipy import interpolate
filename = '/home/aistudio/work/Scanner/ScanDiagBlock/img258.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray32 = cv2.resize(gray, (64,64))
plt.figure(figsize=(10,10))
plt.imshow(gray32)
plt.savefig('/home/aistudio/stdout.jpg')
#------------------------------------------------------------
x = arange(64)
y = arange(64)
f = interpolate.interp2d(x,y,gray32, kind='quintic')
#------------------------------------------------------------
xx = arange(0, 64, 0.1)
yy = arange(0, 64, 0.1)
zz = f(xx, yy)
plt.figure(figsize=(10,10))
plt.imshow(zz)
plt.savefig('/home/aistudio/stdout.jpg')
#------------------------------------------------------------
# END OF FILE : TEST3.PY
#============================================================
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/122710599
- 点赞
- 收藏
- 关注作者
评论(0)