MATLAB: How to crop square of size 100 X 100 around the centroid of an image

roi extraction

I have a binary image with centroid and I want to crop 100 X 100 image patch from the centroid of the image. I tried using imrect command and imcrop command. But it does not give automatic extraction of that patch instead I need to drag and place the square. Please help with MATLAB code for this.

Best Answer

Change x_cent and y_cent for your centroids; change the name of images variables
x_cent = 120;
y_cent = 150;
size_of_cropped_img = 100;
centroide = [x_cent y_cent];
I = imread('circuit.tif');
imshow(I);
%I2 = imcrop(I,rect) crops the image I. rect is a four-element position vector of the
%form [xmin ymin width height] that specifies the size and position of the crop rectangle.
%imcrop returns the cropped image, I2.
xmin = x_cent-size_of_cropped_img/2;
ymin = y_cent-size_of_cropped_img/2;
I2 = imcrop(I,[xmin ymin size_of_cropped_img size_of_cropped_img]);
figure();
imshow(I2)