Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Distance from boundary pixels to centroid?

Subject: Distance from boundary pixels to centroid?

From: Ayesha

Date: 28 Nov, 2011 16:15:09

Message: 1 of 4

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 (r-1>0 & bw(r,c)~=bw(r-1,c))|(r+1<=r & bw(r+1,c)~=bw(r,c))|(c-1>0 & bw(r,c-1)~=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_pixel-centroid_x).^2))
  fprintf('Object %d: Area: %d Centroid x: %d Centroid y: %d \n', k, area(k), centroid_x, centroid_y);
end

Subject: Distance from boundary pixels to centroid?

From: Parag Chandakkar

Date: 28 Nov, 2011 17:26:02

Message: 2 of 4

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  (r-1>0 & bw(r,c)~=bw(r-1,c))|(r+1<=r & bw(r+1,c)~=bw(r,c))|(c-1>0 & bw(r,c-1)~=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_pixel-centroid_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.

Subject: Distance from boundary pixels to centroid?

From: Matt J

Date: 28 Nov, 2011 18:37:07

Message: 3 of 4

"Ayesha " <bijlikamasla12@yahoo.com> wrote in message <jb0c2d$ipu$1@newscl01ah.mathworks.com>...
> 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?
=================

'bw' is a bad name for the label image, since the label image is not black-and-white.

Below is a method of finding both the centroid and the boundary pixels which involves just a small modification to the version we discussed a few days ago,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/314720#859655

It avoids practices that I HIGHLY RECOMMENDED THAT YOU ABANDON!



[M,N]=size(matrix_img);
[X,Y]=ndgrid(1:M,1:N);
bmask=[0 1 0; 1 1 1; 0 1 0]/5;

n = max(matrix_img(:));

cx=nan(1,n);
cy=nan(1,n);
Xboundary=cell(1,n)
Yboundary=cell(1,n);

for k=1:n
 
  idx=(matrix_img==k);

 cx(k)=mean(X(idx)); %The k-th centroid X coord
 cy(k)=mean(Y(idx)); %The k-th centroid Y coord

 [Xboundary{k},Yboundary{k}]=find( idx & conv2(double(idx),bmask,'same')<1);
 
end

Subject: Distance from boundary pixels to centroid?

From: ImageAnalyst

Date: 28 Nov, 2011 20:25:03

Message: 4 of 4

If you have the Image Processing Toolbox, you can use bwdist().

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us