Tuesday, January 25, 2011

Exploring initial seeds conditions with ImageJ and mathematical morphology.

Up to now, image labelling was performed with seeds chosen manually and drawn with the gimp or ImageJ.
The seeds localization have to be found automatically and mathematical morphology can provide solutions. The chromosomal axis is close to the particule squeleton:
  
Binary chromosomes
 From the binary particules, imagej gives the following distance map:
Distance map of binary particules (false colors)
 with the following code recorded from the macro:
  1. run("Make Binary");
  2. run("Distance Map");
  3. run("3-3-2 RGB");
Binary particules squeleton
and then the squeleton. From the squeleton, peculiar points are searched: branching points and end-points using a binary hit-and-miss operator:

End points detection with imagej:
  1. run("Duplicate...", "title=squel.png");
  2. run("Make Binary");
  3. run("Skeletonize");
  4. run("BinaryHitOrMiss ", "kernel_a=[0 0 0 0 1 0 0 1 0 ] rotations=[rotate 45] white");
  5. run("Dilate");
  6. run("Select All");
  7. run("Copy");
  8. selectWindow("bin.png");
  9. run("Paste");
Branching points detection:
  1. run("Duplicate...", "title=EndPoints-Triple.png");
  2. run("Make Binary");
  3. run("Skeletonize");
  4. run("BinaryHitOrMiss ", "kernel_a=[0 1 0 1 1 1 2 2 2 ] rotations=[rotate 45] white");
  5. run("Dilate");
  6. selectWindow("bin.png");
  7. selectWindow("EndPoints-Triple.png");
  8. run("Select All");
  9. run("Copy");
  10. selectWindow("bin.png");
  11. run("Paste");
  12. saveAs("PNG", "/home/jeanpat/Applications/Images_test/png/essai07/ImageJ_HitnMiss_pointstriples_type2.png");
  13. open("/home/jeanpat/Applications/Images_test/png/essai07/ImageJ_HitnMiss_pointstriples.png");
  14. run("Select All");
  15. run("Copy");
  16. selectWindow("ImageJ_HitnMiss_pointstriples_type2.png");
  17. run("Paste");
Different hit-n-miss kernels suceed in detecting branching points. If end-points and branching points are overlaid on the binary particules, chromosomal areas close to those chosen by hand, are finally found:
  
end-points (black), branching-points (colored)
 Good candidates are selected.

Toward robust chromatids labelling :

Holed, bended, round shaped chromosomes may lead to poor chromatids labelling with one pair of gray seeds. Here with several pairs of seeds, a much better labelling is reached:
 
Chromatids labelling after watershed
 The result depends on how the seeds were chosen. Four independent factors must be taken into account:
  • The pairs of seeds number:
    • One pair yields poor results as observed previously.
  • The orientation of the pair of seeds  relative to the chromosome
    • The axis of the pair of seeds, must be locally orthogonal to the axis of the chromosome.
  • For a given pair of seeds, the distance between the two discs.
  • The relative orientation of the pair of seeds
    • The pair of seeds must have the same orientation
The seeds used to get the result were choosen by hand:
seeds and edge of binary chromosomes in false colors.



When the seeds don't have the same orientation, watershed yields an harlequin  pattern :
Harlequin pattern (black holes are due to the absence of seeds to label the background)

Thursday, January 20, 2011

Labelling chromosome-like shapes using watershed: effect of the chromosome shape

[edit feb 2013]
In previous post, the  chromatids of metaphasic chromosomes were labelled by hand with a painting tool in ImageJ following a watershed segmentation step:
Initial chromatids segmentation in ImageJ after high pass  filtering, thresholding, watershed and image labelling.
Final chromatids (blue or green) segmentation after painting by hand, centromeric region was added and paint in red.

 With a simplistic rigid model of chromosome and well chosen seeds, a watershed can label the chromatids:

Chromosomes present variable shapes, metacentric chromosomes are X shaped, acrocentric chromosomes are Y-shaped. The  shape of the same chromosome varies also from one metaphase spread to another, it can be randomly bended,condensed. Image processing introduces also variability according to the parameters used (threshold, filtering ...). Is it possible to segment chromatids such conditions ? Let submit a contour image of chromosome-like shapes to wateshed algorithm implemented in mahotas:

model shapes of chromosomes of variable size, bending. (Gimp)
seeds: the darkest seeds will label the back ground, they are outside the chromosomes. The pairs of lighter seeds are supposed to label the chromatids. (The Gimp)
In a python console:

    PyShell 0.9.8 - The Flakiest Python Shell
    Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    Startup script executed: /home/claire/.pyshell/startup
   >>> g=readmagick.readimg("/home/claire/Applications/ImagesTest/essai02/grad.tif")
   >>> s=readmagick.readimg("/home/claire/Applications/ImagesTest/essai02/seeds.tif")
   >>> label=mahotas.cwatershed(g, s, Bc=None, return_lines=False)
   >>> readmagick.writeimg(label,"/home/claire/Applications/ImagesTest/essai02/label.tif")

