Skip to content
Snippets Groups Projects
Commit d5982750 authored by Kai Schleicher's avatar Kai Schleicher
Browse files

Remove unused functions and imports

parent 48234c5b
No related branches found
No related tags found
1 merge request!2Initial commit
...@@ -12,13 +12,11 @@ ...@@ -12,13 +12,11 @@
# trackmate imports # trackmate imports
from fiji.plugin.trackmate import Settings from fiji.plugin.trackmate import Settings
from fiji.plugin.trackmate import TrackMate from fiji.plugin.trackmate import TrackMate
from fiji.plugin.trackmate.cellpose import CellposeDetectorFactory
from fiji.plugin.trackmate.cellpose.CellposeSettings import PretrainedModel
from fiji.plugin.trackmate.detection import DogDetectorFactory from fiji.plugin.trackmate.detection import DogDetectorFactory
from fiji.plugin.trackmate.action import LabelImgExporter from fiji.plugin.trackmate.action import LabelImgExporter
# ij imports # ij imports
from ij import IJ from ij import IJ
from ij.plugin import ZProjector, ImageCalculator from ij.plugin import ImageCalculator
from ij.plugin.frame import RoiManager from ij.plugin.frame import RoiManager
from ij.measure import ResultsTable from ij.measure import ResultsTable
# Bio-Formats imports # Bio-Formats imports
...@@ -28,8 +26,6 @@ from loci.plugins.in import ImporterOptions ...@@ -28,8 +26,6 @@ from loci.plugins.in import ImporterOptions
from loci.formats import ImageReader, MetadataTools, Memoizer from loci.formats import ImageReader, MetadataTools, Memoizer
# MorpholibJ imports # MorpholibJ imports
from inra.ijpb.binary import BinaryImages from inra.ijpb.binary import BinaryImages
# CLIJ imports
from net.haesleinhuepf.clij2 import CLIJ2
# imglib2 imports # imglib2 imports
from net.imglib2.img.display.imagej import ImageJFunctions from net.imglib2.img.display.imagej import ImageJFunctions
from net.imglib2.roi import Regions from net.imglib2.roi import Regions
...@@ -61,114 +57,6 @@ def get_parentdir_filename_ext_from_path(path): ...@@ -61,114 +57,6 @@ def get_parentdir_filename_ext_from_path(path):
return (parent_dir, filename, ext) return (parent_dir, filename, ext)
def tile_image(image_width, image_height, tile_width, tile_height, overlap):
""" Calculate tiles of an image of given its dimensions. Calculates tile start coordinates and tile dimensions including overlap,
which can directly be used in Bio-Formats "crop" function. Also calculates tile crop start coordinates and dimensions
to remove the overlap and the crops start coordinates to re-assemble the original size image. Useful to e.g. segment a large
already stitched image tile by tile.
Parameters
----------
image_width : int
the input images width in pixels
image_height : int
the input images height in pixels
tile_width : int
the desired tile width in pixels
tile_height : int
the desired tile height in pixels
overlap : int
the desired tile overlap in pixels
Returns
-------
list
all_tile_regions: tile coordinates and dimensions including overlap
all_crop_regions: tile coordinates and dimensions to remove the overlap again
all_tile_start_no_overlap: start coordinates of the tiles without overlap to re-assemble the original size image
"""
# TODO: this function likely needs refactoring to be more clear and readable
number_of_columns = image_width // tile_width # is a floored division
column_remainder_px = image_width % tile_width
number_of_rows = image_height // tile_height
row_remainder_px = image_height % tile_height
all_tile_regions = []
all_crop_regions = []
all_tile_start_no_overlap = []
for current_row in range(number_of_rows):
if current_row == number_of_rows -1 :
tile_height_boarder_addition = row_remainder_px # increase tile height in the last row
else:
tile_height_boarder_addition = 0
for current_column in range (number_of_columns):
if current_column == number_of_columns - 1 :
tile_width_boarder_addition = column_remainder_px # increase tile width in the last column
else:
tile_width_boarder_addition = 0
tile_start_x_no_overlap = current_column * tile_width
tile_start_y_no_overlap = current_row * tile_height
if current_column > 0:
tile_start_x = current_column * tile_width - overlap
crop_start_x = overlap
else:
tile_start_x = 0
crop_start_x = 0
if current_row > 0:
tile_start_y = current_row * tile_height - overlap
crop_start_y = overlap
else:
tile_start_y = 0
crop_start_y = 0
if current_column == 0 or current_column == number_of_columns - 1:
current_tile_width = tile_width + overlap + tile_width_boarder_addition
else:
current_tile_width = tile_width + 2 * overlap
if current_row == 0 or current_row == number_of_rows - 1:
current_tile_height = tile_height + overlap + tile_height_boarder_addition
else:
current_tile_height = tile_height + 2 * overlap
tile_region = [tile_start_x, tile_start_y, current_tile_width, current_tile_height]
crop_region = [crop_start_x, crop_start_y, current_tile_width - overlap, current_tile_height - overlap]
tile_start_no_overlap = [tile_start_x_no_overlap, tile_start_y_no_overlap]
all_tile_regions.append(tile_region)
all_crop_regions.append(crop_region)
all_tile_start_no_overlap.append(tile_start_no_overlap)
return all_tile_regions, all_crop_regions, all_tile_start_no_overlap
def create_empty_image(bit_depth, image_width, image_height):
"""creates an empty image of given dimensions. The image is 2D, filled with black pixels of value 0.
Parameters
----------
bit_depth : int
the image bit dept
image_width : int
image width in px
image_height : int
image height in px
Returns
-------
ImagePlus
the empty image
"""
canvas = IJ.createImage("mosaic", str(bit_depth) + "-bit black", image_width, image_height, 1)
return canvas
def write_bf_memoryfile(path_to_file): def write_bf_memoryfile(path_to_file):
"""Write a BF memo-file so subsequent access to the same file is faster. """Write a BF memo-file so subsequent access to the same file is faster.
...@@ -321,53 +209,6 @@ def run_trackmate_dog_spot_detector(imp, spot_diameter, quality_threshold): ...@@ -321,53 +209,6 @@ def run_trackmate_dog_spot_detector(imp, spot_diameter, quality_threshold):
return label_imp return label_imp
def run_trackmate_cellpose_detector(imp):
"""Run TrackMates CellPose detector on a target image
Parameters
----------
imp : ImagePlus
the input image
Returns
-------
ImagePlus
a label image of the detected regions.
"""
cal = imp.getCalibration()
settings = Settings(imp)
# TODO: expose more parameters
settings.detectorFactory = CellposeDetectorFactory()
settings.detectorSettings['TARGET_CHANNEL'] = 0
settings.detectorSettings['OPTIONAL_CHANNEL_2'] = 0
settings.detectorSettings['CELLPOSE_PYTHON_FILEPATH'] = 'S:/cellpose_env/python.exe'
settings.detectorSettings['CELLPOSE_MODEL_FILEPATH'] = ''
settings.detectorSettings['CELLPOSE_MODEL'] = PretrainedModel.CYTO
settings.detectorSettings['CELL_DIAMETER'] = 30.0
settings.detectorSettings['USE_GPU'] = True
settings.detectorSettings['SIMPLIFY_CONTOURS'] = True
trackmate = TrackMate(settings)
trackmate.computeSpotFeatures(False)
trackmate.computeTrackFeatures(False)
if not trackmate.checkInput():
print( str( trackmate.getErrorMessage() ) )
if not trackmate.process():
print( str( trackmate.getErrorMessage() ) )
exportSpotsAsDots = False
exportTracksOnly = False
label_imp = LabelImgExporter.createLabelImagePlus(trackmate, exportSpotsAsDots, exportTracksOnly, False)
label_imp.setCalibration(cal)
imp.close()
return label_imp
def get_threshold_from_method(imp, method): def get_threshold_from_method(imp, method):
"""returns the threshold value of chosen IJ AutoThreshold method from a stack (hyperstack?) """returns the threshold value of chosen IJ AutoThreshold method from a stack (hyperstack?)
...@@ -423,109 +264,6 @@ def convert_to_binary(imp, threshold=1, scale_binary=True): ...@@ -423,109 +264,6 @@ def convert_to_binary(imp, threshold=1, scale_binary=True):
return mask_imp return mask_imp
def erode_labelimp_gpu(label_imp, radius, relabel_islands=True):
"""Erode a label image on the GPU.
Parameters
----------
label_imp : ImagePlus
the input image
radius : float
radius used for erosion
relabel_islands : bool, optional
if True, split objects will get new labels each , by default True
Returns
-------
ImagePlus
the eroded label image
"""
clij2 = CLIJ2.getInstance()
labels_input = clij2.push(label_imp)
labels_destination = clij2.create(labels_input)
radius = 1.0
clij2.erodeLabels(labels_input, labels_destination, radius, relabel_islands)
eroded_labelimp = clij2.pull(labels_destination)
clij2.release(labels_input)
clij2.release(labels_destination)
label_imp.close()
return eroded_labelimp
def remove_tileoverlap(imp, crop_region):
"""Crop an image to specified dimensions.
The operation is destructive, the original image is lost.
Parameters
----------
imp : ImagePlus
the input image
crop_region : list or array of int
the crop region in the format of [start_x, start_y, crop_width, crop_height]
Returns
-------
ImagePlus
the cropped image
"""
start_x = crop_region[0]
start_y = crop_region[1]
crop_width = crop_region[2]
crop_height = crop_region[3]
imp.setRoi(start_x, start_y, crop_width, crop_height)
cropped_imp = imp.crop()
imp.close()
return cropped_imp
def copy_and_paste_image(source, target, start_coordinates_xy):
"""Copy the source image and paste it into the target image
Parameters
----------
source : ImagePlus
the source image
target : ImagePlus
the target image
start_coordinates_xy : list or array of int
the x and y coordinates in the target image where the source image will be pasted
"""
source.copy()
start_x = start_coordinates_xy[0]
start_y = start_coordinates_xy[1]
target.paste(start_x, start_y)
source.close()
def merge_touching_labels(label_imp, bit_depth):
"""merge touching labels in a label image so they have the same label ID.
Parameters
----------
label_imp : ImagePlus
the label image with touching labels of different label ID
bit_depth : int
the image bit depth
Returns
-------
ImagePlus
the label image with merged touching labels
"""
binary_imp = BinaryImages.binarize(label_imp)
connectivity = 8
merged_label_imp = BinaryImages.componentsLabeling(binary_imp, connectivity, bit_depth)
label_imp.close()
binary_imp.close()
return merged_label_imp
def save_image_as_IJtif(imp, filename, suffix, target): def save_image_as_IJtif(imp, filename, suffix, target):
"""Save the image as ImageJ-Tiff """Save the image as ImageJ-Tiff
...@@ -547,25 +285,6 @@ def save_image_as_IJtif(imp, filename, suffix, target): ...@@ -547,25 +285,6 @@ def save_image_as_IJtif(imp, filename, suffix, target):
IJ.saveAs(imp, "Tiff", savepath) IJ.saveAs(imp, "Tiff", savepath)
def measure_label_size_shape_2d(label_imp):
rm = RoiManager.getInstance()
if not rm:
rm = RoiManager()
rm.reset()
rt = ResultsTable()
command.run( Labels2Rois , False , 'imp' , label_imp , 'rm', rm).get()
IJ.run("Set Measurements...", "area centroid perimeter shape feret's redirect=None decimal=3")
rm.runCommand(label_imp,"Deselect")
rm.runCommand(label_imp,"Measure")
rm.reset()
IJ.renameResults("Results", "Size and shape features")
results_table = rt.getResultsTable("Size and shape features")
return results_table
def convert_labelimage_to_imglib2regions(imp): def convert_labelimage_to_imglib2regions(imp):
"""Convert a ImagePlus label image to imglib2 regions """Convert a ImagePlus label image to imglib2 regions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment