Image cropping in exact cordinate in cell array

9 views (last 30 days)
Hello, i have problem with cropping images that in cell array elements, in exact cordinates.
Here is my code, i tried cellfun function, but i can not use cellfun function with my cordinates. My cordinates: [133 133 150 150]
Is there another way to crop all images and save another cell array or if it possible how to use my cordinates with cellfun function?
for n=1:64
cd 'myDir';
images{n} = imresize(imread(sprintf('%d.jpg',n)),[400,400]);
grayImages = cellfun(@rgb2gray, images, 'UniformOutput', false);
cd ..\
cropped_imgs = cellfun(@imcrop, grayimages, 'UniformOutput', false); % i need to apply [133 133 150 150] to here.
end

Accepted Answer

Turlough Hughes
Turlough Hughes on 27 Nov 2021
You can make the following modification (make sure to place your cellfun calls after the for loop)
cropped_imgs = cellfun(@(I) imcrop(I, [133 133 150 150]),...
grayImages, 'UniformOutput', false);
However, are you using images and grayImage for anything other than getting the cropped grayscale images? If you don't need them then you don't store them. For example, the following would be much better:
N = 4;
croppedGrayImages = repmat({zeros(400,400,3)}, N, 1); % preallocate what you wish to store
for n = 1:N
I = imread(sprintf('car_%d.jpg',n)); % cars_%d as an example
resizedImage = imresize(I,[400,400]);
grayImage = rgb2gray(resizedImage);
croppedGrayImages{n} = imcrop(grayImage,[133 133 150 150]); % store output
end
note that space for croppedGrayImages is preallocated and resizedImage and grayImage initialise on the first loop.
  1 Comment
Emre Mert Aygormus
Emre Mert Aygormus on 27 Nov 2021
Thanks for answering. I got your point that "if you don't need them then don't store", this is going to help me. Thanks for your time and both of your answers.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by