How i can save all image after cropped in ciclo for?

1 view (last 30 days)
Hello,
I want save all the image cropped , but the image overwrites.
A=imread('peppers.png');
H=384;
W=512;
size=32;
% crop=zeros(32,32);
for i=1:size:H
for j = 1:size:W
crop = A(i:i+size-1,j:j+size-1,:);
save
end
end
Can i resolve this problem?
  1 Comment
Rik
Rik on 22 May 2020
Of course it overwrites. You don't use your loop indices anywhere after the cropping. You will need to generate a file name that is based on i and j.
You may want to consider storing the images as image files, instead of a mat file. I would also suggest using a different variable name, as size is a basic Matlab function, so shadowing it is not a smart move.
It also looks like you could benefit from using the blockproc function instead.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 May 2020
Try (untested):
rgbImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage);
sizeStep = 32;
for col = 1 : sizeStep : columns
for row = 1 : sizeStep : rows
croppedImage = A(row:row+sizeStep-1, col:col+sizeStep-1,:);
imshow(croppedImage); % Show it.
drawnow;
% Construct filename.
baseFileName = sprintf('Row %d, Col %d.png', row, col);
fullFileName = fullfile(pwd, baseFileName);
fprintf('Saving %s\n', fullFileName);
% Save to the drive in the current folder.
imwrite(croppedImage, fullFileName);
end
end
Don't use size as the name of a variable since it's the name of a built-in function.
  2 Comments
Pasquale Giordano
Pasquale Giordano on 22 May 2020
Edited: Image Analyst on 22 May 2020
Thanks, the script is that i want.
Thanks for the advice. Can you help me set up the game save by using indexes. I have difficulty making this setting.
Image Analyst
Image Analyst on 22 May 2020
Not sure what you mean. What game? What setting?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!