Loading...
 

Useful Tcl procedures


This article contains some Tcl Huygens procedures that might be useful as examples of Tcl programming with the Huygens Software, or to use in new scripts.


Please login to view this page

Login



#   PURPOSE:
#   Basic tools: microscopy simulations, file conversion.
#
#   FILE USAGE:
#   Save the file with a name of your choice and the .tcl extension. Then
#   'source' it in Huygens. The below procedures can be called from the
#   Huygens Tcl Scripting shell.
#
#
#
#   Authors:
#   Dr. Jose Vina,
#   Daniel Sevilla.
#
#   Copyright: 
#   Scientific Volume Imaging Holding BV
#   Laapersveld 63,
#   1213 VB Hilversum,
#   The Netherlands,
#  
# 



# Convert the file type of all the files in a folder.
#
# USAGE example: fileConverter "/home" "/tmp" tiff hdf5
#
proc fileConverter { srcDir destDir inType outType } {

    # Retrieve the list of files stored in the source directory.
    set imgList [glob "${srcDir}*"]
    
    # Proceed to open the images and save them with a different extension.
    foreach image $imgList {
        
        # Check whether the image has the extension we are looking for.
        if { [string match "*.$inType" $image] } {
            
            # First open the image.
            set originalImg [img open $image]
            
            # Save the image with a different extension.
            $originalImg save "${destDir}$originalImg" -type $outType
            
            # Close the image to free memory.
            $originalImg del
        }
    }
}




# Adds photon noise up to a given SNR value to an image opened in Huygens.
#
# USAGE example: addNoise image 25
#
proc addNoise { image snr } {   

    # First check if the SNR value is sensible.
    if {$snr > 100 } {set snr 100}
    if {$snr < 3   } {set snr 3}

    # Find the image maximum intensity across all channels.
    set rangeList [$image range]
    set max 0
    foreach {maxTag maxValue minTag minValue} $rangeList {
        if {$maxValue > $max} {
            set max $maxValue
        }
    }

    # Calculate how much noise should be added.
    set ppu [expr {$snr * $snr / $max}]

    # Add noise.
    $image pnoise -> $image -ppu $ppu
}

# Simulates microscopy acquisition creating a synthetic bead and a PSF.
#
# USAGE example: simulateImageAcquisition 0.5 dest widefield 40
#
proc simulateImageAcquisition {beadSize result {mType "confocal"} {snr 100}} {

    # Create empty images.
    set beadImg [img create "beadImg" -type float -dim {32 32 32 0}]
    set fftBead [img create "fftBead" -type float -dim {32 32 32 0}]
    set theoPsf [img create "theoPsf" -type float -dim {32 32 32 0}]
    set fftPsf  [img create "psfFFT"  -type float -dim {32 32 32 0}]

    # Create a synthetic bead.
    $beadImg gensphere -> $beadImg -r $beadSize

    # Show the synthetic bead.
    $beadImg show

    # Set the microscope type, which plays a role in the acquisition result. 
    $beadImg setp -micr $mType

    # Calculate the FFT of the bead image.
    $beadImg fft -> $fftBead -dir f

    # Generate a theoretical PSF for the microscopic settings of the bead.
    $beadImg genpsf -> $theoPsf -dims parent

    # Calculate the FFT of the theoretical PSF.
    $theoPsf fft -> $fftPsf -dir f 

    # Multiply the two FFT's as in the Convolution Theorem.
    $fftBead * $fftPsf -> $result
    
    # Calculate the FFT of the resulting image to go back to real space.
    $result fft -> $result -dir i

    # Set the Fourier domain to 'optical representation'.
    $result optrep -> $result

    # Add photon noise to the result image.
    addNoise $result $snr
    
    # Delete the in-between results.
    $fftBead del
    $fftPsf  del
    $theoPsf del
}