Note that there are some explanatory texts on larger screens.

plurals
  1. POCentroid of objects/connected components in matlab?
    text
    copied!<p>I am trying to find centroid of objects. I have already implemented connected components labeling and I have developed following code for centroid, it does give result but does not gives correct result: I have following output matrix i.e matrix_img:</p> <pre><code> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 2 2 2 2 0 0 0 0 0 1 1 1 1 1 1 0 2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 0 0 0 0 0 1 1 1 1 1 1 0 2 2 2 2 0 0 0 0 0 0 1 1 1 1 0 0 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0 2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0 2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 </code></pre> <p>and following is the code</p> <pre><code>n= max(max(matrix_img)); for k=1:n a(k)=length(find(matrix_img==k)); sx(k)=0; sy(k)=0; cx=0; cy=0; for i=1:1:r for j=1:1:c if(matrix_img(i,j)==k) sx(k)=sx(k)+i; sy(k)=sy(k)+j; cx=sx(k)/a(k); cy=sy(k)/a(k); end end end fprintf('Centroid of Object %d is %d and %d \n', k, cx, cy); end </code></pre> <p>It gives result like :</p> <pre><code>Centroid of Object 1 is 7 and 1.250000e+001 Centroid of Object 2 is 1.050000e+001 and 2.500000e+000 Centroid of Object 3 is 0 and 0 Centroid of Object 4 is 0 and 0 Centroid of Object 5 is 14 and 8 </code></pre> <p>Object 5 result is correct, object 2 is completely wrong and object 1 is partially wrong.. what shall I do?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload