Problem when changing labels in image

2 views (last 30 days)
Hello
I have a strange problem with labeling an image in MATLAB. I do not know if this is a problem occurring by my mistake or is caused by MATLAB.
I have a cell matrix with string names containing the classes (classification) of image objects called MK
The number of labels in the image is equal to Num
My label image is called LL
The problem occurring is that when I loop through the image and try to change the labels some of the objects within are lost. I do not know why this is happening. The labels within the image have a range from 1 to 8 (in this case) and the MK vector contains 8 class names.
My code look like this
for i=1:Num
ccc=MK(i);
% Convert to character
cc=char(ccc);
if strcmp(cc,'Water')
LL(LL==i)=100;
end
if strcmp(cc,'Soil')
LL(LL==i)=200;
end
if strcmp(cc,'Trees')
LL(LL==i)=300;
end
if strcmp(cc,'Vegetation')
LL(LL==i)=400;
end
if strcmp(cc,'Building')
LL(LL==i)=500;
end
if strcmp(cc,'Shadow')
LL(LL==i)=600;
end
end
I would really appreciate some help with this problem as I am stuck and I can not find a solution !

Accepted Answer

Image Analyst
Image Analyst on 13 Jul 2012
Edited: Image Analyst on 13 Jul 2012
Tell me what this says
max(LL(:))
You say it's supposed to work with any number of objects but you're going to have a problem with your code if you have more than 100 objects because you will be replacing their labels with your new ones. And tell me why you think some of the labels are lost. What does
numberOfObjects = hist(LL(:))
return?
And why do you want the labels to be a multiple of 100 in the first place? That is not necessary for anything as far as I can tell.
By the way, you could get rid of the loop entirely with a single call to intlut().
  2 Comments
Dimitris M
Dimitris M on 13 Jul 2012
Hello
max(LL(:)) retuns 6 which is normal as after the replacement of the values with their new classes. Though if I run the command before replacing the values gives me an 8 value.
The number of objects will never overcome a number such of 100 as there is a condition before that merges small object so this is case is prevented. By introducing big numbers like 100 I am just avoiding the new labels to be replaced in the next loop.
The numberOfObjects = hist(LL(:)) returns a histogram of values 0,3,5,6
But this is wrong as it should also have the value 4 (given in the classification result) but this value is lost. Also in the export image I am creating I can see that many regions are lost and merged with others !
I think the whole problem occurs with the command
LL(LL==i)=whatever
I do not know why but it replaces some of the values that shouldn't when it runs in a loop.I am sure that somehow I can avoid loops but I am not so good in matlab yet !
Thank you for your precious help in advance !
Image Analyst
Image Analyst on 14 Jul 2012
Several confusing/contradictory things in your comment. If the histogram is 0,3,5,6 that means you have 0+3+5+6 = 14 objects, not 6 objects. But hist uses 10 bins so you should have 10 numbers, not 4 - what happened to the other numbers? But with 10 bins there might be groupings/binnings we don't want. Therefore, try this:
[numberOfObject binValue] = hist(LL(:), max(LL(:)))
so that we have one bin for exactly one label number. If your labels go as high as 8, you should have 8 bins and 8 values for the bins (one value per bin). Of course you need to do this on LL before you change its values. The numbers will still add up to be 14 though and that is confusing because you say there are 8 objects not 14.
Finally, you didn't answer why you want the labels renumbered. Like I said, it's not necessary. And you didn't say why (if you still insist on renumbering) you didn't use intlut() instead of that for loop.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 9 Jul 2012
[tf, idx] = ismember( Mk, {'Water', 'Soil', 'Trees', 'Vegetation', 'Building', 'Shadow'} ); %and add the other two cases
LLsubset = (LL >= 1 & LL <= Num);
LL( LLsubset ) = idx( LL( LLsubset ) ) * 100;
  3 Comments
Walter Roberson
Walter Roberson on 9 Jul 2012
You have 8 classes in Mk but only test 6 of them ?
What do you observe when a label is "lost" ?
Dimitris M
Dimitris M on 13 Jul 2012
Hello Again and I am sorry for my late reply.
In the example mentioned above I have an image with 8 regions (so 8 labels strings in Mk), but the algorithm works on sequences of images which can have arbitrary number of regions (or labels) . The classification though has only 6 classes so some string values in Mk may be the same (ex. 2 times class Trees, 3 times Building etc). So the algorithm just tests the string value is assigned to each different label.
Is it more clear to you know ?
Thank you

Sign in to comment.


Dimitris M
Dimitris M on 13 Jul 2012
So I worked a little bit on the code so know I am closer to a better approach but still can not overcome the previous mentioned problem.
My code looks like this
% Assign correct values to each label
[~,ClassLabel] = ismember(Mk,{'Water', 'Soil', 'Trees', 'Vegetation', 'Building', 'Shadow'});
for i=1:length(ClassLabel)
LL(LL==i)=ClassLabel(i);
end
The problem is that still the LL(LL=i) doesn't give me the write result in the for loop ! Do you know why this happens ?
I just need to replace the labels of the LL with the ClassLabel vales but this command is giving me really hard times !
  2 Comments
Image Analyst
Image Analyst on 14 Jul 2012
At this point I think the only way we can make progress in helping you is for you to upload you LL file. Pick your favorite file hosting web site and let us know what the URL is. And tell us exactly how you create Mk.
Dimitris M
Dimitris M on 16 Jul 2012
Thank you very much for your support but I solved the problem. I have no idea though why the command
LL(LL==SOMETHING)=SOMETHING ELSE
doesn't works properly in a loop !
Thank you anyway

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!