MATLAB: How to find the centroid of an object in an image and crop a portion of it around that centre point

image processing

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 227×227.
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!

Best 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