TSDK处理大疆H20T热成像图像小白记录
- TSDK的环境配置
- TSDK的调用
- .raw转.tif
- 图像的exif信息
- pix4d拼接
- pos编辑
- 全部代码
记录我用TSDK处理热成像图片的过程
TSDK的环境配置
由于不会调用SDK,所以我将下载的TSDK中sample文件夹用VS打开,找到dji_irp.cpp文件,复制代码。创建新的项目,创建tsdk.cpp文件,将代码粘贴进去。
tsdk.cpp需要链接argagg.hpp,dirp_api.h和dirp_wrapper.h头文件以及libdirp.lib库文件。在tsdk.cpp所在文件夹中创建Libs文件夹,在Libs文件夹中创建icn文件夹和lib文件夹。将下载的TSDK中tsdk-core目录下api文件夹内的文件复制到icn文件夹中,将sample\argparse目录下的文件也放入icn文件夹内;将tsdk-core/lib/windows/release_x64目录下所有.lib文件复制到lib文件夹中,将所有.dll文件和.ini文件复制到tsdk.cpp所在文件夹中x64\Debug文件夹下,与tsdk.exe放在一起。
在tsdk项目中选择调试>tsdk调试属性>C/C++>常规中,将附加包含目录设置为icn文件夹;在tsdk项目中选择调试>tsdk调试属性>连接器>常规中,将附加库目录设置为lib文件夹;在tsdk项目中选择调试>tsdk调试属性>连接器>输入中,将附加依赖项设置为libdirp.lib。至此配置成功。
在调试>tsdk调试属性>调试中输入命令参数-h再运行可以查看帮助
TSDK的调用
通过python代码调用tsdk.exe,批量处理热成像图片,生成存储温度信息的.raw文件。
import ostsdk = r'C:\Users\poppip\source\repos\tsdk\x64\Debug\tsdk.exe'
##tsdk.exe的储存位置
path='F:/thermalsensingimage/DCIM/DJI_202207221723_011_/'
##拍摄的rJPG的储存位置
savepath = "C:/Users/poppip/Desktop/test/tempture/DJI_202207221723_011_/"
##处理结果的储存位置
os.makedirs(savepath,exist_ok=True)distance=25.0
emissivity=0.95
humidity=45
reflection=51.8
##参数根据实际情况设置def use_tsdk(tsdk, path, savepath):print('start')imgnamelist=os.listdir(path)for imgname in imgnamelist:if "T" in imgname:##带T的图像是温度图像portion = os.path.splitext(imgname)coreimgname = portion[0]param = '-s '+ path + imgname + ' -a measure -o ' + savepath + coreimgname + '.raw' + ' --distance ' + str(distance) + ' --emissivity '+ str(emissivity) +' --humidity '+ str(humidity) + '--reflection ' + str(reflection)##选择的模式是measure输出的结果是温度信息,不是原始信息r_v = os.system(tsdk+' '+param)print (r_v)##输出的为tsdk.exe运行的返回值use_tsdk(tsdk, path, savepath)
.raw转.tif
将.raw文件中的值除以10之后为温度值,转为.tif文件以备后续使用。python代码
import os
import opencv
import numpy as np
savepath = "C:/Users/poppip/Desktop/test/tempture/DJI_202207221723_011_/"def raw_to_tif(path, rows, cols, channels):print('to .tif start')files = os.listdir(path)for file in files:portion = os.path.splitext(file)if portion[1] == '.raw':realPath = path + fileimg = np.fromfile(realPath, dtype='uint16')img = img/10##除10之后为温度值img = img.reshape(rows, cols, channels)fileName = portion[0] + '.tif'tif_fileName = os.path.join(path, fileName)cv2.imwrite(tif_fileName, img , (int(cv2.IMWRITE_TIFF_COMPRESSION), 1))os.remove(realPath) ##delete .raw file,如有需要,可以不删除else:print(file + ' it is not .raw file')print('to .tif finsh')raw_to_tif(savepath, 512, 640, 1)
图像的exif信息
转出来的.tif文件没有exif信息,无法定位。将exif信息从rjpg图像中提取出来,写入.tif文件的python代码
from pyexiv2 import Image
import ospath='F:/thermalsensingimage/DCIM/DJI_202207221723_011_/'
savepath = "C:/Users/poppip/Desktop/test/tempture/DJI_202207221723_011_/"def exifrw(path,exif_path):print('exifr&w start')i=0files = os.listdir(path)read_files = os.listdir(exif_path)for read_file in read_files:if "T" in read_file:portion = os.path.splitext(read_file)file = portion[0] + '.tif'if file in files:file_path = os.path.join(path,file)img = Image(file_path)exif_file = os.path.join(exif_path, read_file)imge = Image(exif_file)exif = imge.read_exif()img.modify_exif(exif)imge.close()img.close()else:i=i+1print(str(file)+"unfinish")##检查是否所有.rjpgd的温度文件都完成了全部转换print('exifr&w finish')print(str(i) + " files can not be processed")print('end')
exifrw(savepath,path)
pix4d拼接
最后使用pix4d进行图像拼接,不知道为什么大疆智图无法拼接转换后的图像。
pos编辑
以上方法处理exif信息只能读取地理信息,无法读取精度及航向角等信息,拼接的图像不准,可以用大疆智图导出pos信息,经过编辑,导入pix4d。
pos编辑代码,每组信息单独切片的部分还能简化。
import ospath = 'C:/Users/poppip/Desktop/test/temperature/DJI_202207221723_011_humidity45/'
pos_path= 'F:/thermalsensingimage/DCIM/DJI_202207221723_011_/'posread = os.path.join(pos_path,"pos.txt")
f = open(posread,encoding='utf8')
poswrite=os.path.join(path,"posT.txt")
for line in f:if "T" in line:i1=line.find(",")line1=line[i1-29:i1-4]#nameline2=line[i1+1:]i2=line2.find(",")line3=line2[i2+1:]line2=line2[:i2]#lati3=line3.find(",")line4=line3[i3+1:]line3=line3[:i3]#loni4=line4.find(",")line5=line4[i4+1:]line4=line4[:i4]#altitudei5=line5.find(",")line6=line5[i5+1:]line5=line5[:i5]#yawi6=line6.find(",")line7=line6[i6+1:]line6=line6[:i6]#pitchi7=line7.find(",")line8=line7[i7+1:]line7=line7[:i7]#rolli8=line8.find(",")line9=line8[i8+1:]line8=line8[:i8]#horizontali9=len(line9)line9=line9[:i9-1]#verticalline = line1+'.tif '+line2+' '+line3+' '+line4+' '+line6+' '+line5+' '+line7+' '+line8+' '+line9#for pixprint(line)with open(poswrite,"a") as fs:fs.write(line+"\n")
全部代码
除了tsdk的全部python代码,之前的分步代码使用时应去除##注释。这是我使用TSDK的全部过程,本人只是个小白,很多步骤有待推敲,所有代码仅供参考。
import os
import cv2
import numpy as np
from pyexiv2 import Imagetsdk = r'C:\Users\poppip\source\repos\tsdk\x64\Debug\tsdk.exe'
path='F:/thermalsensingimage/DCIM/DJI_202207221723_011_/'
savepath = "C:/Users/poppip/Desktop/test/tempture/DJI_202207221723_011_/"
os.makedirs(savepath,exist_ok=True)distance=25.0
emissivity=0.95
humidity=45
reflection=51.8def use_tsdk(tsdk, path, savepath):print('start')imgnamelist=os.listdir(path)for imgname in imgnamelist:if "T" in imgname:portion = os.path.splitext(imgname)coreimgname = portion[0]param = '-s '+ path + imgname + ' -a measure -o ' + savepath + coreimgname + '.raw' + ' --distance ' + str(distance) + ' --emissivity '+ str(emissivity) +' --humidity '+ str(humidity) + '--reflection ' + str(reflection)r_v = os.system(tsdk+' '+param)print (r_v)def raw_to_tif(path, rows, cols, channels):print('to .tif start')files = os.listdir(path)for file in files:portion = os.path.splitext(file)if portion[1] == '.raw':realPath = path + fileimg = np.fromfile(realPath, dtype='uint16')img = img/10img = img.reshape(rows, cols, channels)fileName = portion[0] + '.tif'tif_fileName = os.path.join(path, fileName)cv2.imwrite(tif_fileName, img , (int(cv2.IMWRITE_TIFF_COMPRESSION), 1))os.remove(realPath) ##delete .raw fileelse:print(file + ' it is not .raw file')print('to .tif finsh')def exifrw(path,exif_path):print('exifr&w start')i=0files = os.listdir(path)read_files = os.listdir(exif_path)for read_file in read_files:if "T" in read_file:portion = os.path.splitext(read_file)file = portion[0] + '.tif'if file in files:file_path = os.path.join(path,file)img = Image(file_path)exif_file = os.path.join(exif_path, read_file)imge = Image(exif_file)exif = imge.read_exif()img.modify_exif(exif)imge.close()img.close()else:i=i+1print(str(file)+"unfinish")print('exifr&w finish')print(str(i) + " files can not be processed")print('end')use_tsdk(tsdk, path, savepath)
raw_to_tif(savepath, 512, 640, 1)
exifrw(savepath,path)