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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!