def test_read_3D(self): sp_img_inr = imread(data_path('time_0_cut.inr')) img_path_tiff = data_path('time_0_cut.tiff') imsave(img_path_tiff, sp_img_inr) sp_img_tiff = imread(img_path_tiff) self.assertDictEqual(sp_img_inr.get_metadata(), sp_img_tiff.get_metadata()) np.testing.assert_array_equal(sp_img_inr, sp_img_tiff)
def test_plugin(): im = imread(data_path('segmentation_src.inr')) im_ref = imread(data_path('segmentation_seeded_watershed.inr')) smooth_img = linear_filtering(im, std_dev=2.0, method='gaussian_smoothing') regext_img = h_transform(smooth_img, h=5, method='h_transform_min') conn_img = region_labeling(regext_img, low_threshold=1, high_threshold=3, method='connected_components') wat_img = segmentation(smooth_img, conn_img, control='first', method='seeded_watershed') np.testing.assert_array_equal(wat_img, im_ref)
def test_gaussian_filtering(): """ Gaussian filter : $ linearfilter filtering_src.inr.gz filtering_linearfilter_sigma.inr.gz -smoothing -sigma 3 -o 1 """ #--- apply linear filter (sigma==3) sp_img_ref = imread(data_path('filtering_linearfilter_sigma_3.inr')) sp_img = imread(data_path('filtering_src.inr')) filtered = linearfilter(sp_img, '-smoothing -sigma 3.0') np.testing.assert_array_equal(filtered, sp_img_ref)
def test_segmentation(): im = imread(data_path('segmentation_src.inr')) im_ref = imread(data_path('segmentation_seeded_watershed.inr')) smooth_img = linearfilter(im, param_str_2="-smoothing -sigma 2.0") regext_img = regionalext(smooth_img, param_str_2="-minima -connectivity 26 -h 5") conn_img = connexe(regext_img, param_str_2="-low-threshold 1 -high-threshold 3 -labels -connectivity 26") wat_img = watershed(smooth_img, conn_img) # Uncomment clean (rm -vf ...) in gen_image.sh script and uncomment next lines to test step results #np.testing.assert_array_equal(smooth_img, data('segmentation_smooth.inr')) #np.testing.assert_array_equal(regext_img, data('segmentation_regext.inr')) #np.testing.assert_array_equal(conn_img, data('segmentation_connexe.inr')) np.testing.assert_array_equal(wat_img, im_ref)
def test_plugin_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) deformable_trsf = data_path("deformable_0_1.trsf") trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) trsf_out, res = registration(floating_img, reference_img, method='deformable_registration') np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def test_blockmatching_rigid(self): #--- rigid registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") trsf_out, res = blockmatching(floating_img, reference_img) trsf_inp = BalTransformation() trsf_inp.read(rigid_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def test_segmentation(): im = imread(data_path('segmentation_src.inr')) im_ref = imread(data_path('segmentation_seeded_watershed.inr')) smooth_img = linearfilter(im, param_str_2="-smoothing -sigma 2.0") regext_img = regionalext(smooth_img, param_str_2="-minima -connectivity 26 -h 5") conn_img = connexe( regext_img, param_str_2= "-low-threshold 1 -high-threshold 3 -labels -connectivity 26") wat_img = watershed(smooth_img, conn_img) # Uncomment clean (rm -vf ...) in gen_image.sh script and uncomment next lines to test step results #np.testing.assert_array_equal(smooth_img, data('segmentation_smooth.inr')) #np.testing.assert_array_equal(regext_img, data('segmentation_regext.inr')) #np.testing.assert_array_equal(conn_img, data('segmentation_connexe.inr')) np.testing.assert_array_equal(wat_img, im_ref)
def test_blockmatching_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") deformable_trsf = data_path("deformable_0_1.trsf") init_result_transformation = BalTransformation() init_result_transformation.read(rigid_trsf) param_str_2 = '-trsf-type vectorfield' trsf_out, res = blockmatching(floating_img, reference_img, init_result_transformation=init_result_transformation, left_transformation=None, param_str_2=param_str_2) trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def test_blockmatching_basic(self): img_src = imread(data_path('filtering_src.inr')) trsf_out, res = blockmatching(img_src, img_src) identity_trsf = create_trsf(param_str_2='-identity') np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), identity_trsf.mat.to_np_array(), decimal=8) np.testing.assert_array_almost_equal(res, img_src, decimal=8) identity_trsf.free() trsf_out.free() return
def test_blockmatching_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") deformable_trsf = data_path("deformable_0_1.trsf") init_result_transformation = BalTransformation() init_result_transformation.read(rigid_trsf) param_str_2 = '-trsf-type vectorfield' trsf_out, res = blockmatching( floating_img, reference_img, init_result_transformation=init_result_transformation, left_transformation=None, param_str_2=param_str_2) trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
# WARNING : currently, it seems that tests cannot be run in parallel. # Need to check and fix it! import numpy as np try: from timagetk.util import data_path from timagetk.components import imread from timagetk.algorithms import compose_trsf, inv_trsf, apply_trsf, create_trsf from timagetk.wrapping.bal_trsf import BalTransformation except ImportError: raise ImportError('Import Error') #--- Images img_src = imread(data_path("filtering_src.inr")) arbitrary_transformed_ref = imread(data_path("transformed_img.inr")) deformable_transformed_ref = imread(data_path("deformed_img.inr")) #--- Transformations trsf_arbitrary_path = data_path("arbitrary_trsf.trsf") trsf_deformable_path = data_path("deformable_trsf.trsf") trsf_composed_path = data_path("composed_trsf.trsf") inv_trsf_ref_path = data_path("arbitrary_trsf_inv.trsf") r = np.arange(12) mat_ref = np.array(np.reshape(r, (3, 4)), np.float64) def test_create_trsf(): #--- create identity transformation
def test_plugin_2(): im = imread(data_path('filtering_src.inr')) im_ref = imread(data_path('morpho_erosion_default.inr')) output = morphology(im, method='erosion') np.testing.assert_array_equal(output, im_ref)
def test_erosion(): #--- erosion im = imread(data_path('filtering_src.inr')) im_ref = imread(data_path('morpho_erosion_default.inr')) output = morpho(im, param_str_2='-erosion') np.testing.assert_array_equal(output, im_ref)
# imports try: from timagetk.util import data_path from timagetk.components import imread, imsave from timagetk.plugins import linear_filtering, morphology from timagetk.plugins import h_transform from timagetk.plugins import region_labeling, segmentation from timagetk.plugins import labels_post_processing except ImportError: raise ImportError('Import Error') out_path = './results/' # to save results # we consider an input image # SpatialImage instance input_img = imread(data_path('input.tif')) # optional denoising block smooth_img = linear_filtering(input_img, std_dev=2.0, method='gaussian_smoothing') asf_img = morphology(smooth_img, max_radius=3, method='co_alternate_sequential_filter') ext_img = h_transform(asf_img, h=150, method='h_transform_min') con_img = region_labeling(ext_img, low_threshold=1, high_threshold=150, method='connected_components') seg_img = segmentation(smooth_img, con_img, control='first', method='seeded_watershed') # optional post processig block
# Copyright 2016 INRIA # # File author(s): # Guillaume Baty <*****@*****.**> # Sophie Ribes <*****@*****.**> # # See accompanying file LICENSE.txt #------------------------------------------------------------------------------ import numpy as np try: from timagetk.util import data_path from timagetk.components import imread from timagetk.algorithms import mean_images from timagetk.plugins import averaging except ImportError: raise ImportError('Import Error') image_1 = imread(data_path('filtering_src.inr')) image_2 = imread(data_path('transformed_img.inr')) mean_image = imread(data_path('mean_image.inr')) def test_mean_image(): out = mean_images([image_1, image_2]) np.testing.assert_array_equal(out, mean_image) def test_plugin(): out_1 = averaging([image_1, image_2], method='mean_averaging') out_2 = mean_images([image_1, image_2]) np.testing.assert_array_equal(out_1, out_2)
def test_erosion(): sp_img_ref = imread(data_path('eroded_segmentation.inr')) sp_img = imread(data_path('segmentation_seeded_watershed.inr')) output = cell_filter(sp_img, param_str_2='-erosion -R 1') np.testing.assert_array_equal(output, sp_img_ref)
def test_pluginmin(): im = imread(data_path('filtering_src.inr')) im_ref = imread(data_path('regionalmin_default.inr')) output = h_transform(im, h=3, method='h_transform_min') np.testing.assert_array_equal(output, im_ref)
from timagetk.components import imread, imsave from timagetk.plugins import linear_filtering, morphology from timagetk.plugins import h_transform from timagetk.plugins import region_labeling, segmentation from timagetk.plugins import labels_post_processing #except ImportError: # raise ImportError('Import Error') #out_path = './results/' # to save results #if not os.path.isdir(out_path): # new_fold = os.path.join(os.getcwd(),'results') # os.mkdir(new_fold) # we consider an input image # SpatialImage instance input_img = imread(sys.argv[1]) print input_img if input_img.dtype == np.uint8: input_img = input_img.astype(np.uint16) input_img *= 256 # optional denoising block smooth_img = linear_filtering(input_img, std_dev=float(sys.argv[4]), method='gaussian_smoothing') asf_img = morphology(smooth_img, max_radius=1, method='co_alternate_sequential_filter')
try: from timagetk.components import SpatialImage from timagetk.components import imread, imsave from timagetk.util import data_path except ImportError: raise ImportError('Import Error') out_path = './results/' # to save results if not os.path.isdir(out_path): new_fold = os.path.join(os.getcwd(), 'results') os.mkdir(new_fold) # input image path img_path = data_path('time_0_cut.inr') # .inr format to SpatialImage sp_img = imread(img_path) # sp_img is now a SpatialImage instance if isinstance(sp_img, SpatialImage): # get the SpatialImage metadata, accessor metadata = sp_img.get_metadata() # dictionary # print the SpatialImage metadata print('Metadata :'), metadata # metadata modification metadata['filename'] = img_path # set the SpatialImage metadata, mutator sp_img.set_metadata(metadata) # print the SpatialImage metadata print('New metadata :'), sp_img.get_metadata() # get the SpatialImage voxelsize (list of floating numbers)
import numpy as np try: from timagetk.util import data_path from timagetk.components import imread from timagetk.algorithms import GeometricalFeatures except ImportError: raise ImportError('Import Error') try: import cPickle as pickle except: import pickle out_path = './results/' # to save results # We consider two different times times = [3,4] list_images = [imread(data_path('time_' + str(time) + '_seg.inr')) for time in times] # list of SpatialImage feature_space_list = [] for ind, time in enumerate(times): segmentation = list_images[ind] # segmentations labels = np.unique(segmentation).tolist() # labels background_id = np.min(labels) labels.remove(background_id) # remove background int_seg = GeometricalFeatures(segmentation, label=labels) # GeometricalFeatures object feature_space_dict = int_seg.compute_feature_space(background_id=background_id) # compute feature space feature_space_list.append(feature_space_dict) for ind, time in enumerate(times): pkl_path = ''.join([out_path, 'example_geom_feat_time_', str(times[ind]), '.pkl']) out = open(str(pkl_path), 'wb') # save as pickle object (serialization)
def test_regionalmin(): im = imread(data_path('filtering_src.inr')) im_ref = imread(data_path('regionalmin_default.inr')) output = regionalext(im, param_str_2="-minima -connectivity 26 -h 3") np.testing.assert_array_equal(output, im_ref)
# Guillaume Baty <*****@*****.**> # Sophie Ribes <*****@*****.**> # # See accompanying file LICENSE.txt #------------------------------------------------------------------------------ import unittest import numpy as np try: from timagetk.components import SpatialImage, imread from timagetk.util import data_path from timagetk.wrapping.bal_image import BalImage, spatial_image_to_bal_image except ImportError: raise ImportError('Import Error') image = imread(data_path('filtering_src.inr')) class TestBalImage(unittest.TestCase): def check_image(self, sp_image_ref, bal_image): """ sp_image_ref: SpatialImage instance bal_image: bal_image instance """ sp_image = bal_image.to_spatial_image() np.testing.assert_array_almost_equal(sp_image_ref, sp_image, decimal=6) ix, iy, iz = sp_image_ref.get_voxelsize() nx, ny, nz = sp_image.get_voxelsize() np.testing.assert_almost_equal(ix, nx, decimal=6) np.testing.assert_almost_equal(iy, ny, decimal=6)
# Guillaume Baty <*****@*****.**> # Sophie Ribes <*****@*****.**> # # See accompanying file LICENSE.txt #------------------------------------------------------------------------------ import unittest import numpy as np try: from timagetk.components import SpatialImage, imread from timagetk.util import data_path from timagetk.wrapping.bal_image import BalImage, spatial_image_to_bal_image except ImportError: raise ImportError('Import Error') image = imread(data_path('filtering_src.inr')) class TestBalImage(unittest.TestCase): def check_image(self, sp_image_ref, bal_image): """ sp_image_ref: SpatialImage instance bal_image: bal_image instance """ sp_image = bal_image.to_spatial_image() np.testing.assert_array_almost_equal(sp_image_ref, sp_image, decimal=6) ix, iy, iz = sp_image_ref.get_voxelsize() nx, ny, nz = sp_image.get_voxelsize() np.testing.assert_almost_equal(ix, nx, decimal=6) np.testing.assert_almost_equal(iy, ny, decimal=6) np.testing.assert_almost_equal(iz, nz, decimal=6)
from timagetk.algorithms import GeometricalFeatures from timagetk.algorithms import TemporalMatching except ImportError: raise ImportError('Import Error') try: import cPickle as pickle except: import pickle out_path = './results/' # We consider two different times times = [3,4] segmentation_list, feature_space_list, back_id_list = [], [], [] for ind, val in enumerate(times): img = imread(data_path('time_' + str(val) + '_seg.inr')) # subimage extraction shape = img.get_shape() indices = [0,shape[0]-1,0,shape[1]-1,0,5] img = img.get_region(indices=indices) # remove small cells labels = np.unique(img).tolist() img = labels_post_processing(img, method='labels_erosion', radius=2) img[img==0] = np.min(labels) img = SpatialImage(img, voxelsize=img.get_voxelsize()) # save input labeled images res_name = 'example_track_time_' + str(val) + '_seg.inr' imsave(out_path+res_name, img)
# imports try: from timagetk.components import SpatialImage from timagetk.components import imread, imsave from timagetk.util import data_path except ImportError: raise ImportError('Import Error') out_path = './results/' # to save results # input image path img_path = data_path('time_0_cut.inr') # .inr format to SpatialImage sp_img = imread(img_path) # sp_img is now a SpatialImage instance if isinstance(sp_img, SpatialImage): # get the SpatialImage metadata, accessor metadata = sp_img.get_metadata() # dictionary # print the SpatialImage metadata print('Metadata :'), metadata # metadata modification metadata['filename'] = img_path # set the SpatialImage metadata, mutator sp_img.set_metadata(metadata) # print the SpatialImage metadata print('New metadata :'), sp_img.get_metadata() # get the SpatialImage voxelsize (list of floating numbers) vox = sp_img.get_voxelsize() # print the SpatialImage voxelsize print('Voxelsize :'), vox
# File author(s): # Guillaume Baty <*****@*****.**> # Sophie Ribes <*****@*****.**> # # See accompanying file LICENSE.txt #------------------------------------------------------------------------------ import numpy as np try: from timagetk.util import data_path from timagetk.components import imread from timagetk.algorithms import mean_images from timagetk.plugins import averaging except ImportError: raise ImportError('Import Error') image_1 = imread(data_path('filtering_src.inr')) image_2 = imread(data_path('transformed_img.inr')) mean_image = imread(data_path('mean_image.inr')) def test_mean_image(): out = mean_images([image_1, image_2]) np.testing.assert_array_equal(out, mean_image) def test_plugin(): out_1 = averaging([image_1, image_2], method='mean_averaging') out_2 = mean_images([image_1, image_2]) np.testing.assert_array_equal(out_1, out_2)
def test_linear_filter(): #--- apply default linear filter sp_img_ref = imread(data_path('filtering_linearfilter_default.inr')) sp_img = imread(data_path('filtering_src.inr')) filtered = linearfilter(sp_img) np.testing.assert_array_equal(filtered, sp_img_ref)
try: from timagetk.util import data_path from timagetk.components import imread, imsave from timagetk.plugins import registration #from timagetk.wrapping.bal_trsf import BalTransformation except ImportError: raise ImportError('Import Error') out_path = './results/' # to save results # we consider two different times # time_1 is the floating image # time_2 is the reference image times = [1, 2] # list of SpatialImage instances list_images = [ imread(data_path('time_' + str(time) + '.inr')) for time in times ] floating_img, reference_img = list_images[0], list_images[1] # Rigid registration: trsf_rig, res_rig = registration(floating_img, reference_img, method='rigid_registration') # display the spatial transformation (4x4 matrix): trsf_rig.c_display() # save the spatial transformation: res_name = 'example_trsf_rigid.trsf' # filename trsf_rig.write(out_path + res_name) # save the result image: res_name = 'example_reg_rigid_1_2.tif' # filename # SpatialImage to .tif format
def test_plugin_2(): sp_img_ref = imread(data_path('filtering_linearfilter_sigma_3.inr')) sp_img = imread(data_path('filtering_src.inr')) filtered = linear_filtering(sp_img, std_dev=3.0, method='gaussian_smoothing') np.testing.assert_array_equal(filtered, sp_img_ref)
#------------------------------------------------------------------------------ # WARNING : currently, it seems that tests cannot be run in parallel. # Need to check and fix it! import numpy as np try: from timagetk.util import data_path from timagetk.components import imread from timagetk.algorithms import compose_trsf, inv_trsf, apply_trsf, create_trsf from timagetk.wrapping.bal_trsf import BalTransformation except ImportError: raise ImportError('Import Error') #--- Images img_src = imread(data_path("filtering_src.inr")) arbitrary_transformed_ref = imread(data_path("transformed_img.inr")) deformable_transformed_ref = imread(data_path("deformed_img.inr")) #--- Transformations trsf_arbitrary_path = data_path("arbitrary_trsf.trsf") trsf_deformable_path = data_path("deformable_trsf.trsf") trsf_composed_path = data_path("composed_trsf.trsf") inv_trsf_ref_path = data_path("arbitrary_trsf_inv.trsf") r = np.arange(12) mat_ref = np.array(np.reshape(r, (3, 4)), np.float64) def test_create_trsf(): #--- create identity transformation identity = create_trsf(param_str_2='-identity',