赞
踩
哈哈,题目取得这么绕,其实就是自己写了一个很渣的类似图像放大的算法。已知矩阵四周的4点,扩展成更大的矩阵,中间的元素值均匀插入,例如:
矩阵:
1 2
3 4
扩展成3x3的:
1 1.5 2
2 2.5 3
3 3.5 4
不说废话,直接上代码:
# -*- coding: utf-8 -*-
"""
蒋方正二维插值算法。
"""
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from numpy import *
# 一维插值
def yiweichazhi(inputmat):
i = 0
for _ in inputmat:
inputmat[i] = inputmat[0] + (inputmat[-1] - inputmat[0]) * i / (len(inputmat) - 1)
i = i + 1
return inputmat
# 画伪彩色图
def 伪彩色图(zz):
Row = zz.shape[0]
Col = zz.shape[1]
xx, yy = np.meshgrid(np.linspace(0, 10, Col), np.linspace(0, 10, Row)) # 图像xy范围和插值
cmap = matplotlib.cm.jet # 指定colormap
plt.imshow(zz, origin='lower', extent=[xx.min(), xx.max(), yy.min(), yy.max()], cmap=cmap) # 伪彩色图
plt.show()
# 由角4点扩展为插值大矩阵
def 蒋方正插值(a):
# 扩张矩阵 10x10
pointRow = 100 # 插值点数-行
pointCol = 100 # 插值点数-行
aa = np.zeros([pointRow, pointCol], dtype=float)
# 四周点直接赋值
aa[0][0] = a[0][0]
aa[0][-1] = a[0][1]
aa[-1][0] = a[1][0]
aa[-1][-1] = a[1][1]
# 四周先插值
aa[0] = yiweichazhi(aa[0])
aa[-1] = yiweichazhi(aa[-1])
aa[:, 0] = yiweichazhi(aa[:, 0])
aa[:, -1] = yiweichazhi(aa[:, -1])
# 全部插值
for i in range(len(aa)):
aa[i] = yiweichazhi(aa[i])
i = i + 1
return aa
# 未插值前4点矩阵
a = np.array([
[1, 2],
[3, 4]
], dtype=float)
aa = 蒋方正插值(a)
# 打印aa
print(aa, "\n")
# 画图
伪彩色图(aa)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。