How to use looping method to crop image dynamically (auto-cropping)?
    6 views (last 30 days)
  
       Show older comments
    
Hello. How to crop image using dynamic cropping method in matlab? Is there any example implementation codes?
Lets say, my original image have two different sizes which are 482 x 841 pixels and 608 x 865 pixels, so can I use loop step at first to extend the pixels which those that not enough pixels if I want to crop into 512 x 512 pixels? If the image size is enough to crop into 512 x512 pixels so no need to extend the pixels. I recently use this code:- 
%To read input image
I=imread('1_776.jpg');
imfinfo('1_776.jpg')
First image, image size 482 x 841 px

Second image, image size 608 x 865 px

%To resize image
out = padarray(I, [10 10], 0, 'both');
imshow(out);
imwrite(out,'resize1.jpg');
but this is manual. Now I want it to automatically crop only specific area.
1 Comment
  Rik
      
      
 on 2 Jul 2024
				So you want to crop images if they exceed 512 pixels in either height or width, and want to pad them with black if they are smaller than 512x512?
Answers (1)
  Tejas
      
 on 13 Aug 2024
        Hello Dayangku, 
I am assuming you want to automate the process of resizing images to 512 x 512 pixels by adding padding if any dimension is smaller than 512 pixels and cropping if any dimension exceeds 512 pixels.
To achieve this, follow the steps below: 
- List the names of the images to be resized in a cell array.
- Loop through each image in the cell array.
- Check if the image’s rows or columns require padding.
- Add padding to the rows or columns if necessary.
- Crop the image to the desired size using the ‘imcrop’ function.
- Save the cropped image using the ‘imwrite’ function.
The code snippet below demonstrates how this can be done: 
% Add the names of images to be resized
listOfImages = {'I1.jpeg', 'I2.jpeg'}; 
desiredCropSize = [512, 512];
for itr = 1:length(listOfImages)
    % Read image based on current index
    I = imread(listOfImages{itr});
    [rows, cols, ~] = size(I);
    % Check if row/column require padding
    if rows < desiredCropSize(1) || cols < desiredCropSize(2)
        % Calculate padding sizes
        padRows = max(0, desiredCropSize(1) - rows);
        padCols = max(0, desiredCropSize(2) - cols);
        % Evenly add padding to the image. Image is padded twice 
        % to deal with odd number of padRows/padCols
        I = padarray(I, [ceil(padRows/2), ceil(padCols/2)], 'both');
        I = padarray(I, [floor(padRows/2), floor(padCols/2)], 'both');
    end
    % Crop the image to the desired size
    croppedImage = imcrop(I, [1, 1, desiredCropSize(2)-1, desiredCropSize(1)-1]);
    % Display the cropped image
    imshow(croppedImage);
    % Save the cropped images
    outputFileName = sprintf('Image_%d_resized.jpg', itr);
    imwrite(croppedImage, outputFileName);
    imfinfo(outputFileName)
end
For a better understanding of the functions ‘imcrop’ and ‘for’, refer to the following links: 
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

