Skip to content

Segment Each Structure

Lauren Beck edited this page Dec 6, 2019 · 11 revisions

The instructions in this section apply to any structure that you want to segment, but I will refer to segmenting the nuclei as an example.

Guess the 2D Segmentation

Here, we will use an algorithm (of the user's choosing) to guess the 2D segmentation for the structure.

  1. For each structure you want to segment, make a folder called segmentations_X where X is nuclei, lumens, organoid, or buds. This folder should be in the data folder.

  2. Make the segmentations folder MATLAB's working directory.

    >> cd segmentations_nuclei;
  3. Guess the 2D segmentation.

    >> organoids2.guess_2D_segmentations;
  4. Select the algorithm you want to use. Technically, you can use any algorithm for any structure (organoids, lumens, nuclei, buds), but certain algorithms are best for different structures (see below).

    • Using nucleaizer - useful for nuclei - This will prepare the data so that you can run nucleaizer (on the lab GPU).
    • Using flood algorithm - useful for lumens and organoids - This will attempt the guess the segmentation automatically.
    • Using GUI to draw on max merge - useful for buds - This will open a GUI that allows you to draw the 2D segmentation over the max merge of the image. It is useful when the 2D segmentation does not change much throughout the stack (i.e. for buds).
  5. Depending on the algorithm you selected, continue the guessing.

    Nucleaizer

    1. Tell the code what channel you would like to use (dapi).

    2. Copy the "slices_XY" and "slices_XZ" folders from the "segmentations_nuclei" folder to a USB drive.

      1. Alternatively, if you want to run nucleaizer on multiple main folders, you can run an optional script that will format the images so you need only run nucleaizer once. The input to this script should be a cell of paths to the main folders to include.

        >> cd /Volumes/LAUREN_TEMP/
        >> path_to_images = {'~/Desktop/well06_5ngmLHGF', '~/Desktop/well07_20ngmLHGF'}; 
        >> run_nucleaizer_batch_preprocess(paths_to_images);
        
    3. Plug that USB drive into the lab's GPU.

    4. On the GPU, open the Command Prompt.

    5. In the Command Prompt, change the directory to the one containing the nucleaizer code.

      $ cd \Users\lab\Documents\nucleaizer\biomagdsb-master
      
    6. In the Command Prompt, enter the virtual environment containing the relevant Python code.

      $ env\Scripts\activate
      
    7. In Notepad, open the start_prediction_fast.bat file (found in biomagdsb-master folder). Edit the line to store the path to the images to segment. Save the changes. For example, the line might read:

      set "images_dir=D:\slices_XY"

    8. Make sure the following folders are empty. This is important because all images in the first folder (NOT just the images in the directory you pointed the code to) are segmented.

      • biomagdsb-master\kaggle_workflow\outputs\images
      • biomagdsb-master\kaggle_workflow\outputs\presegment
    9. In the Command Prompt, start nucleaizer.

      $ start_prediction_fast
      
    10. Once done, copy the contents of the "biomagdsb-master/kaggle_workflow/outputs/presegment" folder to "slices_XY/results" folder.

    11. Repeat for the "slices_XZ" folder. This will segment the orthogonal images so we can connect the 2D segmentations into 3D segmentations.

    12. Plug the USB back into your computer and copy the files from both of the "results" folders to their respective folders in the main folder.

      1. Alternatively, if you ran nucleaizer on images from many main folders, run the postprocessing script to put the result images in their proper folders.

        >> cd /Volumes/LAUREN_TEMP/
        >> run_nucleaizer_batch_postprocess;
        

    Flood Algorithm

    1. Tell the code what tolerance you would like to use. This tolerance is the expected intensity difference between the background and the structure of interest.
      • For MDCK organoids, 3000 works well for lumens and 1000 works well for organoid.
    2. Tell the code what channel you would like to use to guess the segmentations.
      • For MDCK organoids, this is gfp for segmenting lumens and organoid.
      • For Intestine organoids, this is tmr for segmenting lumens and organoid.
    3. Sometimes, when looking at the results from this algorithm in the next step (reviewing the guessed segmentations), I do not like the results. You can re-run the code with different tolerances to try and correct this.

    GUI to Draw on Max Merge

    1. Simply using the GUI to draw the segmentations you need. Though you only see the max merge of the stack, the segmentations you draw will be applied to each slice.
  6. Once you have finished guessing the segmentations, prepare them for review.

    >> organoids2.prepare_2D_segmentations_for_review;

Review the 2D Segmentation

Here, we can review (and change, if necessary) the segmentations from the prior step.

  1. Make the segmentations folder MATLAB's working directory.

    >> cd segmentations_nuclei;
  2. Start the GUI to review the segmentations.

    >> organoids2.review_2D_segmentations;
  3. Edit the segmentations as needed.

    • The letters/symbols in parentheses are the keyboard shortcuts for that button.
    • You can change the slice/stack using the next/previous buttons or by entering the number of the slice/stack you want to go to.
    • You can zoom in or out of the image using the mouse scroll.
    • You can use the button for each channel to toggle its visibility. The dropdown menu will change the color that channel is displayed in.
    • The radiobuttons next to each channel indicate which channel is used for any intensity calculations done by the tools.
    • You can drag the contrast bar to make the images brighter. This will apply to all channels.
    • You can move a segmentation by clicking and dragging within it.
    • You can move an anchor point for a segmentation by clicking and dragging the anchor point.
    • You must select (or hit the keyboard shortcut of) the tool each time you want to use it.
      • Delete - Click on a segmentation to delete it.
      • Automatic - not yet working
      • Flood - Click on a region in the image. Will "flood" outward from that point until the intensity of the radiobutton channel is greater/less the intensity of the point +/- the value in the box.
      • Draw - Manually draw a segmentation.
      • Redraw - Manually re-draw part of a segmentation.
      • Split - Click a segmentation to split it into pieces. This works best with snowman-shaped segmentations that should be two ellipses.
      • Grow - Click a segmentation to make it larger (or smaller) by the number of pixels in the box (can be positive or negative).
  4. You do not need to complete all the edits you want to make in one session. If you re-run the code, your prior work will be loaded.

Guess the 3D Segmentation

Here, we will convert the 2D segmentations to 3D.

  1. Make the segmentations folder MATLAB's working directory.

    >> cd segmentations_nuclei;
  2. Run the code to guess the 3D segmentation and select which algorithm you want to use. Alternatively, you can give the number of the algorithm you want to use as an input.

    >> organoids2.guess_3D_segmentation;

    or

    >> organoids2.guess_3D_segmentation(1);
    • (1) using orthogonal segmentations - use if you used nucleaizer (for nuclei)
    • (2) using connected components - use for everything else
  3. Run the code to prepare the guessed segmentations for review.

    >> organoids2.prepare_3D_segmentations_for_review;

Review the 3D Segmentation

This currently does not work.