Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable loading TIFF with float or 32 bits #228

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/idvc/dvc_runner.py
paskino marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ def set_up(self, *args, **kwargs):

message_callback.emit("Creating run configurations")
vol_bit_depth = int(config['vol_bit_depth'])
if vol_bit_depth not in ['8', '16']:
# the data will be converted to 16 bit by save_tiff_stack_as_raw
# it won't work with other formats
vol_bit_depth = 16
vol_hdr_lngth = int(config['vol_hdr_lngth'])

if 'vol_endian' in config:
Expand Down
42 changes: 12 additions & 30 deletions src/idvc/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def loadTif(*args, **kwargs):
target_size = kwargs.get('target_size', 0.125)
origin = kwargs.get('origin', (0, 0, 0))
target_z_extent = kwargs.get('target_z_extent', (0, 0))
bits_per_byte = 8
bits_per_byte = 16

# time.sleep(0.1) #required so that progress window displays
# progress_callback.emit(10)
Expand Down Expand Up @@ -530,36 +530,9 @@ def loadTif(*args, **kwargs):
getProgress, progress_callback=progress_callback))
reader.SetFileName(filenames)

dtype = vtk.VTK_UNSIGNED_CHAR

if reader.GetOutput().GetScalarType() != dtype and False:
# need to cast to 8 bits unsigned
print("The if statement is true")


stats = vtk.vtkImageAccumulate()
stats.SetInputConnection(reader.GetOutputPort())
stats.Update()
iMin = stats.GetMin()[0]
iMax = stats.GetMax()[0]
if (iMax - iMin == 0):
scale = 1
else:
scale = vtk.VTK_UNSIGNED_CHAR_MAX / (iMax - iMin)

shiftScaler = vtk.vtkImageShiftScale()
shiftScaler.SetInputConnection(reader.GetOutputPort())
shiftScaler.SetScale(scale)
shiftScaler.SetShift(-iMin)
shiftScaler.SetOutputScalarType(dtype)
shiftScaler.Update()

tmpdir = tempfile.gettempdir()
writer = vtk.vtkMetaImageWriter()
writer.SetInputConnection(shiftScaler.GetOutputPort())
writer.SetFileName(os.path.join(tmpdir, 'input8bit.mhd'))
writer.Write()

reader = shiftScaler

reader.Update()

shape = reader.GetStoredArrayShape()
Expand Down Expand Up @@ -1019,6 +992,15 @@ def save_tiff_stack_as_raw(filenames: list, output_fname: str, progress_callback
reader.SetFileName(el)
reader.Update()
slice_data = Converter.vtk2numpy(reader.GetOutput())
if slice_data.dtype not in [numpy.uint8, numpy.uint16, numpy.int8, numpy.int16]:
# scale to uint16
convert_to_dtype = numpy.uint16
# rescale as float32
slice_data = slice_data.astype(numpy.float32)
m,M = slice_data.min(), slice_data.max()
slice_data = (slice_data - m) / (M - m) * numpy.iinfo(convert_to_dtype).max
# finally cast to uint16
slice_data = slice_data.astype(numpy.uint16)
f.write(slice_data.tobytes())
progress_callback.emit(int(start_progress + (end_progress - start_progress) * (filenames.index(el) / steps)))

Expand Down