in the above code you are detecting centroids and clustering it...mean shift algorithm is implemented via the above code now segmentation part can be done by regionprops where you can give the BW image and the centroid which you have detected with the help of mean shift algorithm, I suggest you should see regionprops and watershed from matlab help you will understand my suggestion, atleast you can give it a try though
To understand mean shift segmentation, check following publication
Comaniciu, Dorin, and Peter Meer. "Mean shift: A robust approach toward feature space analysis." Pattern Analysis and Machine Intelligence, IEEE Transactions on 24, no. 5 (2002): 603-619.