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 Kommentare

Jan
Jan am 18 Nov. 2022
What have you tried so far? Do you know this already: FAQ: Process sequence of files .
It is hard to guess, where your files are stored (in one folder?), if they are recognized by their names of file extensions, if they are RGB or gray imagaes, how the naming scheme for the output should look like, where you want to store the output files and so on. So please share more details and the parts of the code you have written already.
MUSTAFA
MUSTAFA am 18 Nov. 2022
I did not tried becose my info is bad about image processing
I have pictures RGB with different names stored in one folder , recognized by extensions
and want to split each image into other new images 3*3 or 5*5 but it is sequre dimantion and save these new pic (output) in one new folder with new names that are sequential in new extensions or different it not matter .
Thanks
Jan
Jan am 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. :-)
MUSTAFA
MUSTAFA am 23 Nov. 2022
Yes , I do not have much experiences with programming in Matlab , but my specific problem as below :
i have 15000 ( white rice grain ) and want to take picture for every grain by my camera , and this is hard .
so i am thinked to put 25 grain in one picture and rescale + splite it by matlab before that i must remove background and put black one becouse the rice is white color ...
all that just to use this pictures as dataset for CNN to classifcition rice grains , this is my problem .
Thank you

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 18 Nov. 2022
Bearbeitet: Image Analyst am 18 Nov. 2022

1 Stimme

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 Kommentare

MUSTAFA
MUSTAFA am 18 Nov. 2022
Thank you ..
I will try it
Image Analyst
Image Analyst am 18 Nov. 2022
OK, how did it work out?
MUSTAFA
MUSTAFA am 23 Nov. 2022
Thank you .. it work .
can you help me with add other code to that code as below:-
1- rescale (old or orginal ) photo before split it .
2-I have Rice grain as pictures and all orginal pictures have same background (green color) . so I want to put black color as new background . for sure object must not effect in this change ..
THANKS
Image Analyst
Image Analyst am 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 rescale an image in intensity you can use rescale or imadjust.
To rescale an image spatially you can use imresize.
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.
MUSTAFA
MUSTAFA am 23 Nov. 2022
OK . Thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 18 Nov. 2022

0 Stimmen

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?

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 18 Nov. 2022

Kommentiert:

am 23 Nov. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by