Home > Matlab, Vision > Mean Shift Segmentation in Matlab

Mean Shift Segmentation in Matlab

November 16th, 2007 Leave a comment Go to comments


Recently I have decided to explore tracking from 3D point clouds extracted from stereo vision cameras. Step 1: Extract 3D point cloud from stereo vision cameras. So right now I’m implementing Segment-Based Stereo Matching Using Belief Propogation and Self-Adapting Dissimilarity Measure” by Klaus, Sormann, and Karner. This paper is defined by the source on stereo vision to be the best one around. This paper has two parts. Part 1: Segment the image. Part 2: Compute disparity (and depth) from the segments. Well, today I finished Part 1.

Stereo Cameras

First Try

The authors refer to a mean-shift segmentation algorithm presented in Mean Shift: A Robust Approach Toward Feature Space Analysis” [pdf] by Comaniciu and Meer to do the image segmentation. This paper (unlike some of my own previous work) leans towards oversegmentation of an image. Meaning that you prefer to get lots of little bits rather than the “right object” after the algorithm has run.

Well, after looking over the paper and getting a grasp for the mathematics, I took a crack at implementing it. Easily done… HOWEVER, my first attempt, written in Matlab, was painfully slow. (For a simple image it took 6 hours to run!) So, I got on the internet and came up with a better solution!

The Solution

Some great guys at Rutgers University implemented this paper in C++ and made the code available to the public under the name EDISON. (there’s also a nice GUI that goes along with this if you want to just play to see if these codes will work for you). Okay, so I had C++ codes that worked well (only 2 sec to do an image rather than 6 hours). The next step was to bring the code into Matlab.

Mean Shift Segmentation Results
These were the type of results I was trying for

I cracked my knuckles and got ready to write a MEX wrapper for this EDISON code. Then I said to myself, “Self, maybe you should check the ‘net first.” Turns out I had a good point. I found the website of Shai Bagon. Mr. Bagon had already made the MEX wrapper! Awesome.

I downloaded the codes and put them together. Mr. Bagon’s stuff worked right out of the box, although it would have saved me about an hour if I would have had this information (alternative readme.txt for Matlab Interface for EDISON). I also wrote my own wrapper-wrapper so that I could process grayscale images, and do simpler calls to accomplish what I wanted. If you’d like the code, download my wrapper-wrapper here (msseg.m).


Here is a sample of the output of this algorithm. The first image is a regular photo of some posed objects. The second image is the segmented version. Notice how the regions of the image are much, much more constant. This image has been broken into “tiles” of constant color.

Left Image
The original image (part of a standard pair of test images).
segmented image
The segmented image (ready to be processed in step 2)


Don’t re-invent the wheel. Taking a first crack at the implementation was good, and it helped me understand the algorithm. However, there was no need for me to spend a week tweaking it to be super-fast or two days getting the Matlab interface working. These things had already been done! It feels nice to knock out a task that you thought was going to take a week in a few hours : ) Stay tuned for the stereo part of this paper coming soon. Then maybe people will be writing about my page!

  1. RONIT
    April 3rd, 2012 at 04:41 | #1

    Where can I get the code in matlab?

  2. Anonymous
    May 10th, 2012 at 20:21 | #2

    you are missing the assignment of L in your wrapper.

  3. Mohammad
    June 8th, 2012 at 01:19 | #3

    Thanx buddy,

    This post was very helpful to me.

  4. AA
    March 1st, 2013 at 04:33 | #4

    This is a good article.

  5. Iveel
    March 4th, 2013 at 07:29 | #5

    Thanks ..The article that i was looking for :)
    Keep on posting, Man !

  6. Anonymous
    April 27th, 2013 at 07:40 | #6

    can i get a source code of it plz

  7. maria
    April 27th, 2013 at 07:40 | #7

    can i get a source code of it plz

  8. Pau
    May 14th, 2013 at 10:56 | #8

    Thanks. It’s been very helpful!

  9. aleena
    June 28th, 2013 at 04:33 | #9

    can i get the code for mean shift method

  10. Regan
    August 17th, 2013 at 01:24 | #10

    can i get matlab code this? kindly send me…

  11. Quang Vu
    November 5th, 2013 at 04:02 | #11

    Can you helm me get the code in c++?
    I can not get anywhere.
    best regards,

  12. Tej
    December 13th, 2013 at 17:19 | #12

    Can i get the code in matlab?

  13. JQ
    January 21st, 2014 at 10:48 | #13

    Can I get the code for mean shift segmentation?

  14. ab singh
    June 22nd, 2014 at 23:40 | #14

    plz send code for mean shift . thank u for info

  15. Park
    July 1st, 2014 at 05:45 | #15

    Hi nowaday, i study segmentation using matlab
    but it is very hard to me
    so if you can, Can i get the code in matlab?

  16. rasid
    October 30th, 2014 at 10:58 | #16

    hello, hai, please send code for mean shift segmentation. tqvm

  17. avi
    November 20th, 2014 at 06:06 | #17

    hello could I get the code for mean shift segmentation please??

  18. Pulkesh
    May 2nd, 2015 at 05:41 | #18

    Hello Sir,
    Can I get the code for mean shift segmentation.
    Please reply me.

  19. bengie
    June 24th, 2015 at 13:35 | #19

    Great post! I got the mean shift Matlab interface working and it’s really great. I used Matlab 2015a with the windows SDK 7.1 compiler. I did get some compiler errors initially due to some of the .cpp and .h files having the Mac end-of-line format and also had to typecast an input to the c++ pow() function to a double. After these fixes, it compiled without any problems.

  20. RHJ
    November 15th, 2015 at 01:40 | #20

    Hey bengie! I need help. How should i start? Do i need to C++ platform as well for this code or just matlab?

  21. Yijun Yan
    November 27th, 2015 at 08:15 | #21

    Hi, I’m very interesting on this code, however, my computer is window 64bit, and when I use MATLAB to compile this edison wrapper, it shows ‘Mac file format detected: please convert the source file to either DOS or UNIX format’
    So, I wonder how can I fix that problem?

  22. jithin
    January 18th, 2016 at 03:42 | #22

    sir can i get the code please…kindly send me the matlab code to my email id

  23. Math Yin
    February 24th, 2016 at 15:21 | #23

    Hi !
    Great article, can you send me the matlab code for this ?
    Thanks !

  24. Harsh
    May 9th, 2016 at 07:05 | #24

    getting error error “C4335: Mac file format detected: please convert the
    source file to either DOS or UNIX format” I don’t how to remove it. Please help

  25. yifei
    May 21st, 2016 at 06:28 | #25

    can I get the sorce code in matlab, please?

Comment pages
1 2 257
  1. June 13th, 2012 at 02:39 | #1