Editing images using MATLAB
5 views (last 30 days)
Show older comments
Hello
I have file with 100 pictures inside it , i want to split or divide every picture into 5*5 or 3*3 new pieces and save this new parts as new pictures into other file with new name as sequences name
any help
4 Comments
Jan
on 18 Nov 2022
The mainpart of the code has no relation to image processing, but it concerns the reading and writing of files.
Even if you do not have much experiences with programming in Matlab, you have much more experiences with your specific problem than the members of the forum have. :-)
Accepted Answer
Image Analyst
on 18 Nov 2022
Edited: Image Analyst
on 18 Nov 2022
OK, I sense you're having some trouble with this so I did it for you.
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
n = 3; % Number of partitions we'll divide the image up into.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(imageArray);
subplot(5, 5, 1)
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
startRows = round(linspace(1, rows+1, n+1))
startCols = round(linspace(1, columns+1, n+1))
plotLocation = 2;
for rowk = 1 : n
row1 = startRows(rowk);
row2 = startRows(rowk + 1) - 1;
for colk = 1 : n
col1 = startCols(colk);
col2 = startCols(colk + 1) - 1;
subImage = imageArray(row1:row2, col1:col2, :);
subplot(n, n+1, plotLocation);
imshow(subImage)
plotLocation = plotLocation + 1;
% Create a filename for this subimage
baseFileName2 = sprintf('%s Row %d Col %d.png', baseFileName, rowk, colk);
fullFileName2 = fullfile(theFiles(k).folder, baseFileName2);
fprintf('Saving %s\n', fullFileName2);
imwrite(subImage, fullFileName2);
end
end
drawnow;
end
5 Comments
Image Analyst
on 23 Nov 2022
OK I didn't know since you didn't Accept the answer. Since it did work can you please click the "Accept this answer" link? Thanks in advance. 🙂
To set all green in a picture to black you can use the Color Thresholder app on the Apps tab of the tool ribbon. Do it in HSV color space and then tell it to export the function.
More Answers (1)
Image Analyst
on 18 Nov 2022
I assume you tried to adapt the FAQ:
and must have run into some difficulties. What happened? Are your images a multiple of 3 and 5?
0 Comments
See Also
Categories
Find more on Convert Image Type 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!