How to set the label of a object manually
Show older comments
As the bwlabel command assigns the labels on a specific sequence and not according to the user .I want to use it to label a collection of arrows in a binary image according to the sequence I set . I also tried doing the same using for loop for a binary image (521*441),but here i had to enter the values many times (229761) please suggest an alternative I want the program to show me the object and ask the user to set the label for the object.
Answers (1)
L = bwlabel(BW, 4);
myLabels = [77, 55, 22, 7];
L = myLabels( L+1 );
will relabel 4 labelled objects with user-defined labels, where BW is your original binary image. In this case I hard-coded the number of labels to 4. If you have an unknown amount of labels then the number of such labels is just max( L ) + 1 so you need to have that many labels in your 'myLabels' array, otherwise any labels beyond that will just remain unchanged from their defaults.
How you choose to put that array of 'myLabels' together is something of a different question.
10 Comments
Soumya Behera
on 15 Dec 2015
Adam
on 15 Dec 2015
That is the line that actually does the relabelling via logical indexing. The values in L run from 0 to n-1 so I add 1 to be able to use them as indices into the array of defined labels, then it simply replaces all 1's with myLabels(1), all 2's with myLabels(2), etc, etc.
Note that it is actually not "logical indexing" but linear indexing that Adam is using:
Logical indexing is when the index is of class logical, which L+1 is not.
Soumya Behera
on 15 Dec 2015
@Soumya Behera: you are using code from my Answer, which I deleted. While it does work (I tested it) Adam's solution is a bit simpler and neater. You should use it.
The basic problem is that you are not using the output of my code: it creates the variable out, but you are viewing the variable L2 instead of out.
Adam
on 15 Dec 2015
You also don't want to hard-code 4 in here:
L2=bwlabel(bwF,4);
I just did that as an example. If you know exactly how many objects you have in advance then you can hardcode that number (seems like a lot more than 4 in your image). If not miss out the second argument and use either your 'count' variable as you are anyway or the method I mentioned to determine the number of labelled regions.
Soumya Behera
on 15 Dec 2015
Adam
on 15 Dec 2015
Once you have identified the objects you have to determine which is which if you want specific labels for specific objects, but how you choose to do that is more complicated than the actual relabelling once you have determined your mapping from the original labels to your new labels.
I don't know what order bwlabel labels objects in or whether you want an interactive way for the user to click an object and give it a label or some automated way with hard-coded labels like above.
Soumya Behera
on 15 Dec 2015
Edited: Soumya Behera
on 15 Dec 2015
Categories
Find more on Template Matching 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!