Image cropping in exact cordinate in cell array

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Turlough Hughes
Turlough Hughes il 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 Commento
Emre Mert Aygormus
Emre Mert Aygormus il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by