MLX90640的阵列插值处理-多项式插值由32×24像素到512×384像素(附从温度阵列转化为热成像图代码)
由于大四final year project的需求,需要利用mlx90640热成像芯片处理,由于像素过于低,无法利用灰度图进行人脸识别,因此必须扩展像素。这里link 采用了博主的代码,将Pascal版本改编为python版本的代码。
下面是改编的函数的代码,命名为func.py,函数分为:
PolynomialInterpolationArr(SourceDatas,Dir, times) 。 SourceDatas为一个列表,里面是温度矩阵中的一行温度数据,最后返回值为插值结束后的温度数据。具体插值方法见上面链接。
GetPolyData_U(OriginData,times,coes) 会返回一个系数,该系数会被应用在PolynomialInterpolationArr函数当中。
chazhi(temp_matrix)是为了主函数直观清晰而写的一个函数。该函数将温度矩阵拆分为一维列表并根据PolynomialInterpolationArr函数进行插值。
整体思路是先将24×32的温度矩阵在行的方向上插值两次,变为24×512的温度矩阵。之后将24×512的矩阵在列上进行两次插值,变为384*512的温度矩阵.
这里用了MLX90640采集的一次人脸温度数据。txt文本放不上来,有需要的可以把project下载下来测试一下情况。
import numpy as np
def PolynomialInterpolationArr (SourceDatas,Dir, times):
arrCount=len(SourceDatas)
Result=[0]*arrCount*4
if (Dir==0) :
startIndex = 2
else :
startIndex = 1
for i in range(arrCount):
Result[startIndex+i*4]=SourceDatas[i]
coes = [0] * 4 #这里初始化一下,之后应该就每次变化不用归0
OriginDatas=np.zeros((2,times))
for i in range(arrCount-times+1) :
for j in range(times) : # 初始化拟合原始数据
OriginDatas[0,j] = j * 4
OriginDatas[1,j] = SourceDatas[i + j]
coes=GetPolyData_U(OriginDatas, times, coes);
for h in range(1,4) :
if times >= 2 :
tempDou = coes[0]+ (h) * coes[1];
if times >= 3 :
tempDou = tempDou+ (h) * (h) * coes[2];
if times >= 4 :
tempDou = tempDou+ (h) * (h) * (h) * coes[3];
Result[startIndex + i * 4 + (h)] = tempDou
# 两端插值,两端插值直接使用线性插值
OriginDatas = np.zeros((2, 2))
OriginDatas[0,0] = 0
OriginDatas[1,0] = SourceDatas[0]
OriginDatas[0,1] = 4
OriginDatas[1,1] = SourceDatas[1]
GetPolyData_U(OriginDatas, 2, coes)
if Dir==0:
tempDou = coes[0] + (-1) * coes[1]
Result[1] = tempDou
tempDou = coes[0] + (-2) * coes[1]
Result[0] = tempDou
else:
tempDou = coes[0] + (-1) * coes[1]
Result[0] = tempDou
# 末端插值
for i in range(arrCount - times,arrCount - 1):
for j in range(2):
OriginDatas[0,j] = j * 4
OriginDatas[1,j] = SourceDatas[i + j]
coes=GetPolyData_U(OriginDatas, 2, coes)
for j in range(1,4):
tempDou = coes[0] + j * coes[1]
Result[startIndex + i * 4 + j] = tempDou
if Dir==0:
tempDou = coes[0] + (5) * coes[1]
Result[arrCount * 4 - 1] = tempDou
else:
tempDou = coes[0] + (5) * coes[1]
Result[arrCount * 4 - 2] = tempDou
tempDou = coes[0] + (6) * coes[1]
Result[arrCount * 4 - 1] = tempDou
return Result
def GetPolyData_U(OriginData,times,coes):
if ((times < 2) or (times > 4)) :
times=2
if times==2:
x1 = OriginData[0,0]
x2 = OriginData[0,1]
y1 = OriginData[1,0]
y2 = OriginData[1,1]
coes[1] = (y2 - y1) / x2
coes[0] = y1
elif times==3:
x1 = OriginData[0,0]
x2 = OriginData[0,1]
x3 = OriginData[0,2]
y1 = OriginData[1,0]
y2 = OriginData[1,1]
y3 = OriginData[1,2]
coes[2] = ((y3 - y1) * x2 - (y2 - y1) * x3) / (x2 * x3 * x3 - x2 * x2 * x3)
coes[1] = (y2 - y1) / x2 - coes[2] * x2
coes[0] = y1
elif times==4:
x1 = OriginData[0,0]
x2 = OriginData[0,1]
x3 = OriginData[0,2]
x4 = OriginData[0,3]
y1 = OriginData[1,0]
y2 = OriginData[1,1]
y3 = OriginData[1,2]
y4 = OriginData[1,3]
coes[3] = ((y4 - y1) * x2 - (y2 - y1) * x4) / x2 - ((y3 - y1) * x2 - (y2 - y1) * x3) / (
x2 * x3 * x3 - x2 * x2 * x3) * (x2 * x4 * x4 - x2 * x2 * x4) / x2
coes[3] = coes[3] / ((x2 * x4 * x4 * x4 - x2 * x2 * x2 * x4) / x2 - (x2 * x3 * x3 * x3 - x2 * x2 * x2 * x3) / (
x2 * x3 * x3 - x2 * x2 * x3) * (x2 * x4 * x4 - x2 * x2 * x4) / x2)
coes[2] = ((y3 - y1) * x2 - (y2 - y1) * x3) / (x2 * x3 * x3 - x2 * x2 * x3) - coes[3] * (
(x2 * x3 * x3 * x3 - x2 * x2 * x2 * x3) / (x2 * x3 * x3 - x2 * x2 * x3))
coes[1] = ((y2 - y1) - coes[2] * x2 * x2 - coes[3] * x2 * x2 * x2) / x2
coes[0] = y1
return coes
def chazhi(temp_matrix): #temp_matrix是一个24*32的数组
temp_row = np.zeros((24, 32 * 16)) # 插值之后的数组
temp_line = np.zeros((32 * 16, 24 * 16)) # temp_row转置之后的数组
for i in range(24):
SourceDatas = temp_matrix[i, :].tolist()
temp_row[i, :] = PolynomialInterpolationArr((PolynomialInterpolationArr(SourceDatas, 0, 4)), 1, 4)
temp_row = np.transpose(temp_row) # 方便继续插值
for i in range(32 * 16):
SourceDatas = temp_row[i, :].tolist()
temp_line[i, :] = PolynomialInterpolationArr((PolynomialInterpolationArr(SourceDatas, 0, 4)), 1, 4)
expandtemp = np.transpose(temp_line)
return expandtemp
print(len(expandtemp))
附上main函数代码
import func
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import matplotlib
from scipy.interpolate import griddata
f = open(r"temp.txt")
val_list = f.readlines()
lists =[]
for string in val_list:
string = string.split()
lists.append(string[0:32])
a = np.array(lists)
a= a.astype(float)
#a = np.matrix(a).reshape(24,32)
a=a.reshape(24,32)
#a=np.transpose(a) #转置,担心a旋转了
expandtemp=func.chazhi(a)
#####
#interpolation=nearest 插值
#origin=upper 镜像
#cmap=plt.cm. jet\hsv\hot\cool\spring\summer\autumn\winter\gray\bone\copper\pink\lines
#####
#plt.imshow(a,interpolation='nearest',cmap=plt.cm.hot,origin='upper')
plt.imshow(expandtemp,interpolation='None',cmap=plt.cm.gray,origin='upper')
#温标 shrink=0.8
plt.xticks(())
plt.yticks(())
plt.colorbar()
plt.show()
#保存图片
#matplotlib.image.imsave('test.png',a)
图1:插值后根据温度矩阵转化来的灰度图(将plt.imshow中的gray改为hot即可得到热图)

图2:原始灰度图
希望接下来进行的人脸识别能对我友好一点呜呜呜┭┮﹏┭┮。

本文介绍了如何使用多项式插值方法将MLX90640热成像芯片的32×24像素阵列扩展到512×384像素,以便于人脸识别。通过Python实现,包括PolynomialInterpolationArr和GetPolyData_U等关键函数,详细步骤为先沿行方向插值,再沿列方向插值。文中提供了插值处理后的温度数据及转换为灰度图的代码示例。
1707

被折叠的 条评论
为什么被折叠?