The background is labelled with one seeds outside the chromosomes and with seeds (same grey level) inside the chromosome holes. The chromatids are labelled with one pair of seeds inside the chromosomes:
Pair of seeds for watershed with two different grey levels


They are chosen grossly to fall apart the chromosomal axe:

Labelling with one pair of seeds per chromosome
If several pair of seeds are chosen by hand, a better chromatid segmentation can be found:
Pair of seeds overlaid to the particles contour
Chromatids like segmentation
The result depends on how the seeds were chosen. Four independent factors seems to be considered:

  • Seeds number:  one pair yields poor results as observed previously.
  • Seeds orientation : the seeds axis, must be locally orthogonal to the axis of the chromosome.
  • For a given pair of seeds, the distance between the two discs.
  • Relative orientation of the pair of seeds: The pair of seeds must have the same orientation.
When the seeds don't have the same relative orientation, watershed yields an harlequin  pattern :
opposite pair of seed yield harlequin pattern

Up to now, image labelling was performed with seeds chosen manually and drawn by hand with the gimp or ImageJ. The seeds localization have to be found automatically and mathematical morphology may provide solutions. The chromosomal axis is close to the particle skeleton. Can the particle skeleton be used to automatically find seeds useful for chromatid labelling with watershed? Using ImageJ interactively, chromosome-like particles skeleton was found:
chromosome-like particles
particles skeletons
From the skeleton, the branching points and end-points are searched using a binary hit-or-miss operator in ImageJ (see here for code). Different hit-or-miss structuring elements suceed in detecting branching points. If end-points and branching points are overlaid on the binary particules, chromosomal areas close to those chosen by hand, are finally found:
Skeleton branching points (color), skeleton end points (black)
At first sight end-points would provide better candidate seeds for watershed.



Monday, January 17, 2011

Test images for geodesic region growing (watersheed with gray seeds)

Two new 8bits images, made with the gimp, to test watershed with seeds:
cg.jpeg: a grayscale image of a circle
s.jpeg: a grayscale image of two seeds within the circle.








Now let the seeds  grow inside the circle. Using Mahotas from pycrust, here is a python session:

PyCrust 0.9.8 - The Flakiest Python Shell
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import readmagick
import mahotas
c=readmagick.readimg("/home/claire/Applications/ImagesTest/essai01/circle.tif")
print c.shape
(100, 100, 3)
 
s=readmagick.readimg("/home/claire/Applications/ImagesTest/essai01/seeds.tif")
print s.shape
(100, 100)
cg=c[:,:,0]#converts the 3 channels image into a 1 channel grayscale image.
print c.shape
(100, 100, 3)
import pymorph

#converts the 3 channels image into a 1 channel grayscale image.
cg=pymorph.to_gray(c)

print cg.shape
(100, 100)
print s.shape
(100, 100)
lab=mahotas.cwatershed(cg, s)
readmagick.writeimg(lab,"/home/claire/Applications/ImagesTest/essai01/label.tif")
readmagick.writeimg(lab,"/home/claire/Applications/ImagesTest/essai01/label.jpeg")
readmagick.writeimg(cg,"/home/claire/Applications/ImagesTest/essai01/cg.jpeg")
readmagick.writeimg(s,"/home/claire/Applications/ImagesTest/essai01/s.jpeg")
#save the image in jpeg, blogspot doesn't like tif
After watershed the labelled region:


The inner circle region is ok, but outside ...







Thursday, January 6, 2011

Good seeds, bad seeds :using watershed to label chromatids of a metacentric model chromosome

A model image of a metacentric chromosome was generated with ImageJ. The model is simplistic, it consists in X shaped binary image:
16 bits binary  image
The seeds consists in small spots in a image. To detect the chromatids, the seeds are designed as two discs labelled by two different gray level (one for each chromatid).

The initial location of the seeds, was in that first trial choosen by hand.

Initial seeds location inside the chromosome 
Gray seeds (that is a 16 bits grayscale image) are then allowed to grow using the watershed algorithm inside the chromosomes, yielding(the result is displayed with pylab):
seeds after watershed yield labelled chromatids (light blue, dark blue)

The result is nice because the initial location was purposefully chosen. What would happen with varying seeds location or orientation ?
Seeds location shifted in the lower left chromatid


After rotation, the seeds can label  p and q arms:
Upper left: the seeds are 45° rotated, the p arm (light blue) and the q arm (dark blue) are detected.
Upper, lower right: shifting vertically the seeds 
Lower left:use of three seeds p  arm (blue), centromeric region (green), q arm (yellow)