Save each image to different folder

1 view (last 30 days)
Khalid
Khalid on 14 May 2013
I have a matlab code for image 300X720 pixel has been divide (split) to 4X12 parts (the total is 48 parts), and I would like to to do the following step which I couldn't do it:
  1. first, I want to make a loop (insert 6 image instead of one image).
  2. second, I want to save each image output (48 parts for each image) in different file (folder).
Here my last matlab code for one image splited to 4X12 (48 parts).
clc; % Clear the command window.
format compact;
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = ('C:\Users\HOME\Desktop\image.tif');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage=imread(fullFileName);
grayImage = rgb2gray(grayImage);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows1 columns1 numberOfColorBands1] = size(grayImage);
% Resize the image to be 300x720
grayImage = imresize(grayImage, [300, 720]);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows2 columns2 numberOfColorBands2] = size(grayImage);
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[.05 .05 .9 .9]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
folder = pwd;
plotIndex = 1;
for i=1:4
% Extract out the small image.
row1 = (i-1)*75+1;
row2 = row1 + 74;
for j=1:12
column1 = (j-1)*60+1;
column2 = column1 + 59;
smallImage = grayImage(row1:row2, column1:column2);
% Display the small image.
subplot(4, 12, plotIndex);
imshow(smallImage);
caption = sprintf('Image #%d', plotIndex);
title(caption);
plotIndex = plotIndex + 1;
% Save the small image to disk.
baseFileName=sprintf('image%i.jpg',i,'image%j.jpg',j);
fullFileName = fullfile(folder, baseFileName);
fprintf('Saving %s\n', fullFileName);
imwrite(smallImage, fullFileName);
end;
end;
I'm looking for your attention, and thankx for every one here which answering the question
  2 Comments
Jan
Jan on 14 May 2013
Beside the ugly formatted code, you forgot to mention the problem. Does the code solve the question already? If not, what does not work as expected?
Khalid
Khalid on 20 May 2013
There were some thing wrong on web, that is why became like that, however I found the code.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 14 May 2013
Looks like your sprintf() to create the base file name is messed up:
baseFileName=sprintf('image%i.jpg',i,'image%j.jpg',j);
what are you trying to do there??? Maybe try this:
baseFileName=sprintf('image_%d_%d.jpg', i, j);

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!