0

I have a binary image and divided this image into multiple blocks. e.g. a (60x60) image divided into 4 blocks

(30x30) (30x30) (30x30) (30x30)

Now I want to calculate the centroid of each block, I look up for a formula to do that and ended up with

Centroid Formula

How can I do that using matlab, and what is the difference between this method and regionprops(...,'Centroid')

Update 1: This is what I've written so far...

m = 2;
n = 2;
im = imread('C:\Users\Home\Desktop\Samples\0_1.bmp');
flippedIm = 1-im;
boundary = regionprops(flippedIm, 'BoundingBox');
boundedIm = vertcat(boundary(1).BoundingBox);
xmin = boundedIm(1);
ymin = boundedIm(2);
width = boundedIm(3);
height = boundedIm(4);
croppedIm = imcrop(flippedIm,[xmin ymin width height]);

%USE X TO TEST
x = reshape(1:9,3,3)';
x = myPadding(x,m,n);

%a contains cells (blocks)
a = mySplit(x,m,n);

measurements = regionprops(x,'Centroid');
display(measurements(1));

And the result of that code is ...

measurements = 

9x1 struct array with fields:

Centroid
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Hossam Houssien
  • 359
  • 4
  • 14
  • 2
    The centroid calculated by `regionprops` should be identical to the one calculated by your formula. Are you getting different results? Why is it significant that your image is divided into 4 blocks? Unless there is something missing from your question, if you can find the centroid of one image you can find the centroids of 4 images. – beaker Feb 24 '17 at 16:28
  • And what have you done? – smttsp Feb 24 '17 at 16:47
  • @smttsp I have added what I did so far – Hossam Houssien Feb 24 '17 at 20:03
  • @beaker `regionprops` gives me a 9x1 struct array as I mentioned above, shouldn't centroid be (x,y) coordinates? why it give me 9x1 values? Sorry but can you explain to me where is my wrong, thanks in advance – Hossam Houssien Feb 24 '17 at 20:07
  • @rayryeng I still don't get it, you mean that one image can have more than one centroid? – Hossam Houssien Feb 24 '17 at 20:10
  • @HossamHoussien You are using `regionprops` on non-binary image that has values from 1 to 9 in a 3 x 3 grid. The effect of this is that it treats each pixel as a separate object - That's why you have 9 centroids and thus have a 9 element structure array. If you examined each centroid of the structure array, you will see that it's exactly equal to the locations of each element in the 3 x 3 grid. Simply put, you are not using the function properly with the given input. – rayryeng Feb 24 '17 at 20:12
  • BTW, I think you may have meant to use `a` in your `regionprops` call instead of `x`, but `x` is still a non-binary matrix. You need to either convert to binary, or ensure that the input is really what you want. – rayryeng Feb 24 '17 at 20:19
  • @rayryeng I understand now, also your answer [here](http://stackoverflow.com/a/25984775/6365031) helped a lot. Thanks for explaining :) – Hossam Houssien Feb 24 '17 at 20:20
  • @HossamHoussien Ah :) I wrote that answer a long time ago. It is my pleasure. – rayryeng Feb 24 '17 at 20:24

0 Answers0