On Nov 28, 9:15 am, "Ayesha " <bijlikamasl...@yahoo.com> wrote:
> I have found the objects in an image using connected components, and now i want to find the distance between boundary pixels and centroid, what shall I do? I have used the following code, but it gives error that, "Undefined function or variable "boundary_pixel". What is the proper and good way of doing so? without using built in functions?
>
> My matrix is:
> I = [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];
> My code is:
> [r c] = size(bw);
> num = max(max(bw));
> for k=1:num
> area(k)=length(find(bw==k));
> sumx(k)=0;
> sumy(k)=0;
> centroid_x=0;
> centroid_y=0;
>
> for i=1:1:r
> for j=1:1:c
> if (r1>0 & bw(r,c)~=bw(r1,c))(r+1<=r & bw(r+1,c)~=bw(r,c))(c1>0 & bw(r,c1)~=bw(r,c)) (c+1<=c & bw(r,c+1)~=bw(r,c))
> boundary_pixel = bw(r,c);
> end
> if(bw(i,j)==k)
>
> sumx(k)=sumx(k)+i;
> sumy(k)=sumy(k)+j;
> centroid_x=sumx(k)/area(k);
> centroid_y=sumy(k)/area(k);
>
> end
> end
> end
> euclidean_distance = sqrt(sum((boundary_pixelcentroid_x).^2))
> fprintf('Object %d: Area: %d Centroid x: %d Centroid y: %d \n', k, area(k), centroid_x, centroid_y);
> end
That error means your "if" condition is never getting satisfied.
Also, how are you calculating "Area" by calculating length? It should
be product of length and height.
Your question and method both are not clear to me. If you calculate
distance between centroid and boundary pixel, why can't you do just
simple subtraction of coordinates?
Regards,
 Parag S. Chandakkar.
