Image cropping in exact cordinate in cell array
2 views (last 30 days)
Show older comments
Emre Mert Aygormus
on 27 Nov 2021
Commented: Emre Mert Aygormus
on 27 Nov 2021
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
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!