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

1 vue (au cours des 30 derniers jours)
Pasquale Giordano
Pasquale Giordano le 22 Mai 2020
Commenté : Image Analyst le 22 Mai 2020
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 commentaire
Rik
Rik le 22 Mai 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.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 22 Mai 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 commentaires
Pasquale Giordano
Pasquale Giordano le 22 Mai 2020
Modifié(e) : Image Analyst le 22 Mai 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 le 22 Mai 2020
Not sure what you mean. What game? What setting?

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by