# creating a correct mathematical expression from matlab code?

I have a labeled image L and binary image B. I am able to identify the necessary labels which have 1 in the binary image.

Explanation: We will take each and every label from 1 to 6. say, L=1 at (row,column)(1,1),we will check the same at binary image B(row,column) i.e.,B(1,1)=1. we will increase the count ctr_W by 1. For label L=1 the count ctr_w=13,count ctr_B=2; so count ctr_W>count ctr_B. we will take L=1 as white. For label L=2 ctr_w=1 and ctr_B=14;so ctr_w

``````L={1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
3 3 3 4 4 5 5 5 6 6
3 3 3 4 4 5 5 5 6 6}

B={    1 1 1 1 1 0 0 0 0 1
1 0 0 1 1 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 0}

labels={1,2,3,4,5,6}
while ~isempty(labels)
for i=length(lebels)
Hih=labels(i);
[r,c] =find(L==Hih);
ctr_B= 0;
ctr_W=0;
for i = 1 : length(r)
if((B(r(i),c(i)) ~= 0))
ctr_W=ctr_W+1;
else
ctr_B=ctr_B+1;
end

end
if(ctr_W>ctr_B)
white=[white,Hih];
else
black=[black,Hih];
end
end
end
``````

I got labels 1,6. How can I write the mathematical expression which exactly represents the code? kindly help me. Thanks in advance

If I understand correctly you want to count labels, only if they are "enabled" by the binary mask.

First of all: learn the difference between cell arrays and matrices, and realize why you do not need cell arrays.

``````L=[1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
3 3 3 4 4 5 5 5 6 6
3 3 3 4 4 5 5 5 6 6];

B=[1 1 1 1 1 0 0 0 0 1
1 0 0 1 1 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 0];
``````

``````L2=L.*B;
``````

Count how many of each unique value exist:

``````counts=histcounts(L2);
``````

Ignore the first one (zeroes)

``````counts(1)=[];
``````

There you go, `counts` contains how many of each of the labels exist, within the mask.