MATLAB Answers

segment a image and then save all segments with new names

조회 수: 1(최근 30일)
Mujtaba Tahir
Mujtaba Tahir 2021년 7월 28일
답변: Image Analyst 2021년 7월 29일
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);
inputFolder = uigetdir(); % Ask for a new one.
if inputFolder == 0
% User clicked Cancel
% 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)
% 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);
axis on;
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);
for k = 1 : length(leftColumns)
xline(leftColumns(k), 'Color', 'y', 'LineWidth', 2);
% 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, :);
caption = sprintf('Rows %d-%d, Columns %d-%d', row1, row2, col1, col2);
plotCounter = plotCounter + 1;
baseOutputFileName = baseInputFileName2;
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
fprintf('Now writing "%s"\n', fullOutputFileName);
imwrite(subImage, fullOutputFileName);

채택된 답변

Image Analyst
Image Analyst 2021년 7월 29일
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.

추가 답변(0개)


Community Treasure Hunt

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

Start Hunting!

Translated by