Image cropping in exact cordinate in cell array

1 次查看(过去 30 天)
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

采纳的回答

Turlough Hughes
Turlough Hughes 2021-11-27
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 个评论
Emre Mert Aygormus
Emre Mert Aygormus 2021-11-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by