segment a image and then save all segments with new names

3 views (last 30 days)
I have a image i have divided into 4 parts how can i save them with different names to a specific floder. I am using a code but it overwites the previos images as it saves all the segments with the same name. following is the code i am using....
% Specify the folder where the input files live.
inputFolder = 'C:\Users\user\Pictures\Saved Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(inputFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', inputFolder);
uiwait(warndlg(errorMessage));
inputFolder = uigetdir(); % Ask for a new one.
if inputFolder == 0
% User clicked Cancel
return;
end
end
% Specify the folder where the output files should be written to.
outputFolder = 'C:\Users\user\Pictures\Saved Pictures\New folder';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Get a list of all input PNG image files in the input folder with the desired file name pattern.
filePattern = fullfile(inputFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseInputFileName = theFiles(k).name;
fullInputFileName = fullfile(theFiles(k).folder, baseInputFileName);
fprintf('Now reading "%s"\n', fullInputFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullInputFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Now process the image somehow to create outputImage.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Read in image
grayImage = imread(fullInputFileName);
[rows, columns, numColorChannels] = size(grayImage);
imshow(grayImage);
axis on;
impixelinfo
numBandsVertically = 2;
numBandsHorizontally = 2;
topRows = round(linspace(1, rows+1, numBandsVertically + 1));
leftColumns = round(linspace(1, columns+1, numBandsHorizontally + 1));
% Draw lines over image
for k = 1 : length(topRows)
yline(topRows(k), 'Color', 'y', 'LineWidth', 2);
end
for k = 1 : length(leftColumns)
xline(leftColumns(k), 'Color', 'y', 'LineWidth', 2);
end
% Extract into subimages and display on a new figure.
hFig2 = figure();
plotCounter = 1;
for row = 1 : length(topRows) - 1
row1 = topRows(row);
row2 = topRows(row + 1) - 1;
for col = 1 : length(leftColumns) - 1
col1 = leftColumns(col);
col2 = leftColumns(col + 1) - 1;
subplot(numBandsVertically, numBandsHorizontally, plotCounter);
subImage = grayImage(row1 : row2, col1 : col2, :);
imshow(subImage);
caption = sprintf('Rows %d-%d, Columns %d-%d', row1, row2, col1, col2);
title(caption);
drawnow;
plotCounter = plotCounter + 1;
end
end
baseOutputFileName = baseInputFileName2;
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
fprintf('Now writing "%s"\n', fullOutputFileName);
imwrite(subImage, fullOutputFileName);
end

Accepted Answer

Image Analyst
Image Analyst on 29 Jul 2021
You need to use sprintf() at the right place to construct the filename you want. You are not doing that. Maybe if you indented your code correctly you'd see where to make the filename.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!