Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 3b79f558 authored by NADAL Morgane's avatar NADAL Morgane
Browse files

modif FindVoxelDimensionInMicrons() w/ re expression + try-except

parent 0aa2d768
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,9 @@ import numpy as np_ ...@@ -34,6 +34,9 @@ import numpy as np_
import sys as sy_ import sys as sy_
from PIL import Image from PIL import Image
from PIL.ExifTags import TAGS from PIL.ExifTags import TAGS
import re
size_voxel_in_micron = None
def ImageVerification(image: array_t, channel: str) -> array_t: def ImageVerification(image: array_t, channel: str) -> array_t:
...@@ -70,6 +73,10 @@ def ImageVerification(image: array_t, channel: str) -> array_t: ...@@ -70,6 +73,10 @@ def ImageVerification(image: array_t, channel: str) -> array_t:
def IntensityNormalizedImage(image: array_t) -> array_t: def IntensityNormalizedImage(image: array_t) -> array_t:
'''
Relative normalization of the image between 0 and 1.
No division per 0 since the image should not be constant (ImageVerification function).
'''
# #
print('Relative Intensity Normalization between 0 and 1. Need to reevaluate the parameters !!!') print('Relative Intensity Normalization between 0 and 1. Need to reevaluate the parameters !!!')
...@@ -94,30 +101,35 @@ def IntensityNormalizedImage(image: array_t) -> array_t: ...@@ -94,30 +101,35 @@ def IntensityNormalizedImage(image: array_t) -> array_t:
# return result # return result
def VoxelDimensionInMicrons(data_path: str) -> array_t: def FindVoxelDimensionInMicrons(data_path: str) -> array_t:
# TODO Verify if metadata are in the same format for all the images - if not, add a raise error ? #
if size_voxel_in_micron is not None:
return np_.array(size_voxel_in_micron)
else:
print('The size of a voxel is not specified in the parameters.')
try:
with Image.open(data_path) as img: # Find the voxels dimensions in micron in the metadata.
meta_dict = {TAGS.get(key, 'missing'): img.tag[key] for key in
img.tag} # Use the exif tags into the image metadata
with Image.open(data_path) as img: # Find the voxels dimensions in micron in the metadata. # Decode the tags text
meta_dict = {TAGS.get(key, 'missing'): img.tag[key] for key in metadata = meta_dict['missing'].decode('utf8') # Decode the tags text
img.tag} # Use the exif tags into the image metadata metadata = metadata.replace('\x00', '')
# Decode the tags text voxel_size = [] # Initialize the list of voxel size in str
metadata = meta_dict['missing'].decode('utf8') # Decode the tags text for axe in 'XYZ':
metadata = metadata.replace('\x00', '') pattern = 'Voxel' + axe + '.+\= (\d.+E.\d.)' # Regular expression
voxel_size.append(re.findall(pattern, metadata)[0])
voxel_size = ['', '', ''] # Initialize the list of voxel size in str voxel_size = np_.array(list(map(float, voxel_size)))
for idaxe, axe in enumerate('XYZ'): voxel_size_microns = 1.0e6 * voxel_size
idvox = metadata.find('dblVoxel' + axe) # Get the index of the voxel size for the axe X, Y, Z print('Voxel dimension in the image is [X Y Z] =', voxel_size_microns, 'in microns.')
idvox += 15 return voxel_size_microns
while metadata[idvox] != '\n':
voxel_size[idaxe] += metadata[idvox]
idvox += 1
voxel_size = np_.asarray(list(map(float, voxel_size)))
voxel_size_microns = 1.0e6 * voxel_size
print('Voxel dimension in the image is [X Y Z] =', voxel_size_microns, 'in microns. WARNING this method highly '
'depends on the format of the metadata.')
return voxel_size_microns except:
print('Unable to find the voxel dimensions in micron in the metadata. Please specify it in the parameters.')
def ToPixels(micron: float, voxel_size_microns: array_t) -> int: def ToPixels(micron: float, voxel_size_microns: array_t) -> int:
......
...@@ -89,7 +89,7 @@ start_time = tm_.time() ...@@ -89,7 +89,7 @@ start_time = tm_.time()
# --- Images # --- Images
voxel_micron = in_.VoxelDimensionInMicrons(data_path) voxel_micron = in_.FindVoxelDimensionInMicrons(data_path)
image = io_.imread(data_path) image = io_.imread(data_path)
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
data_path = "./data/DIO_6H_6_1.70bis_2.2_3.tif" data_path = "./data/DIO_6H_6_1.70bis_2.2_3.tif"
channel = 'G' channel = 'G'
size_voxel_in_micron = None # -> list [X,Y,Z]
with_plot = True with_plot = True
soma_low_c = 0.15 soma_low_c = 0.15
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment