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:
Obtain all pixels from centroid coordinates.

Subject: Obtain all pixels from centroid coordinates.

From: Jack Branning

Date: 22 Jul, 2010 10:34:04

Message: 1 of 9

Hi,

I have an image which contains 24 squares of roughly equal size. I have used segmentation techniques and regionprops to obtain the coordinates that represent the center of each of these squares...

Now I need to use these centroid coordinates to create a 50x50 square. I then need to extract all pixels within these squares in a 2-D array.

Does anyone know how to create a region of interest (ROI) based on a center coordinate?

Thanks in advance for any help you can give me!

Jack

Subject: Obtain all pixels from centroid coordinates.

From: Sean

Date: 22 Jul, 2010 13:23:04

Message: 2 of 9

"Jack Branning" <jbr.nospam@nospam.com> wrote in message <i296qs$590$1@fred.mathworks.com>...
> Hi,
>
> I have an image which contains 24 squares of roughly equal size. I have used segmentation techniques and regionprops to obtain the coordinates that represent the center of each of these squares...
>
> Now I need to use these centroid coordinates to create a 50x50 square. I then need to extract all pixels within these squares in a 2-D array.

I don't understand what you're trying to do here, but I think I can still answer your question


> Does anyone know how to create a region of interest (ROI) based on a center coordinate?

row_idx = row_centroid;
col_idx = column_centroid;

ROI_size = 11; %Will have to add more if you want even ROI_size (pick which side is greater) or if you want a non square ROI_size

ROI = I((row_idx-floor(ROI_size/2)):(row_idx+floor(ROI_size/2)),(row_idx-floor(ROI_size/2)):(row_idx+floor(ROI_size/2)));%I is your image. These are the actual pixels.

If you want a mask:
Imask = false(size(I));
Imask((row_idx-floor(ROI_size/2)):(row_idx+floor(ROI_size/2)),(row_idx-floor(ROI_size/2)):(row_idx+floor(ROI_size/2))) = true; %logical true part will be your region of interest.

Subject: Obtain all pixels from centroid coordinates.

From: Jack Branning

Date: 22 Jul, 2010 14:21:07

Message: 3 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i29gno$rmu$1@fred.mathworks.com>...
> "Jack Branning" <jbr.nospam@nospam.com> wrote in message <i296qs$590$1@fred.mathworks.com>...
> > Hi,
> >
> > I have an image which contains 24 squares of roughly equal size. I have used segmentation techniques and regionprops to obtain the coordinates that represent the center of each of these squares...
> >
> > Now I need to use these centroid coordinates to create a 50x50 square. I then need to extract all pixels within these squares in a 2-D array.
>
> I don't understand what you're trying to do here, but I think I can still answer your question
>

Thanks for your help! To clarify, I am trying to extract the pixels from each of the squares in the image. I am thinking that the best way to do this is to use the center coordinates that I have, and somehow make 50x50 pixel squares around these coordinates for each...

Subject: Obtain all pixels from centroid coordinates.

From: Sean

Date: 22 Jul, 2010 14:35:04

Message: 4 of 9

"Jack Branning" <jbr.nospam@nospam.com> wrote in message <i29k4j$8m5$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i29gno$rmu$1@fred.mathworks.com>...
> > "Jack Branning" <jbr.nospam@nospam.com> wrote in message <i296qs$590$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I have an image which contains 24 squares of roughly equal size. I have used segmentation techniques and regionprops to obtain the coordinates that represent the center of each of these squares...
> > >
> > > Now I need to use these centroid coordinates to create a 50x50 square. I then need to extract all pixels within these squares in a 2-D array.
> >
> > I don't understand what you're trying to do here, but I think I can still answer your question
> >
>
> Thanks for your help! To clarify, I am trying to extract the pixels from each of the squares in the image. I am thinking that the best way to do this is to use the center coordinates that I have, and somehow make 50x50 pixel squares around these coordinates for each...

The what I provided is fine
ROI_size = 50; %You'll have to pick which side of the centroid gets 24 added and the other 25; to do this use ceil() on one and floor on the other.

Then the ROI code above will work. You'll have to loop over all of them and store each ROI or you could create a mask for the entire thing.
2 ways to store them would be:
1. Use cells:
>>ROI = cell(24,1)% Then store each ROI as a cell
2. Use the 3rd dimension:
>>ROI = zeros(50,50,24); %Where each page is a ROI. This works because they're all the same size.

Subject: Obtain all pixels from centroid coordinates.

From: Sean

Date: 22 Jul, 2010 14:43:20

Message: 5 of 9

"Jack Branning" <jbr.nospam@nospam.com> wrote in message <i29k4j$8m5$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i29gno$rmu$1@fred.mathworks.com>...
> > "Jack Branning" <jbr.nospam@nospam.com> wrote in message <i296qs$590$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I have an image which contains 24 squares of roughly equal size. I have used segmentation techniques and regionprops to obtain the coordinates that represent the center of each of these squares...
> > >
> > > Now I need to use these centroid coordinates to create a 50x50 square. I then need to extract all pixels within these squares in a 2-D array.
> >
> > I don't understand what you're trying to do here, but I think I can still answer your question
> >
>
> Thanks for your help! To clarify, I am trying to extract the pixels from each of the squares in the image. I am thinking that the best way to do this is to use the center coordinates that I have, and somehow make 50x50 pixel squares around these coordinates for each...

You could also just use the 'BoundingBox' output of regionprops and segment everything inside that.

Subject: Obtain all pixels from centroid coordinates.

From: Jack Branning

Date: 22 Jul, 2010 21:20:14

Message: 6 of 9


> You could also just use the 'BoundingBox' output of regionprops and segment everything inside that.

Thanks again!

I have been trying all your suggestions but have had no luck. To simplify things, I am now working off the following 10x10 matrix:

A =

     1 1 1 1 1 1 1 1 1 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 1 1 1 1 1 1 1 1 1

Where the zeros represent the regions of interest, where I need to extract the pixel data (here, the pixels are just 0).

The centroid data I have is:

c =
      3 3
      3 8
      8 3
      8 8

which represent the coordinates of the center pixels for each square.

How can I obtain all the values for each 3x3 square for each of these coordinates?

Subject: Obtain all pixels from centroid coordinates.

From: Jeff

Date: 23 Jul, 2010 04:00:09

Message: 7 of 9

"Jack Branning" <jbr.nospam@nospam.com> wrote in message <i2acme$502$1@fred.mathworks.com>...
>
> > You could also just use the 'BoundingBox' output of regionprops and segment everything inside that.
>
> Thanks again!
>
> I have been trying all your suggestions but have had no luck. To simplify things, I am now working off the following 10x10 matrix:
>
> A =
>
> 1 1 1 1 1 1 1 1 1 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 1 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1 1 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 1 1 1 1 1 1 1 1 1
>
> Where the zeros represent the regions of interest, where I need to extract the pixel data (here, the pixels are just 0).
>
> The centroid data I have is:
>
> c =
> 3 3
> 3 8
> 8 3
> 8 8
>
> which represent the coordinates of the center pixels for each square.
>
> How can I obtain all the values for each 3x3 square for each of these coordinates?

BoundingBox combined with imcrop won't work for this exact instance, as the handling of boundary pixels is...interesting. From the help section on imcrop:
the output image includes all pixels in the input image that are completely or partially enclosed by the rectangle


Here's what's working for me, which isn't using centroids:

Ai = ~A; %turn 0's into 1's, and vice versa
Ai_lab = bwlabel(Ai); % creates labeled image of all connected 1's

for j = 1:max(max(Ai_lab)) %loop j through all connected components
[ r c ] = find(Ai_lab == j); % return 1's row coordinates in r and columns in c
Ai_struct.j = [r c]; % store each connected component's cooridnates
end

Subject: Obtain all pixels from centroid coordinates.

From: Jeff

Date: 23 Jul, 2010 05:41:06

Message: 8 of 9

"Jeff " <jea@gene.dot.com> wrote in message <i2b449$130$1@fred.mathworks.com>...
> "Jack Branning" <jbr.nospam@nospam.com> wrote in message <i2acme$502$1@fred.mathworks.com>...
> >
> > > You could also just use the 'BoundingBox' output of regionprops and segment everything inside that.
> >
> > Thanks again!
> >
> > I have been trying all your suggestions but have had no luck. To simplify things, I am now working off the following 10x10 matrix:
> >
> > A =
> >
> > 1 1 1 1 1 1 1 1 1 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 1 1 1 1 1 1 1 1 1
> > 1 1 1 1 1 1 1 1 1 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 0 0 0 1 1 0 0 0 1
> > 1 1 1 1 1 1 1 1 1 1
> >
> > Where the zeros represent the regions of interest, where I need to extract the pixel data (here, the pixels are just 0).
> >
> > The centroid data I have is:
> >
> > c =
> > 3 3
> > 3 8
> > 8 3
> > 8 8
> >
> > which represent the coordinates of the center pixels for each square.
> >
> > How can I obtain all the values for each 3x3 square for each of these coordinates?
>

Sorry, I missed the part where you want to crop out a second image. Give this a try:

Ai = ~A; %turn 0's into 1's, and vice versa
Ai_lab = bwlabel(Ai); % creates labeled image of all connected 1's

for j = 1:max(max(Ai_lab)) %loop j through all connected components
    
    Ai_temp = ismember(Ai_lab, j); %create matrix specific to one connected component at a time
    B_crop = B .* Ai_temp; %zero out any elements in original image B that aren't in Ai_temp
    B_crop = B_crop(any(B_crop), any(B_crop)); %remove any zero elements

end

Subject: Obtain all pixels from centroid coordinates.

From: Sean

Date: 23 Jul, 2010 13:30:26

Message: 9 of 9

"Jack Branning" <jbr.nospam@nospam.com> wrote in message <i2acme$502$1@fred.mathworks.com>...
>
> > You could also just use the 'BoundingBox' output of regionprops and segment everything inside that.
>
> Thanks again!
>
> I have been trying all your suggestions but have had no luck. To simplify things, I am now working off the following 10x10 matrix:
>
> A =
>
> 1 1 1 1 1 1 1 1 1 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 1 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1 1 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 0 0 0 1 1 0 0 0 1
> 1 1 1 1 1 1 1 1 1 1
>
> Where the zeros represent the regions of interest, where I need to extract the pixel data (here, the pixels are just 0).
>
> The centroid data I have is:
>
> c =
> 3 3
> 3 8
> 8 3
> 8 8
>
> which represent the coordinates of the center pixels for each square.
>
> How can I obtain all the values for each 3x3 square for each of these coordinates?

A =[...
     1 1 1 1 1 1 1 1 1 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 0 0 0 1 1 0 0 0 1
     1 1 1 1 1 1 1 1 1 1];


c = [...
      3 3
      3 8
      8 3
      8 8];
%Ok you've successfully created an (inverse) mask; i.e. what you want right
%now is 0 (false) and what you don't want is 1; true.

%BWCONNCOMP with REGIONPROPS:
CC = bwconncomp(~A); %(~A) means "NOT" A this is a logical condition to make your ROIs =1.
RP = regionprops(CC,'PixelList','PixelIdxList');
%Note the two different properties I have:
% -'PixelList' is a list of every [row col] coordinate of the points
% -'PixelIdxList' is a list of direct indices to the points you want.
% This is more useful but harder to visualize
%Both of these are 4x1 struct arrays meaning that each component will have
%the indices to each ROI.
RP(1).PixelList
%These are the direct coordinates to each of the nine members of ROI1
RP(1).PixelIdxList
%These are the linear indexes in to A. For extracting values there are
%easier and faster to use i.e.
A(RP(1).PixelIdxList) %These are the values.
%If A is the mask and B is the actual image then you would do:
B(RP(1).PixelIdxList %for the image values
%If you want to extract them with the PixelList you would need an additonal
%call to sub2ind().
%Hope this helps

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