I got a color image and store into a variable A.
A =
(22,10,10) (22,10,10) (39,40,50)
(89,11,14) (23,11,11) (99,10,10)
(89,11,14) (69,10,10) (99,10,10)
x=1 when loop horizontally I wish to register the first color I seem and label it into x.
when loop same color then label it into same label.
when new color is seem then register the color and label into x+1.
I wish to get a table like
color label
(22,10,10) 1
(39,40,50) 2
(89,11,14) 3
(23,11,11) 4
(99,10,10) 5
(69,10,10) 6
so result finally get is
B=
1 1 2
3 4 5
3 6 5
How to get the label table and how to get the result?

2 commentaires

Walter Roberson
Walter Roberson le 3 Déc 2015
Modifié(e) : Walter Roberson le 3 Déc 2015
What data type is A? The notation you use is not MATLAB syntax, and
A = {
[22,10,10], [22,10,10], [39,40,50];
[89,11,14], [23,11,11], [99,10,10];
[89,11,14], [69,10,10], [99,10,10] }
would not be a 3D matrix.
Tan Wen Kun
Tan Wen Kun le 3 Déc 2015
Modifié(e) : Tan Wen Kun le 3 Déc 2015
I should say I got a 3D image.
I got a color image like this so what I want to do is loop horizontal and label the color seem and register it.
I just show my problem in a simple form to see.
so I did not use
A(:,:,1) =
22 22 39
89 23 99
89 69 99
A(:,:,2)=
10 10 40
11 11 10
11 10 10
A(:,:,3)=
10 10 50
14 11 10
14 10 10

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 3 Déc 2015

1 vote

The way to do this is to reshape your image into an nx3 matrix where each row correspond to the three colours of a pixel. You can then apply unique with the 'rows' option on this to get your labels. It's then just a matter of reshaping the output.
Note that doing the scanning by rows rather than by columns complicate the code somewhat.
Also, please, post valid matlab code rather than leaving it to us to generate valid matrix
A = cat(3, [22 22 39;89 23 99; 89 69 99], ... it would have been great
[10 10 40; 11 11 10; 11 10 10], ... if I could just have
[10 10 50; 14 11 10; 14 10 10]) %copy/pasted that from your question
[~, ~, labels] = unique(reshape(permute(A, [2 1 3]), [], 3), 'rows', 'stable');
B = reshape(labels, size(A, 2), size(A, 1))'
The same code if you do the labeling by column:
[~, ~, labels] = unique(reshape(A, [], 3), 'rows', 'stable'); %no need to transpose anymore
B = reshape(labels, size(A, 1), size(A, 2)) %no need to transpose either.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 3 Déc 2015

0 votes

If you do not need the labels to start from 1 at the upper left, then
[color, ~, idx] = unique(reshape(A, [], 3), 'rows');
B = reshape(idx, size(A,1), size(A,2));

9 commentaires

Guillaume
Guillaume le 3 Déc 2015
Well, you just have to make the unique 'stable' for the one to start in the upper left.
Slightly trickier is to make the scan horizontal rather than vertical.
Tan Wen Kun
Tan Wen Kun le 3 Déc 2015
Modifié(e) : Tan Wen Kun le 3 Déc 2015
well, I will try scan vertically. If my color image is store in variable img.
Is this correct?
A = cat(3, img(:,:1),img(:,:2),img(:,:3);
[~, ~, labels] = unique(reshape(A, [], 3), 'rows', 'stable'); %no need to transpose anymore
B = reshape(labels, size(A, 1), size(A, 2)) %no need to transpose either.
Tan Wen Kun
Tan Wen Kun le 3 Déc 2015
Modifié(e) : Tan Wen Kun le 3 Déc 2015
Error using unique (line 33) Unrecognized option.
What is this problem?
A = cat(3, img(:,:1),img(:,:2),img(:,:3);
completely unnecessary. this is just the same as
A = img;
"Error using unique (line 33) Unrecognized option". Which version of matlab are you running?
Tan Wen Kun
Tan Wen Kun le 3 Déc 2015
r2011b
Walter Roberson
Walter Roberson le 3 Déc 2015
The 'stable' option was introduced in R2012a. Are you using a MATLAB that is older than that?
Guillaume
Guillaume le 3 Déc 2015
Modifié(e) : Guillaume le 4 Déc 2015
That's a bit ancient. It looks like the stable sort was introduced in the next version (2012a).
You can use unique without 'stable' but it means that the labels will be ordered by colour. That is label 1 will be the colour with the lowest rgb triplet (black if it exists) label 2 the colour with the 2nd lowest rgb triplet, etc.
Is that other method can do like stable? I hope to get 1 at upper left
Columns 1 through 10
152 152 152 152 152 152 152 152 152 152
Columns 11 through 20
152 152 152 152 152 152 152 152 152 152
Columns 21 through 30
152 152 152 152 152 152 152 152 152 152
Guillaume
Guillaume le 3 Déc 2015
Without 'stable', it's not possible to have a row or column ordering of labels with unique. You would have to use sort with the stable option (I'm fairly certain that did exist in 2011b) and the find the duplicate yourself. It's possible but it's worthy of a question in itself.
As for your new question, please start a new question as it is barely related to the current one. When you ask the question, please explain clearly what your definition of connected is. In your above example, I consider 4 and 5 to be connected orthogonally, and 3 and 5 to be connected diagonally.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by