How to crop square of size 100 X 100 around the centroid of an image?
    6 views (last 30 days)
  
       Show older comments
    
    Manjiree Waikar
 on 9 Sep 2017
  
    
    
    
    
    Commented: Manjiree Waikar
 on 9 Sep 2017
            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.
3 Comments
Accepted Answer
  Jose Marques
      
 on 9 Sep 2017
        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)
6 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!