Commit b28f442d authored by haseljic's avatar haseljic

compareDEN function added and changes to writeEmptyHeader and writeFrame

parent f22dac26
......@@ -10,6 +10,7 @@ DEN and DICOM IO manipulation
"""
import numpy as np
import skimage
import os
import cv2
......@@ -22,7 +23,7 @@ import cv2
def getFrame(fileName, 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)
header = np.fromfile(fileName, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
if row_from is None:
......@@ -60,7 +61,7 @@ def storeNdarrayAsDEN(fileName, dataFrame, force=False):
header = np.fromfile(fileName, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
f = open(file, "wb")
f = open(fileName, "wb")
header.tofile(f)
for frame in range(i):
newdata = np.array(data[:, :, frame], np.dtype('<f4'))
......@@ -78,16 +79,16 @@ def writeFrame(fileName, k, data, force=False):
shape = data.shape
if len(shape) != 2:
raise ValueError('Dimension of data should be 2 %d.' % len(shape))
header = np.fromfile(file, np.dtype('<i2'), 3)
header = np.fromfile(fileName, np.dtype('<i2'), 3)
rows = np.uint32(header[0])
columns = np.uint32(header[1])
if shape[0] != rows or shape[1] != columns:
raise ValueError('There is dimension mismatch between frame (%d, %d) and expected (rows, cols) = (%d, %d) according to header.' %
(rows, columns, shape[0], shape[1]))
f = open(file, "wb+")
f = open(fileName, "ab")
data = np.array(data, np.dtype('<f4'))
data = data.reshape((rows * columns, ))
f.seek(6 + rows * columns * k * 4, os.SEEK_SET)
data = data.reshape((rows * columns))
#f.seek(6 + rows * columns * k * 4, os.SEEK_SET)
data.tofile(f)
f.close()
......@@ -95,13 +96,13 @@ def writeFrame(fileName, k, data, force=False):
def writeEmptyDEN(fileName, dimx, dimy, dimz, force=False):
if not force and os.path.exists(fileName):
raise IOError('File %s already exists, no header written' % fileName)
outfile = open(file, "w")
outfile = open(fileName, "w")
header = np.array([dimx, dimy, dimz])
header = np.array(header, dtype='<i2')
header.tofile(outfile)
fileSize = dimx * dimy * dimz * 4 + 6
outfile.seek(fileSize - 1)
outfile.write('\0')
#fileSize = dimx * dimy * dimz * 4 + 6
#outfile.seek(fileSize - 1)
#outfile.write('\0')
outfile.close()
......@@ -123,3 +124,31 @@ def readHeader(file):
def trimFrame(frame, row_from, row_to, col_from, col_to):
newdata = frame[row_from:row_to, col_from:col_to]
return(newdata)
def compareDEN(a_path, b_path):
a_header = readHeader(a_path)
b_header = readHeader(b_path)
if not np.array_equal(a_header, b_header):
exit()
num_of_frames = np.uint32(a_header["zdim"])
mse = np.zeros(num_of_frames)
ssim = np.empty(num_of_frames)
#num_of_frames = 2
writeEmptyDEN('ssim.den', np.uint32(a_header["rows"]), np.uint32(a_header["cols"]), num_of_frames, True)
i = 0
while (i < num_of_frames):
a = getFrame(a_path, i, row_from=None, row_to=None, col_from=None, col_to=None)
b = getFrame(b_path, i+1, row_from=None, row_to=None, col_from=None, col_to=None)
mse[i] = skimage.measure.compare_nrmse(a, b, 'EUCLIDEAN')
ssim[i], smap = skimage.measure.compare_ssim(a, b, win_size=None, gradient=False, data_range=None, multichannel=False, gaussian_weights=False, full=True)
writeFrame('ssim.den', i, smap, force=True)
i+=1
return (mse, ssim)
\ No newline at end of file
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