File Exchange

image thumbnail


version by Nikolay S.
A Matlab Image segmentation via several feature spaces DEMO


Updated 30 Aug 2015

GitHub view license on GitHub

Image segmentation via several feature spaces DEMO.
This demo was designed to demonstrate several commonly used feature spaces, in a segmentation task. It was inspired by multiple questions of Matlab File Exchange users addressed via Matlab Answers, and to author’s personal page and email.
The goal of a segmentation process in image processing is to divide image to elements (segments). When defined and implemented properly, some of the elements may include meaningful elements of the scene- humans, animals, building, vehicles etc. This allows to continue to analyze the image or video stream to get meaningful insight about the filmed scene.
There are multiple segmentation schemes. In short, a classical segmentation scheme is composed of the following stages:
1. Feature space selection, either by hand, using experience and knowledge about problem in hand, or automatic set of features: Harr like Viola Jones windows, LBP, HOG, Color histograms etc…
2. Image transformation to feature space. In many cases a combination of different feature is used.
3. Feature space reduction- to improve both run-time (sometimes dramatically) and segmentation accuracy. PCA is a good example of efficient feature reduction method.
4. Image pixels classification.
5. Spatial/temporal segmentation smoothing- if spatially/temporally continuous segments are assumed.
We have implemented a simplified scheme, omitting some of the above stages.
1. Feature space selection Used feature spaces are: LBP, GLCM, Statistical image moments.
2. Image transformation to feature space. Each feature space is composed of a single feature specified above. In case of color image each color is treated separately, resulting in a larger feature vector. A feature vector if generated of a predefined neighborhood of each chosen pixel. Matab "blockproc" function is used to acquire a feature vector
3. Feature space reduction- to improve both run-time (sometimes dramatically) and segmentation accuracy. PCA is a good example of efficient feature reduction method.
4. Image pixels classification. K-means clustering is chosen du it’s relative simplicity and decent run-time.
5. Not implemented.
By running the demo the user can see various images segmentations achieved by each scheme (differing only in used feature space). Both segmentation quality and run-time are presented.
Note: the scheme is far from perfection, although it achieves decent segmentation. It can be improved in multiple ways- via parameters fitting, utilization of additional feature spaces, better classification schemes, post filtering, usage of more representative color channels etc. Moreover, due to random nature of K-means clustering, different results can be achieved for the same image and parameters. This is not a bug, but an existing scheme drawback.

Cite As

Nikolay S. (2020). kolian1/texture-segmentation-LBP-vs-GLCM (, GitHub. Retrieved .

Comments and Ratings (11)

Please any one say me that where PCA is used in this code for dimensional reduction.

Thanks, Nikolay. You might perhaps update the submission after correcting the mentioned files: it seems that the two functions are still the original ones (also on the github site).
Best regards

Nikolay S.

Hi Giorgio De Nunzio.
Thanks for finding this one. It seems you are correct, to have the processed block dimensions to be of "winDims" the line
should be repalced with
winNeigh=floor( (winDims - winStep)/2 );

This holds for both files "segmentationGLCM.m" and "segmentationLBP.m"
Hope I got this right this time.
Best regards,

Hi, thanks for your work!
I have a doubt. I am running segmentationDemo with:
winDims = [63, 63];
winStep = [5, 5];
which should give me a sliding window 63x63 in which to calculate e.g. the cooccurrence matrices and then the Haralick features, stepping 5 pixel at each iteration (to the right, at first, then down after completing a row, etc).
Now, to verify and to better understand, I've put a breakpoint at the line
imgFeatSpace=blockproc(inImg, winStep, @raw2FeatSpace, 'BorderSize', winNeigh,.......
contained in segmentationGLCM. By investigating the variable values before blockproc call, I got winStep = [5 5] as the "block size" parameter, and winNeigh = [31 31] as the "BorderSize" parameter.
This is strange, because according to
we have that (being [m n] the block size, and [v h] the border):
"The size of each resulting block is: [m+2*v, n+2*h]"
i.e. in this case [5+2*31, 5+2*31] = [67,67]
which is not the desired [63 63].
In order to have [63 63] as the final sliding window size, imho you should give [v h] = [29 29], i.e.
winNeigh = (winDims-winStep)/2 instead of winNeigh=floor(winDims/2)
(where I'm supposing all the values are odd).
Am I wrong? Perhaps I am not understanding some other blockproc parameter that modifies function application?

Nikolay S.

Good afternoon Maria.
In the above demo the used classifier is K-means clustering, who's only parameter is the number of clusters. Actually, as mentioned above, it was chosen due to its ease of use.
The LBP features are also parameter dependent- the parameters are the filter setting (number of angles, angle resolution, scales ets), and the usage or not of regularization (direction normalization). The LBP parameters can be defined based on your problem "geometry"- that is, the way you expect to differentiate features. Note higher resolution will results in better differentiation, but this will come with an increase of computational effort. Also be ware of over-fitting- using too high resolution/obsolete features that will noise your classifier.
Hope this helps,

Hello Sir, what is the best parameter to classified images using lbp features from your code ? i want to use it as input for classification for 40 images(binary) . i thought the parameter is effLBP, is this right ? actually i have read some material about lbp features, but still not enough understand, then i try your codes both efficienLBP and koilan lbp, they are great. can you give me advise because i dont know what is the parameter that i can use for classification. hope my questions are clear :)

Nikolay S.

Thank you for your kind words Nicolas Yu. You're welcome to email me with questions.

Nicolas Yu

The work seems very interesting. I am reading the codes. Thanks.

Nikolay S.

You're welcome my friend.

thanks sir

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux