Commit d4b5f7f0 authored by kulvait's avatar kulvait

Enno Shieferecke work

parent 3be2c261
......@@ -9,6 +9,7 @@ In this file, we work with row major image data indexing them (i,j)=(row,col)
import numpy as np
import os
import cv2
from pathlib import Path
#Get frame from DEN file
#Can get a subframe, where row_from is the first row index
......@@ -16,6 +17,7 @@ import cv2
#col_from is the first col index
#col_to is not included
def getFrame(file, i, row_from=None, row_to=None, col_from=None, col_to=None):
# i is the slice index
header = np.fromfile(file, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
......@@ -27,6 +29,7 @@ def getFrame(file, i, row_from=None, row_to=None, col_from=None, col_to=None):
col_from = 0
if col_to is None:
col_to = columns
f = open(file, "rb")
f.seek(6+rows*columns*4*i, os.SEEK_SET)
data = np.fromfile(f, np.dtype('<f4'), rows*columns)
......@@ -34,6 +37,85 @@ def getFrame(file, i, row_from=None, row_to=None, col_from=None, col_to=None):
newdata = newdata[row_from:row_to, col_from:col_to]
return(newdata)
def storeToDEN(file, data, force=False):
my_file = Path(file)
if not force and my_file.is_file():
print('File already exists, no data written')
return False
if not isinstance(data, np.ndarray):
print('Data has to be of type numpy.array')
return False
if len(data.shape) == 1:
print('Dimension = 1, expected >= 2')
return False
elif len(data.shape) == 2:
writeHeader(file, rows=data.shape[0], cols=data.shape[1], zdim=1, force=force)
writeFrame(file, 0, data, force=force)
return True
i = data.shape[2]
writeEmptyDEN(file, dimx=data.shape[0], dimy=data.shape[1], dimz=data.shape[2], force=force)
header = np.fromfile(file, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
f = open(file, "wb")
header.tofile(f)
for frame in range(i):
newdata = np.array(data[:, :, frame], np.dtype('<f4'))
newdata = newdata.reshape((rows*columns, ))
# put header in front of image data
f.seek(6 + rows * columns * frame * 4, os.SEEK_SET)
newdata.tofile(f)
f.close()
return True
def writeFrame(file, i, data, force=False):
my_file = Path(file)
if not force and my_file.is_file():
print('File already exists, no data written')
return False
dim = data.shape
if len(dim) == 1:
print("Dimension = 1, expected 2")
return False
elif len(dim) == 3:
print("Dimension = 3, expected 2")
elif len(dim) == 2:
pass
else:
raise Exception('Image Dimension Mismatch')
header = np.fromfile(file, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
f = open(file, "wb")
data = np.array(data, np.dtype('<f4'))
data = data.reshape((rows*columns, ))
# put header in front of image data
header.tofile(f)
f.seek(6 + rows * columns * i * 4, os.SEEK_SET)
data.tofile(f)
f.close()
return True
def writeEmptyDEN(file, dimx, dimy, dimz, force=False):
my_file = Path(file)
if not force and my_file.is_file():
print('File already exists, no header written')
return False
outfile = open(file, "w")
header = np.array([dimx, dimy, dimz])
header = np.array(header, dtype='<i2')
header.tofile(outfile)
outfile.close()
return True
def readHeader(file):
header = np.fromfile(file, np.dtype('<i2'), 3)
par = {}
......
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