Clear Filters
Clear Filters

Wrong results while accessing matrix

1 view (last 30 days)
Batuhan Hangun
Batuhan Hangun on 15 Jul 2017
Commented: Walter Roberson on 16 Jul 2017
Hello there. I try to implement my own version of SLIC method to work on both an RGB or a Hyperspectral image. After clustering process I need to apply a second step to correct faulty labeling and remove redundant labels. I find the connected components using MATLAB's "bwconncomp" function. Checking connected components sizes and neighbour labels. I want to modify label of some element's label to their nearest neighbour cluster center's label. To achieve this I need to find an average vector [L a b x y] of given connected component. Labxy is a vector that I created for this purpose. But the values those are stored in Labxy vector are not correct. What am I doing wrong?
clc,clear, close all;
Image = imread('anyrgbimage.jpg');
Superpixels = 512;
tic
[LabelMatrix,ClusterCenters] = slic(Image, Superpixels,10);
toc
ImageLab = rgb2lab(Image);
[Height, Width] = size(LabelMatrix);
SuperPixelSize = (Height * Width)/ Superpixels;
MinimumSegmentSize = round(SuperPixelSize/4);
LabelMatrix2 = LabelMatrix;
for label = 1:size(ClusterCenters,1)
% fprintf('Calculations for Label %d\n\n', label');
ConnectedComponentsL = bwconncomp(LabelMatrix == label);
numPixels = cellfun(@numel,ConnectedComponentsL.PixelIdxList);
[BiggestSegment,idx] = max(numPixels);
for i = 1:length(numPixels)
if(BiggestSegment > MinimumSegmentSize)
if(i ~= idx )
% fprintf( 'numPixels(%d) = %d\n\n', i,numPixels(i));
Segment = ConnectedComponentsL.PixelIdxList{i};
S = [Height , Width];
AdjacentLabelCount = zeros(1,size(ClusterCenters,1));
Labxy = uint8(zeros(length(Segment),size(ClusterCenters,2)));
for j = 1:length(Segment)
[X, Y] = ind2sub(S, Segment(j));
Labxy(j,:) = [(squeeze(ImageLab(X,Y,:)))' X Y];
fprintf('[%d %d %d %d %d]\n\n', Labxy(1),Labxy(2),Labxy(3), X, Y);
fprintf('%d) Segment Index = %d, Subscript = [%d %d], Size of Segment = %d elements \n\n', j, Segment(j), X, Y, numPixels(i));
NLoc = [X, Y] + [-1 , 0];
% fprintf('NLoc = [%d %d] ', NLoc(1), NLoc(2));
NELoc = [X, Y] + [-1 , 1];
% fprintf('NELoc = [%d %d] ', NELoc(1), NELoc(2));
NWLoc = [X, Y] + [-1 , -1];
% fprintf('NWLoc = [%d %d] ', NWLoc(1), NWLoc(2));
SLoc = [X, Y] + [ 1 , 0];
% fprintf('SLoc = [%d %d] ', SLoc(1), SLoc(2));
SELoc = [X, Y] + [1 , 1];
% fprintf('SELoc = [%d %d] ', SELoc(1), SELoc(2));
SWLoc = [X, Y] + [1 , -1];
% fprintf('SWLoc = [%d %d] ', SWLoc(1), SWLoc(2));
WLoc = [X, Y] + [0 ,-1];
% fprintf('WLoc = [%d %d] ', WLoc(1), WLoc(2));
ELoc = [X, Y] + [0 , 1];
% fprintf('ELoc = [%d %d] \n\n', ELoc(1), ELoc(2));
[AdjacentLabelCount, NorthLabel] = checknorth(NLoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, NorthWestLabel] = checknorthwest(NWLoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, NorthEastLabel] = checknortheast(NELoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, SouthLabel] = checksouth(SLoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, SouthWestLabel] = checksouthwest(SWLoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, SouthEastLabel] = checksoutheast(SELoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, EastLabel] = checkeast(ELoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
[AdjacentLabelCount, WestLabel] = checkwest(WLoc, Height, Width, label, LabelMatrix, AdjacentLabelCount);
%MaxMat = find(AdjacentLabelCount == max(AdjacentLabelCount));
end
MaxMat = find(AdjacentLabelCount == max(AdjacentLabelCount));
if(length(MaxMat) == 1)
% fprintf('Biggest adjacent of Label %d is Label %d\n\n', label, MaxMat);
LabelMatrix2(Segment) = MaxMat;
else
% fprintf('There are more than one adjacent labels at same quantity\n\n');
for t = 1 : length(MaxMat)
% fprintf('%d. Biggest adjacent of Label %d is Label %d\n\n', t, label, MaxMat(t))
% LabelMatrix2(Segment) = MaxMat;
end
end
end
else
fprintf('Segment size of Label %d is equal to %d and it is smaller than Minimum Segment Size which is %d\n\n', ...
label, numPixels(i) , MinimumSegmentSize);
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 15 Jul 2017
You have
[X, Y] = ind2sub(S, Segment(j));
Labxy(j,:) = [(squeeze(ImageLab(X,Y,:)))' X Y];
You have made the fairly common mistake of thinking that rows correspond to X and that columns correspond to Y. Instead, rows correspond to Y and columns correspond to X. So,
[Y, X] = ind2sub(S, Segment(j));
Labxy(j,:) = [(squeeze(ImageLab(Y,X,:)))' X Y]; %you need to rethink here whether you want X Y or Y X at the end
  2 Comments
Batuhan Hangun
Batuhan Hangun on 16 Jul 2017
Actually my indexing was right I guess because I did not get any error about matrix dimensions. I think source of wrong results was uint8( ) at
Labxy=uint8(zeros(length(Segment),size(ClusterCenters,2)));
I removed that conversion and everything seems fine. Now I try to access ImageLab(X,Y,:) with [X Y] given by ind2sub(S, Segment(j)); and it looks like that it does fine. I think while displaying it(imagesc, image, imshow), MATLAB converts ImageLab(X,Y,:) to ImageLab(Y,X,:) right? Since I do not display anything
[X, Y] = ind2sub(S, Segment(j));
Labxy(j,:) = [(squeeze(ImageLab(X,Y,:)))' X Y];
this usage is okay. Am I right?
Walter Roberson
Walter Roberson on 16 Jul 2017
MATLAB always uses rows for Y and columns for X for image display. The only question for MATLAB is whether location (1,1) should represent the top left of the image or the bottom left of the image.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!