MATLAB Answers

0

How can I find the centroid of an object in an image and crop a portion of it around that centre point

Hi there,
I want to find the centroid for the marked object. After finding the centroid, I need to crop the image around the centroid with a size of 227x227.
Can someone please help by guilding me or writing the code in which I will be able to find the centroid and crop the object from the image? Thank you in advance!

  2 Comments

What is the "marked object"? I guess it's not the entire white disk because that's not marked. So do you mean the region marked in red? If so, you can just extract that from indexing and use imresize() to force it into a 227x227 size and shape. Or is the marked area just the rough stuff inside the red box and you want to find the weighted centroid (this is different than the centroid) of the stuff inside the marked/red box? The weighted centroid takes into account the gray levels while the centroid does not and would just be the exact center of the box.
If you've marked it in red, then you can just crop that red box - why do you need to find the centroid? Just crop it without finding the centroid.
OR do you want to find the rough stuff, and then find the centroid of that, without there being a red/marked box placed in advance? If so, do a texture filter then threshold it to get a mask and find the centroid or weighted centroid of that, then make a 227x227 box centered about that point.
Overall, you've given a very imprecise description of what you want to do.
And for the staff answer you said you have an image that it didn't work for but you forgot to attach the image!
First of all, sorry for the imprecise description and thanks for replying! What I wanted was to find the rough stuff and then find the centroid of that without there being a red/marked box placed in advance.

Sign in to comment.

2 Answers

Answer by KSSV
on 8 Mar 2019
 Accepted Answer

I = imread('image.jpeg') ;
[nx,ny,nt] = size(I) ;
x = 1:ny ; y = 1:nx ;
C = round([mean(x) mean(y)]) ;
idx = C(2)-227:C(2)+227 ;
idy = C(1)-227:C(1)+227 ;
I = I(idx,idy,:) ;
imshow(I)
YOu may play around with idx and idy

  1 Comment

Sign in to comment.


Answer by Shunichi Kusano on 8 Mar 2019

This is a rough code to detect the centroid of the object. I interpreted that the "object" is the largest one seen in the center.
%% read image
img = imread('image.jpeg');
figure, imshow(img);
%% region filter to reject smooth field
Ifilt = rangefilt(double(rgb2gray(img)), ones(5,5));
figure, imshow(Ifilt, []);
BW = Ifilt > 50;
figure, imshow(BW);
%% processing binary image
BW = imfill(BW, 'holes');
BW = bwareaopen(BW, 10000); % small blobs are removed
figure, imshow(BW);
%% centroid calculation
[c,r] = find(BW);
centroid = [mean(c),mean(r)]; % or you can use regionprops
figure
imshow(img);
hold on;plot(centroid(2), centroid(1), 'r*');
hope this helps.

  2 Comments

Hey there, thanks for the help! It does work but for certain images it couldn't detect the centroid of the object.

Sign in to comment.