MATLAB: Grouping the pixel having same values

image procesing

I have a RGB image in which i have taken some pixel values for example
83 81 81 84 85 84 82 82 84 81 83 83 81 81
83 81 81 83 83 82 80 81 83 80 82 83 81 79
81 79 83 84 84 84 81 81 82 80 81 81 79 79
79 80 82 86 85 84 83 83 85 85 81 81 79 79
79 81 84 85 85 83 83 83 84 83 81 83 81 81
84 83 84 85 85 83 84 80 81 81 81 82 82 83
83 84 86 85 85 83 84 80 79 80 82 82 81 83
for exmaple first value is 83,neighbour value is also 83 so it must be grouped(2 values will be grouped),next value 81(4 values of 81 it must be grouped )then next value is 85 there is no other neighbour value of 85 so it must be grouped as single vaue(1 value will be there),please help how to process
the pixel having same neighbour values must be grouped
please help

Best Answer

I =[83 81 81 84 85 84 82 82 84 81 83 83 81 81
83 81 81 83 83 82 80 81 83 80 82 83 81 79
81 79 83 84 84 84 81 81 82 80 81 81 79 79
79 80 82 86 85 84 83 83 85 85 81 81 79 79
79 81 84 85 85 83 83 83 84 83 81 83 81 81
84 83 84 85 85 83 84 80 81 81 81 82 82 83
83 84 86 85 85 83 84 80 79 80 82 82 81 83 ];
d = unique(I);
out = [];
for jj = 1:numel(d)
r = bwconncomp(I == d(jj),4);
out = [out;[repmat({d(jj)},r.NumObjects,1) r.PixelIdxList']];
end
or
d = unique(I);
out = [];
for jj = 1:numel(d)
s=regionprops(bwconncomp(I == d(jj),4),'PixelList');
out = [out;[repmat({d(jj)},numel(s),1) {s.PixelList}']];
end
[ii,ii] = sortrows(cell2mat(cellfun(@(x)x(1,:),out(:,2),'un',0)),[2 1]);
out = out(ii,:);