Wednesday, February 5, 2014

Making image processing on SageMathCloud. Extraction of chromosomes from multispectral images.

SageMathCloud is a plateform to do mathematics, it also offers the possibility to use a ipython notebook online. Several python libraries are available: numpy, scipy, scikit-image (0.7, 0.93), mahotas 1.1, pandas ...
Once an account is opened (it's free), a project created, some multispectral images were uploaded in a directory of the project. In that project, an ipython notebook was created, this notebook can be run in a project directly in SageMathCloud.

Removing spurious pixels brought by ndimage.find_objects():

The following model image  is a grey scaled image:


Grey image with a pseudo color map

find_objects() succeeds in extracting the letters:

Extracted particles are not convex, their bounding box can contains pixels from other particles in their neighborhood.
For example the letter A, contain pixels belonging to other segmented (labelled) letters. The image of A contains pixels belonging to the letter r and to the letter B. Those spurious pixels have to be eliminated. find_object() provides the bounding box of the segmented particles in a list. The bouding box index seems to correspond to the particle label, this could be used to erase the spurious pixels:

A python function can be written:

def extractParticles(greyIm, LabIm):
locations = nd.find_objects(LabIm)
i=1
extracted_images=[]
for loc in locations:
lab_image = np.copy(LabIm[loc])
grey_image = np.copy(greyIm[loc])
lab_image[lab_image<>i]=0
grey_image[lab_image <>i]=0
extracted_images.append(grey_image)
i=i+1
return extracted_images

It takes two images as arguments: a grey level  image (or stacked images) and a label image and returns a list of images containing the extracted particles from the grey level image. This implementation is much simpler and faster than a previous one .

 

Chromosomes extraction from a MFISH image:

After having uploaded images on cloud.sagemath, for example:

The five components (DAPI excepted) spectral images can be combined in one color image. There is an additional kar image resulting of the segmentation of the MFISH images :
5 spectral images combined into 3 to make a color (rgb) image (left). kar image (right)

Let's use the kar image to produce a label image so that each chromosome is defined by a single label (or grey level):
label image (right)

Now the extraction of individual chromosomes can be done, from the DAPI image:

or from stacked images:

The kar image identifies the chromosomes:

Let's make galleries of chromosomes

The extracted chromosome images have different size. Let's add borders to the images so that the images have all the same size :



def ResizeImages(ImList):
'''Find the largest width and height of images belonging to a list.
Return a list of images of same width/height
'''
maxwidth=0
maxheight=0
if len(np.shape(ImList[0]))==3:
components = np.shape(ImList[0])[2]
imtype = ImList[0].dtype
for i in range(len(ImList)):
width=np.shape(ImList[i])[1]#width=column
height=np.shape(ImList[i])[0]#height=line
#print "width:height",width,":",height
if width>maxwidth:maxwidth=width
if height>maxheight:maxheight=height
#print "maxwidth:maxheight",maxwidth,":",maxheight
NewList=[]
for i in range(0,len(ImList)):
width=np.shape(ImList[i])[1]
height=np.shape(ImList[i])[0]
diffw=maxwidth-width
startw=round(diffw/2)
diffh=maxheight-height
starth=int(round(diffh/2))
startw=int(round(diffw/2))
if len(np.shape(ImList[0]))==3:
newIm=np.zeros((maxheight,maxwidth,components), dtype=imtype)
newIm[starth:starth+height,startw:startw+width,:]=ImList[i][:,:,:]
NewList.append(newIm)
if len(np.shape(ImList[0]))==2:
newIm=np.zeros((maxheight,maxwidth), dtype=imtype)
newIm[starth:starth+height,startw:startw+width]=ImList[i][:,:]
NewList.append(newIm)
return NewList
view raw resizeImages.py hosted with ❤ by GitHub
Then let's build two galleries, one with MFISH images and one with inverse DAPI chromosomes, the code is visible in the notebook; it yields:
Left: MFISH chromosomes, Right:inverse DAPI

It remains to auto rotate the chromosome. A draft code, at the end of the ipython notebook  in the SageMathCloud project, tries to explore another method than the previously proposed. It remains also to arrange the chromosome according to their classification.

Download the ipython notebook

Go to the notebook on sagemathcloud