Tuesday, March 29, 2011

Trying to make an images mosaic with matplotlib

 In ImageJ, it is easy to make an images gallery with make montage.
Complete particles gallery made with make montage in imagej
The script to produce the particles from the metaphase is here. The end of the code tries to produce a particles (single/overlapping chromomes, nuclei) gallery using pylab.subplot, it is commented because it doesn't work.
I get the answer at pythonvision:


import readmagick
import pylab
import os

user=os.path.expanduser("~")
workdir=os.path.join(user,"Applications","ImagesTest","jp","Jpp48","13","DAPI","particules")
file="part1.png"
if __name__ == "__main__":
    file="part1.png"
    complete_path=os.path.join(workdir,file)
    i1=readmagick.readimg(complete_path)
    file="part2.png"
    complete_path=os.path.join(workdir,file)
    i2=readmagick.readimg(complete_path)
    file="part3.png"
    complete_path=os.path.join(workdir,file)
    i3=readmagick.readimg(complete_path)
    file="part4.png"
    complete_path=os.path.join(workdir,file)
    i4=readmagick.readimg(complete_path)
    pylab.subplot(2,2,1)
    pylab.imshow(i1) 
    pylab.subplot(2,2,2)
    pylab.imshow(i2)
    pylab.subplot(2,2,3)
    pylab.imshow(i3)
    pylab.subplot(2,2,4)
    pylab.imshow(i4)
    pylab.show() 
Removing the axis from the displayed images:
Axis can be removed  from the images as follow:

pylab.subplot(2,2,1,frameon=False, xticks=[], yticks=[])
 
click to enlarge

Friday, March 18, 2011

Fluorochrome signal calibration in QFISH analysis

It is desirable that independently from the imaging system or from the fluorochrome used (FITC, Cyanine, ...), the intensity of the fluorescent probe depends only on the number of targets in the biological sample.
Spectral calibration:
In order to compare the FISH signal obtained with different fluorochromes as FITC, Cy3, Cy5, the following assay can be performed with conventional material: 
  • Prepare a glass slide, and lay down a  2micro-L drop of 100 micro-M of fluorescent solution (stock solution of the molecular probe).
  • Heat the slide and mount with an anti-fading solution.
  • For each fluorochrome, take a serie of images of the microscopic field of varying time exposure and compute the mean gray level of each image.
 The following result was obtained with a leica DMR microscope equiped with a KAF1600 CCD camera:
      The data were linearly fitted and the sensitivity of a given fluorochrome was set as the slope of the linear response (gray level per time unit for a give probe concentration). Cy3 yields 713 gray level per 1/10 sec, Cy5:403 gl/time and FITC:297. Thus taking Cy3 as reference:
      Relative sensitivity of fluorescent probe.
      Color compensation
      In fluorescent microscopy, several fluorochromes are present on a biological sample. The light emitted by each fluorochrome is observed in a spectral window "opened" by a filter in the microscope.

      DAPI stains the DNA, with the DAPI block filters (the "blue window""or the blue channel), when you watch in the microscope oculars to see a DAPI stained metaphase, it looks blue. Thus,if you try to watch DAPI stained chromosomes in a green channel (with a FITC band pass filter), you should see nothing: this is true with naked eyes but wrong with a CCD camera with long exposure time. In the following example, images of a DAPI stained metaphase were taken in different spectral channels with different exposure time. Excepted for DAPI, the filters set were from omega filters(alpha vivid serie):
      DAPI:blue channel filter A
      FITC:green channel alpha vivid XF100-2
      Cy3:orange channel alpha vivid XF108-2
      Cy5: near infrared channel alpha vivid XF110-2
        DAPI spectral overlap displayed with false color.Top left:DAPI viewed through filter A (0.1s). Top right:DAPI viewed through green channel (8s). Bottom left: DAPI viewed through orange channel (8s).Bottom right:DAPI viewed through deep red channel for 30s.
        As the exposure time increases, DAPI is visible in the green channel, in the orange channel and even the near infra red channel after 30 sec of exposure. 
        The same is true for other fluorochromes, as FITC. The following example is the result of  a FITC labelled telomeric PNA probe hybridizing V79 cell line chromosomes.
        FITC spectral overlap in blue, orange and red channels
        To address this issue, Castleman (Digital Image processing, Prentice Hall, p556-561), define a color compensation technique relying on linear algebra:

        y=ECx+b

        where E a diagonal matrix specifying the relative exposure time of each channel and C a matrix accounting for the spectral spread through the channels with cij the proportion of signal  from fluorophore j visible in the channel i.
        x  is a 1 by m (m fluorochromes) vector representing an ideal image with no spectral spread.
        y is a 1 by m (m fluorochromes) vector representing the observed image with no spectral spread.
        b  is a 1 by m vector accounting for the background.
        To compute an ideal image with no spectral spread, the following equation has to be solved:
        x=C'E'(y-b)
        with C' and E' the inverse matrix of C and E.
        For example, with four fluorochromes (DAPI, FITC, Cy3, Cy5), the EC matrix was:

        graphical representation of the EC matrix


        Monday, March 14, 2011

        Extracting chromosomes from a complex metaphase

        Using the previous code on a real metaphase image, chromosomes can be isolated:
        Partial gallery of extracted particles (made with make montage in ImageJ)

         The gray level image:
        The labelled image:
        The code of the python script to extract particles from an image is modified quick and dirty such:
        
        path='/home/claire/Applications/ImagesTest/JPP60-3/DAPI/'
        lab=readmagick.readimg(path+'dapilabel.png')
        img=readmagick.readimg(path+'dapi.png')
        if __name__ == "__main__":
            Particles=extractParticles(img,lab)
            for i in range(0,len(Particles)):
                file='part'+str(i)+'.png'
                readmagick.writeimg(Particles[i].astype(np.uint16), path+file)    
         

        Extracting particles from an image: a toy model with numpy

        Image segmentation can be performed by an image labelling process. Once segmented, it could necessary to isolate in independant images the labelled regions. Think of chromosomes in a metaphase image, if resolving chromosomes overlapps or karyotyping must be performed.

        The following script takes "two images", (in fact two small numpy arrays : img is supposed to be a grey level image and lab, the label image which could be obtained after segmentation) and returns two particles, that is two images corresponding to the labelled regions (1 or 2. 0 corresponds to the background):


        The left image corresponds to a labelled image. The two upper images were obtained with extractParticles() and the two lower images were extracted with:
        scipy.ndimage.find_objects()

        find_objects() doesn't extract the particles properly.


        Update 2014-29-01
        The code is on gist 
        If ipython is not installed on your machine, play with it on cloudsagemath.


         
         #pseudo 16 bits gray level image
        img=np.array([\
        [75,167,181,105,33,5,2,2],\
        [72,163,193,136,54,10,1,2],\
        [54,123,154,118,51,11,2,2],\
        [23,54,69,58,33,18,11,4],\
        [5,15,27,43,62,69,53,23],\
        [5,24,61,104,147,162,124,54],\
        [10,54,128,186,220,222,168,72],\
        [14,72,169,230,247,238,177,76]],np.int16)
         
        
        #pseudo 16 bits label image
        lab=np.array([\
        [0,1,1,1,0,0,0,0],\
        [0,1,1,1,1,0,0,0],\
        [0,1,1,1,1,0,0,0],\
        [0,1,1,1,0,0,0,0],\
        [0,0,0,0,0,2,2,2],\
        [0,0,0,2,2,2,2,2],\
        [0,0,2,2,2,2,2,2],\
        [0,2,2,2,2,2,2,2]],np.int16)
         
        #
        #March14, 2011, A script to extract regions (particles) from an array (image)
        #Jean-Patrick Pommier
        #
        import numpy as np
        import os
        def extractParticles(grayIm,labIm):
            ''' give a grayscaled and a labelled image, extract the segmented particles
            ,returns a list of flattened particles'''
            #grayIm and labIm should have the same size
            def unflattenParticles(flatParticleList):
                '''take a list of flat particles and unflat them to yield an image'''
                unflatList=[]
                lenFlatList=len(flatParticleList)
                for i in range(0,lenFlatList):
                    #get the i particle:current Particle
                    curPart=flatParticleList[i]#current particle
                    #x values(col) are stored in the third col (3-1)
                    colmax=curPart[:,2].max()
                    colmin=curPart[:,2].min()
                    #y values(li) are stored in the fourth col (4-1)
                    limax=curPart[:,3].max()
                    limin=curPart[:,3].min()
                    unflatIm=np.zeros((limax-limin+1,colmax-colmin+1),np.int16)
                    #number of pixels in the particle
                    nbPixel=len(curPart[:,1])#count how many lines at col=1
                    for line in range(0,nbPixel):
                        col=curPart[line,2]
                        li=curPart[line,3]
                        pixVal=curPart[line,1]
                        unflatIm[li-limin,col-colmin]=pixVal
                    unflatList.append(unflatIm)
                return unflatList
                           
            sx=grayIm.shape[0]
            sy=grayIm.shape[1]
            #flatten grayIm
            fg=grayIm.flatten()
            fl=labIm.flatten()
            labmax=fl.max()
            #print fg
            #print fl
            #build two 2D array containing x and y
            #of each pixel of the grayIm
            ax=np.zeros((sx,sy),np.int16)
            ay=np.zeros((sx,sy),np.int16)
            #vectorization with numpy may be 
            #more efficient than two loops
            for j in range(0,sy):
                for i in range(0,sx):
                    ax[i,j]=j#filling ax with x=col
                    ay[i,j]=i#filling ay with y values y=li
            #flat arrays of coordinates
            fax=ax.flatten()
            fay=ay.flatten()
            #1D merge graylevel, label and coordinates 
            #in one 1D array of 4-uplet
            extract=np.vstack((fl,fg,fax,fay))
            #transpose to watch it easily
            eT=extract.T
            #create a list of flatten particles
            #labIndex takes the value from 1 (the first particle to labmax the\
            #label of the last particle
            flatParticleList=[]#from Matthieu Brucher
            for labIndex in range(1,labmax+1):
                flatParticleList.append(eT[eT[:,0]==labIndex])#from Matthieu Brucher
            return unflattenParticles(flatParticleList)
        
        if __name__ == "__main__":
            Particles=extractParticles(img,lab)
            for i in range(0,len(Particles)):
                print Particles[i]
                print    
         
        Since that script is supposed to work on true images with a least 1536x1024 pixels, some part of that python script may be optimized with numpy, as:
        
        #vectorization with numpy may be 
            #more efficient than two loops
            for j in range(0,sy):
                for i in range(0,sx):
                    ax[i,j]=j#filling ax with x=col
                    ay[i,j]=i#filling ay with y values y=li
         

        Thursday, February 17, 2011

        Segmentation of a complex metaphase with pymorph

        Part of the original 16bits TIF image

        Labelled image displayed with pylab
        The mean gray level value is also calculated for each labelled region with pymorph.grain:
        [ 1301.88575849    23.00804348   704.34775673   629.5625966   1410.2556391
           871.05686546   946.11368209   841.65226782   860.98429319  1526.48712121
           705.22321429   608.3359841    929.86325301   858.11372549  1000.02900378
           831.46130952   717.89433962   790.23236741   919.48754448   603.34122288
           830.35550936  1325.0239521    843.45842697   880.9575       864.25319693
           970.12876254   901.85921959   914.75644699   902.17860963   864.04659498
           960.92638037   892.95342466   946.98757358  1119.16554163   835.44263629
           872.87977369   838.04045734   734.5129683   1147.49576784   995.61397059
           839.4835443    823.52125984   795.88679245   898.91109422   748.07751938
           635.60344828]
        the python code used for that result:
        
        from scipy import ndimage as ND
        import scipy
        import numpy as np
        import readmagick
        import mahotas
        import pymorph
        import pylab
        import os
        def SegmentMetaphase(image):
            #print "def image:",image.mean()
            #print "def background:",back.mean()
            #print "def noback:",im.mean()
            noBack=RemoveModalBackground(image)
            print "from Segmeta noBack:",noBack.min(),noBack.mean()
            blurLowRes=ND.filters.gaussian_filter(noBack,13)
            blurHiRes=ND.filters.gaussian_filter(noBack,1)
            midPass=pymorph.subm(blurHiRes,0.70*blurLowRes)
            bin=(midPass>1.5*midPass.mean())
            binLowRes=pymorph.open(bin,pymorph.sedisk(4))
            binLR=pymorph.edgeoff(binLowRes)
            blur=ND.filters.gaussian_filter(noBack,9)
            hiPass=pymorph.subm(noBack,1.0*blur)
            gradLowRes=pymorph.gradm(noBack)
            #build seeds for watershed
            bSeeds1=(hiPass>hiPass.mean())
            bSeeds2=scipy.logical_and(bSeeds1,binLR)
            #bSeeds3=ND.binary_fill_holes(bSeeds2)
            bSeeds4=ND.binary_opening(bSeeds2,iterations=4)
            outline=mahotas.bwperim(bin, n=4)
            #display=pymorph.overlay(image,outline)#Hugly
            markers,nr_obj=ND.label(bSeeds4)
            labeled=mahotas.cwatershed(gradLowRes, markers, Bc=None, return_lines=False)
            return labeled
        def RemoveModalBackground(image):
            mode=ModalValue(image)
            back = np.zeros(image.shape, image.dtype)
            back.fill(mode)
            #print "def background:",back.mean()
            im=pymorph.subm(image,back)
            return im
        def ModalValue(image):
            '''look for the modal value of an image'''
            #print image.dtype
            if image.dtype=="uint8":
                depthmax=255
                print "8bits"
            if image.dtype=="uint16":
                depthmax=65535
                print "16bits"
            histo=mahotas.fullhistogram(image)
            countmax=histo.max()
            print "countmax:",countmax
            print "image max",image.max()
            mig=image.min()#image min graylevel
            mag=image.max()#image max gray level
            mode=0
            countmax=0#occurence of a given grayscale
            print "mig=",mig,"  mag=",mag
            for i in range(mig,mag-1,1):
                test=histo[i]>countmax
                #print "test:",test,"histo(",i,")=", histo[i],"max",countmax
                if  test:
                    countmax=histo[i]
                    mode=i
                    #print "mode",mode
            return mode
        def BorderKill(imlabel):
            '''remove labelled objects touching the image border'''
            #from pythonvision.org
            #pymorph.edgeoff should do the same
            whole = mahotas.segmentation.gvoronoi(imlabel)
            borders = np.zeros(imlabel.shape, np.bool)
            borders[ 0,:] = 1
            borders[-1,:] = 1
            borders[:, 0] = 1
            borders[:,-1] = 1
            at_border = np.unique(imlabel[borders])
            for obj in at_border:
                whole[whole == obj] = 0
            return whole
        user=os.path.expanduser("~")
        workdir=os.path.join(user,"Applications","ImagesTest","JPP60-3","DAPI")
        file="1.tif"
        complete_path=os.path.join(workdir,file)
        if __name__ == "__main__":
            #print complete_path
            dapi=readmagick.readimg(complete_path)
            readmagick.writeimg(dapi,os.path.join(workdir,"dapi.png"))
            NoBackIm=RemoveModalBackground(dapi)
            dapilab1=SegmentMetaphase(dapi)
            readmagick.writeimg(dapilab1,os.path.join(workdir,"dapilabel.png"))
            print pymorph.grain(NoBackIm,dapilab1, 'mean', option='data')
            pylab.imshow(dapilab1)
            pylab.show()
        

        Monday, February 7, 2011

        Metaphasic chromosomes segmentation under python

        The following python script implements three ways to segment metaphasic chromosomes through functions relying on scipy.ndimage and pymorph/mahotas. Starting from initial image:

         
        Human DAPI stained metaphasic chromosomes recorded with KAF1600 Peltier cooled CCD camera.
        • The first idea consists in performing a hipass filtering followed by a simple thresholding step and finally cleaning the binary image with a morphological opening. This is achieved with the function def HighPassBinary(image):
        Labelled image displayed with pylab. Hipass filtering results in fragmented nuclei
        •  The second idea, implemented in def metaphaseSeg(image):, tries to preserve both nuclei and chromosomes shape using a region growing algorithm (watershed + markers). The markers are obtained from a regional maxima on a blured DAPI image. However, according to the size of the gaussian kernel the segmented image can be over or under segmented with undetected chromosomes as shown in the following animation:
        gif animation of a region based segmentation with increasing gaussian kernel size (3, 5, 7, 9,11, 13, 19). A small kernel (3) yields an oversegmented image.

        • A third idea tries to combine both methods using the result of the first method as markers to initiate a region growing, implemented in def HighPassMetaphaseSeg(image):
        Artifacts close to the nuclei yield background artifacts near the nuclei.
        The result of this third method wouldn't be too bad if additionnal image cleaning was performed on the first image.
         The python script runs under ubuntu linux , I didn't take care of path to access to the image file to make it portable for windows or MacOS. To use one of the function comment/uncomment the code.

        from scipy import ndimage as ND
        import readmagick
        import mahotas
        import pymorph
        import pylab
        def HighPassBinary(image):
            '''depuis une image DAPI,  genere une image passe haut avec fond a zero'''
            '''necessite scipy.ndimage'''
            blur=ND.filters.gaussian_filter(image, 9)
            sharp=image-1.0*blur
            '''float 64bits'''
            print sharp.dtype, sharp.shape
            smax=sharp.max()
            '''threshold above negative values'''
            imBin=(sharp>0)
            imBin2=ND.binary_fill_holes(imBin)
            imBin3=ND.binary_opening(imBin2, iterations=4)
            labeled,nr_obj=ND.label(imBin3)
            print nr_obj
            return labeled
        
        def metaphaseSeg(image):
            blur=ND.filters.gaussian_filter(image, 15)
            remax=pymorph.regmax(blur)
            binRemax=(remax>0)
            seeds,nr_obj=ND.label(binRemax)
            print nr_obj
            grad=pymorph.gradm(image)
            label=mahotas.cwatershed(grad, seeds, Bc=None, return_lines=False)
            return label
        def HighPassMetaphaseSeg(image):
            '''depuis une image DAPI,  genere une image passe haut avec fond a zero'''
            '''necessite scipy.ndimage'''
            blur=ND.filters.gaussian_filter(image, 9)
            sharp=image-1.0*blur
            '''float 64bits'''
            print sharp.dtype, sharp.shape
            smax=sharp.max()
            '''threshold negative values'''
            imBin=(sharp>0)
            imBin2=ND.binary_fill_holes(imBin)
            imBin3=ND.binary_opening(imBin2, iterations=4)
            seeds,nr_obj=ND.label(imBin3)
            print nr_obj
            grad=pymorph.gradm(ND.filters.gaussian_filter(image, 1))
            labeled=mahotas.cwatershed(grad, seeds, Bc=None, return_lines=False)
            return labeled
           
        if __name__ == "__main__":
            dapi=readmagick.readimg("/home/jeanpat/Applications/Images_test/png/essai09/humain.png")
            '''dapiLabeled=metaphaseSeg(dapi)'''
            dapiLabeled1=HighPassBinary(dapi)
            '''dapiLabeled2=HighPassMetaphaseSeg(dapi)'''
            pylab.imshow(dapiLabeled1)
            pylab.show()
            '''pylab.imshow(dapiLabeled1)
            pylab.show()
            pylab.imshow(dapiLabeled2)
            pylab.show()'''
            '''readmagick.writeimg(dapiBin,"/home/jeanpat/Applications/Images_test/png/essai09/humainBin.png")'''


        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.