## Background

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.

## 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.

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).

## Results

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.

The original image (part of a standard pair of test images).

The segmented image (ready to be processed in step 2)

## Conclusion

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!

## 76 thoughts on “Mean Shift Segmentation in Matlab”

1. RONIT says:

Where can I get the code in matlab?

2. Anonymous says:

you are missing the assignment of L in your wrapper.

Thanx buddy,

This post was very helpful to me.

4. AA says:

thanks.
This is a good article.

5. Iveel says:

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

6. Anonymous says:

can i get a source code of it plz

7. maria says:

can i get a source code of it plz

8. Pau says:

9. aleena says:

can i get the code for mean shift method

10. Regan says:

can i get matlab code this? kindly send me…

11. Quang Vu says:

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

12. Tej says:

Can i get the code in matlab?

13. JQ says:

Can I get the code for mean shift segmentation?

14. ab singh says:

plz send code for mean shift . thank u for info

15. Park says:

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 says:

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

17. avi says:

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

18. Pulkesh says:

Hello Sir,
Can I get the code for mean shift segmentation.
Thanks

19. bengie says:

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 says:

@bengie
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 says:

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 says:

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

23. Math Yin says:

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

24. Harsh says:

getting error error “C4335: Mac file format detected: please convert the