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??

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


buy discount viagra
buy price viagra
buy real viagra online
buy viagra in boston
buy discount soma
buy viagra with discount
buy viagra 100mg
buy now viagra
buy cheap viagra
buy viagra online
cheapest cialis
buy levitra now
buy viagra new york
buy discount levitra
online pharmacy
buy now cialis
buy levitra now
cheapest cialis
Pfizer viagra free samples
buy viagra san antonio
cheap viagra online
buy viagra new york
buy viagra internet
cheapest cialis