Commit b1e3ecce authored by Bryson Howell's avatar Bryson Howell

Started a python script to convert .mat matrices into formats useable in Unity

parent 3f2a0b02
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from PIL import Image
import cv2
#Loads matlab matrices created by feature_set.py as numpy arrays
def main():
#Load the matlab matrix
dir = './matlab_data/'
savedir = './unity_data/'
filename = 'BW_LFandInac_Zelev_kentland.mat'
matdict = loadmat(dir+filename, appendmat=False)
#Testing stuff
k = matdict.keys()
print(k)
#These are square numpy arrays of equal size
elev = matdict['sZelev'] #Values are height of grid cell (in meters)
linfeat = matdict['BWLF'] #Values are 1 for linear feature present, 0 for none
obstacles = matdict['BWInac'] #Values are 1 for obstacle, 0 for clear
make_heightmap(elev,savedir)
return
#Converts the terrain height data in the .mat files into a grayscale heightmap
#that can be loaded into Unity. Heightmaps are 16-bit grayscale images saved in
#the RAW format.
def make_heightmap(elev,savedir):
name = savedir + "kentland_heightmap.raw"
#fig = plt.figure()
#plt.imshow(elev)
#plt.show()
#Normalize values to be between 0-1 and preview as grayscale image
elev_gray = np.float16((elev-np.min(elev))/(np.max(elev)-np.min(elev)))
#elev_gray = np.ushort(elev)
#elev_gray = np.uint16(elev)
fig = plt.figure()
plt.imshow(elev_gray)
plt.show()
f = open(name,'w')
#Convert to raw format image
img = Image.fromarray(elev_gray)
img = img.convert('L')
#img = img.convert('I;16')
img.save(name, 'PNG') #Should I add L or B to the end (endian)
img.show()
#cv2.imshow('Kentland Heightmap',elev_gray)
#cv2.waitKey(0)
#cv2.imwrite('kentland_heightmap.raw',elev_gray)
return
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment