Friday, January 27, 2012

Mean Shift Clustering/ Segmentation in Matlab

The Mean shift implementation in here is based on the following article. 

Comaniciu, Dorin; Peter Meer (May 2002). "Mean Shift: A Robust Approach Toward Feature Space Analysis". IEEE Transactions on Pattern Analysis and Machine Intelligence (IEEE) 24 (5): 603–619


The original C++ implementation credit goes to here.
Matlab wrapper implementation credit goes there. Read this readme before using this wrapper :) Thanks also goes here.

I faced some troubles during the compilation of "compile_edison_wrapper" where i had used visual studio 2010 as a compiler. 

Here are two problems that I have faced !

Problem 1: end of file of the c++ code is in Mac format. 
solution: After opening *.h or *.cpp in visual studio save them with advanced options....choose windows style end of file.

Problem 2: pow function from math is ambigious. 
solution: convert the first parameter of pow into double. 

Here is my corrected version EDISON matlab wrapper (Click File-->Download) Try the do_mean_shift.m. 
Try 
MinimumRegionArea=40;
I=imread('yourimage.jpg');
[fimg labels modes regsize] = edison_wrapper(uint8(I),@RGB2Luv,'MinimumRegionArea',MinimumRegionArea);



A sample result should look like the following.





11 comments:

  1. Thanks Hasan for this version of the code. But how can I download the folder from google drive.

    ReplyDelete
  2. It is shared as public. Therefore, it should be downloadable!

    ReplyDelete
  3. hi!
    I wonder how to use mean shift clustering in this source code?

    ReplyDelete
  4. try something like the following please.

    MinimumRegionArea=40;
    I=imread('yourimage.jpg');

    [fimg labels modes regsize] = edison_wrapper(uint8(I),@RGB2Luv,'MinimumRegionArea',MinimumRegionArea);% default MinimumRegionArea=20;

    ReplyDelete
  5. hello Hasan!!

    I would like to know how to get the 'EDISON matlab wrapper'. It seems to be unavailable.

    Thanks in advance!

    ReplyDelete
  6. Hi Hassan,
    Thanks for the code. I have a question: I want to use this code for H&E image segmentation, basically I want to extract the cell nuclei after segmentation. when I look at the output, it does a great job with segmentation. but I do not know how it reconstruct the final output segmented image named fimg ? do you know what happens in their code when they reconstruct fimg ?

    Thanks,

    ReplyDelete
    Replies
    1. To know the underlying algorithm you may have a quick look in the original paper please.
      http://courses.csail.mit.edu/6.869/handouts/PAMIMeanshift.pdf

      When you have some idea from the paper, please debug the code step by step. Please note that its a wrapper over the original C++ code :P

      Delete
  7. Thanks for the link. I have already read the paper but it is not still clear for example how they got FIGURE 12(a)! they reconstruct the image after removing regions corresponding to sky and grass! I just want to know how they did that! I want to do the same with my H&E images and remove the regions corresponding to cell nuclei! I tried to debug the code in MATLAB but it uses the MEX file and I can not see what is happening in there! I even tried reading their C++ file but I could not see the image reconstruction part in it!
    I know this might be asking too much but have u used this code before? do you know how they got Figure 12(a) ?

    Thanks,

    Kia

    ReplyDelete
    Replies
    1. I think I understand what you would like to. Well, I would suggest you to create a mask to separate the cell nuclei. Let me give an example. Let's use a binary mask. The pixel corresponds to cell nuclei will be 1 and rest of place zero. Let call this mask M. and your segmented image called say, S. Now, if you M.*S (M and S has to be same size) will extract out the cell nuclei!

      Now, the real technical question would be how to create a mask automatically or without much interaction. Segmentation comes into play there. You could probably use cue like color information of cell nuclei if its completely distinct from the rest.

      Good luck!

      Delete
    2. well you are totally right! my next step is to use color information (transform to HSV space) and find a threshold to separate pink hue from blue hue! this is a great paper related to this:
      http://www.researchgate.net/publication/220931966_A_Classification_Scheme_for_Lymphocyte_Segmentation_in_HE_Stained_Histology_Images

      Thanks again for brilliant suggestions :)

      Delete
  8. Thanks for the prompt answer. my problem is FINDING THE PIXELS CORRESPONDING THE CELL NUCLEI! as you know this algorithm has 6 outputs: fimage, labels, modes, regsize, grad, and conf. so I know that I can use a threshold on REGSIZE to find the region that I want. but my problem is I can not find that threshold that separate cell nuclei from other parts of the image. for example I have tried 0<regsize<100 pixels, but it is not returning good results. I want to know how did they separate sky and grass from others in figure 12(a)? only by threshold on regsize? or they used other methods? what wonders me is that how can I have such a nice perfect segmented output, but I can not separate the regions easily?

    ReplyDelete

Please ask if anything is not clear enough..........