How to compare two matrix?

redChannel = IM(:, :, 1);
greenChannel = IM(:, :, 2);
blueChannel = IM(:, :, 3);
for yourNumber = 1 : max(labelimg(:))
thisLabel = labelimg == yourNumber; % for example 2 or 3 or whatever region you want.
meanR(yourNumber) = mean(redChannel(thisLabel));
meanG(yourNumber) = mean(greenChannel(thisLabel));
meanB(yourNumber) = mean(blueChannel(thisLabel));
end
table(neigbor label)=
2 3 4 5
2 1
3 1 1
4 1 1 1
5 1 0 1 1
I got a table which show the neighbor label and I got mean rgb for each label, I just need loop through the left triangle because another part is same.
The mean value for the label is double type, how I can round to integer only?
I got the mean of each label region and then I now want to compare them.
for i=2 : max(table(:))
for j=2:max(table(:))
if(i,j+1)=1 %1 mean 2 label are neighbor
if compare(j+1,i)<=(rgb+-5) %then reassign the label
if (i>j+1) label i = label j+1 %always assign to smaller label
else label j+1 = label to i
Final result I hope to get is like this
label 4 and 5 is within(rgb+-5) with label 2 so all assign to label 2
label =
3 3 3 3 3 3
1 1 1 1 1 1
1 2 2 1 4 1
1 1 1 1 4 1
5 5 5 1 1 1
5 5 5 5 5 5
result =
3 3 3 3 3 3
1 1 1 1 1 1
1 2 2 1 2 1
1 1 1 1 2 1
2 2 2 1 1 1
2 2 2 2 2 2

 采纳的回答

abs(meanR(j+1)-meanR(i)) <= 5 &&
abs(meanB(j+1)-meanB(i)) <= 5 &&
abs(meanG(j+1)-meanG(i)) <= 5

4 个评论

This solve the problem, did not need to using compare, I forget can do like this by using absolute.
for i=2 : max(table(:))
for j=2:max(table(:))
if(i,j+1)=1 %1 mean 2 label are neighbor
if (abs(meanR(i,j+1)-meanR(i,j)) <= 5 &&
abs(meanB(i,j+1)-meanB(i,j)) <= 5 &&
abs(meanG(i,j+1)-meanG(i,j)) <= 5)
This statement true, I want to reassign label to smaller label. Below code is correct?
if (label(i,j)>label(i,j+1))
label(i,j) = label(i,j+1) %always assign to smaller label
else label(i,j+1) = label(i,j)
The definition of meanR and so on that you show in your original post are indexed only by a single value, not by two values.
if(i,j+1)=1
is not valid syntax, not even if you change the = to == . You want
if table(i,j+1) == 1
You also do not want to use
for i=2 : max(table(:))
because table consists entirely of 0 and 1 so max() of it is going to be 1, and for i=2:1 is not going to do anything. You want to run i to the maximum label, not the maximum table.
ya,it is one value for meanR, ytd I think and write wrongly
so I should use for i=2:max(label(:)) ?
label =
1 1 1 1 1
1 2 1 3 1
1 1 1 1 1
I want to made label 3 to label 2, how to do?
for i=2 : max(label(:))
for j=2:max(label(:))
if table(i,j+1)==1 %1 mean 2 label are neighbor
if (abs(meanR(j+1)-meanR(i)) <= 5 && abs(meanB(j+1)-meanB(i)) <= 5 && abs(meanG(j+1)-meanG(i)) <= 5)
if label(j+1)>i
label(j+1)=label(i);
else
label(i)=label(j+1);
end
Error in (line 122)
if (abs(meanR(j+1)-meanR(i)) <= 5 &&
abs(meanB(j+1)-meanB(i)) <= 5 && abs(meanG(j+1)-meanG(i))
<= 5)
why i got this problem?
maxlab = max(label(:));
for i = 1 : maxlab
for j = 1 : maxlab - 1
if table(i,j+1) == 1
if abs(meanR(j+1)-meanR(i)) <= 5 &&
abs(meanB(j+1)-meanB(i)) <= 5 && abs(meanG(j+1)-meanG(i))
<= 5
if label(i, j+1) > label(i, j)
label(i, j+1) = label(i,j)
else
label(i,j) = label(i, j+1);
end
end
end
end

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-12-6

0 个投票

The answer to "how I can round to integer only?" is to use the int32() function.

5 个评论

how to compare mean value for both label? what is the function name?
@Tan Wen Kun: The function name of what? What should the command "compare(j+1,i)<=(rgb+-5)" do? It is impossible to guess, what you want.
I want to compare the mean value of label which is neighbor.
I want to compare whether the meanR, meanG, meanB value of label 2 and label 3 is within 5 or not? If this 3 statement true then I reassign label 3 to label 2.
I have no idea what that triangular table is so I can't answer. And I don't know what reassigning labels is going to do. I think you're trying to do some kind of region growing/merging because you started out with a bad segmentation that was made worse by oversegmentation by watershed and now you're trying to fix it in a way that won't work.
table(neigbor label)=
2 3 4 5
2 1 1 1 1
3 1 1 1 0
4 1 1 1 1
5 1 0 1 1
this table is show which label is neighbor label.
What I mean triangular is because table(2,3) is same as table(3,2) so I do not need to check twice whether their rgb value is within 5 or not.
I only need check half side only

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by