How can I resize image for self-training

4 次查看(过去 30 天)
Good day, I am novice with matlab. I am also novice with openmpi (cluster). We have some nodes on a cluster with Matlab and my boss asked to investigate my self to explain how it works :o| :o)....
My first goal is the following: With matlab on my personnal PC, 1) I would like to resize about 100 high resolution images from a source folder to a target foler. 2) I would like to count the time for that job.
My second goal is to move the matlab script to my cluster and to see hoe long does it take to resize 100 image, with one node, then four node (etc), ..and to learn about openmpi and matlab.
I would appreciate if you can help me with my first goal. Would you know a tutorial, a How to or example of code to 1) To read the images into a folder 1) To resize and copy images from a source folder and a target folder (the quality is not matter)
Many thank for any suggetsion and help

回答(4 个)

KSSV
KSSV 2017-7-10
To read image check imread.
To resize image use imresize
You need to run a loop for each image to read and resize, for that check: https://in.mathworks.com/matlabcentral/answers/385-how-to-read-images-in-a-folder .There are many links for this, this is one among them.
To check the time elapsed or time taken, doc tic, toc , timeit

Pierrot10
Pierrot10 2017-7-10
Thnk for your reply. I tried the following code
files = dir('source');
for m = 1:numel(files)
if strcmp(files(m).name(end-3:end),'.jpg')
x = imread(file(m).name,'jpg');
% process x
end
end
with the following error message Subscript indices must either be real positive integers or logicals.
Error in imgresize (line 5)
if strcmp(files(m).name(end-3:end),'.jpg')
I have to images in my folder 'source'. Then m should be positif? Why do I have s such error message?
  1 个评论
Walter Roberson
Walter Roberson 2017-7-10
Accessing
files(m).name(end-3:end)
assumes that end-3 is a valid index into files(m).name . That is not true if name is less than 4 characters long, such as if you are examining the '.' or '..' entries.
files = dir('source');
files([files.isdir]) = []; %get rid of . and .. and all folders
for m = 1:numel(files)
thisfile = files(m).name;
[~, ~, ext] = fileparts(thisfile);
if strcmp(ext, '.jpg')
x = imread(thisfile, 'jpg');
% process x
end
end
Or you could just
files = dir(fullfile('source', '*.jpg');
files([files.isdir]) = []; %get rid of weirdly named folders
for m = 1 : numel(files)
thisfile = files(m).name;
x = imread(thisfile, 'jpg');
% process x
end
This works without a test because the dir() already restricted to .jpg files

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-7-10
projectdir = 'source';
files = dir(fullfile(projectdir, '*.jpg'));
files([files.isdir]) = []; %get rid of weirdly named folders
for m = 1 : numel(files)
thisfile = fullfile(projectdir, files(m).name );
x = imread(thisfile);
% process x
end

Pierrot10
Pierrot10 2017-7-10
Well, I finally could move and copy all image into the 'source' folder. That great. Now I am trying to resize the image as it is wrote here: imresize
here is my code
% Start script counter
source_folder = 'source';
target_folder = 'target';
original_folder = 'original';
files = dir(fullfile(source_folder, '*.jpg'));
files([files.isdir]) = []; %get rid of weirdly named folders
for m = 1 : numel(files)
thisfile = files(m).name;
%%Keep a copy of the original image
%copyfile(fullfile(source_folder, thisfile), original_folder);
%%Move the image
%movefile(fullfile(source_folder, thisfile), target_folder);
%%resize it
X = imread(fullfile(source_folder, thisfile));
J = imresize(X, 0.5);
figure, imshowpair(X,J,'montage')
axis off
end
% End script counter
I do not understand why imresize generate the following error:
Attempt to execute SCRIPT imresize as a function:
/Users/pierrot/Documents/MATLAB/imresize.m
Error in imgresize (line 24)
J = imresize(X, 0.5);
Should enclose all my code into a function? It attend to run imresize as a function, which is look nrmal as imresize is function. So I am confused. Finnaly, I am wundering if X concider the folder 'source_folder'. I wunder if imresize is looking at image which is at the same level of the script file, and not looking into 'source_folder'?
  3 个评论
Pierrot10
Pierrot10 2017-7-10
That a lot for your reply. I solved that problem. The last problem I have, the image is not resized.
In facte, the image is display with the new size if I add the following
X = imread(fullfile(source_folder, thisfile));
J = imresize(X, 0.5);
figure, imshowpair(X,J,'montage')
axis off
If I comment figure, imshowpair(X,J,'montage') nothing is shown and the image in source folder kept the same size :o)
Is there a command to write J into a folder? Many thank!
Pierrot10
Pierrot10 2017-7-10
Ok sorry I found the answer wirt imwrite
% Start script counter
source_folder = 'source';
target_folder = 'target';
original_folder = 'original';
files = dir(fullfile(source_folder, '*.jpg'));
files([files.isdir]) = []; %get rid of weirdly named folders
for m = 1 : numel(files)
thisfile = files(m).name;
%%Keep a copy of the original image
%copyfile(fullfile(source_folder, thisfile), original_folder);
%%Move the image
%movefile(fullfile(source_folder, thisfile), target_folder);
%%resize it
X = imread(fullfile(source_folder, thisfile));
J = imresize(X, 0.5);
imwrite(J, fullfile(target_folder, thisfile));
% figure, imshowpair(X,J,'montage')
% axis off
end
% End script counter
Thank for your help

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by