Wrong results while accessing matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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.
Siehe auch
Kategorien
Mehr zu Linear Algebra finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!