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

1 Ansicht (letzte 30 Tage)
Pasquale Giordano
Pasquale Giordano am 22 Mai 2020
Kommentiert: Image Analyst am 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 Kommentar
Rik
Rik am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Pasquale Giordano
Pasquale Giordano am 22 Mai 2020
Bearbeitet: Image Analyst am 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 am 22 Mai 2020
Not sure what you mean. What game? What setting?

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by