diving all images in folder into n horizental and vertical strips

2 次查看(过去 30 天)
i have 500 images and i want to divide all of them into 4 equal parts automatically and get stored to a new location. i have a code which can devide a image into n number of horizental and vertical strips but i want to use that code on all the images and store the cropped new 4 images to a seperate folder

采纳的回答

Image Analyst
Image Analyst 2021-7-26
You can get the rows and columns like this
[rows, columns, numberOfColorChannels] = size(yourImage);
r = ceil(rows/4);
c = ceil(c/4);
dividingRows = 1 : r : rows;
dividingColumns = 1 : c : columns
for k = 1 : length(dividingRows)
row1 = dividingRows(k);
row2 = row1 + r;
if row2 >= rows
row2 = rows
end
thisBand = yourImage(row1:row2, :, :);
% etc.
end
% Similar for columns.
for k = 1 : length(dividingColumns)
col1 = dividingColumns(k);
col2 = col1 + c;
if col2 >= columns
col2 = columns
end
thisBand = yourImage(:, col1:col2, :);
end
  6 个评论
Mujtaba Tahir
Mujtaba Tahir 2021-7-28
@Image Analyst the last issue i am facing is that my programme is saving only the last segmented image how can i fix it please
% 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;
% Now write output image to the output folder.
baseOutputFileName = baseInputFileName;
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
fprintf('Now writing "%s"\n', fullOutputFileName);
imwrite(subImage, fullOutputFileName);
end
end
end
Image Analyst
Image Analyst 2021-7-28
You're not changing the name at all. You're just using the same name for all images you save. To fix it try this:
[~, baseNameNoExt, ext] = fileparts(baseInputFileName);
baseOutputFileName = sprintf('%s, row %2.2d, col %2.2d.png', baseNameNoExt, row, col);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by