You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to read image data sets from a folder at once ?
5 views (last 30 days)
Show older comments
Hello. I have 40 datasets in a folder in C drive. I need to convert those files from RGB to grayscale and should resize it but i am unable to read the file and cant convert all the files from RGB to gray at once and cant resize all the images at once and should save the converted and resized images. Can anyone help me with the coding of that please
Accepted Answer
Jan
on 26 Mar 2017
Edited: Jan
on 27 Mar 2017
OutputFolder = 'C:\Temp'; % Set as needed [EDITED]
dinfo = dir('*.jpg');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name;
Img = imread(thisimage);
Y = imshow(Img);
Gray = rgb2gray(Img);
GrayS = imresize(Gray, [112, 92], 'bilinear');
imwrite(GrayS, fullfile(OutputFolder, thisimage)); % [EDITED]
end
Or any other method for the resizing, see: doc imresize.
[EDITED] See the changed code for writing the changed file. Perhaps you want to modify the file name. Then you can split the file extension and the name by fileparts.
3 Comments
Tousif Ahmed
on 27 Mar 2017
Great Simon!! it works. One last help can u please tel me how to save the individual converted file in a folder
Tousif Ahmed
on 27 Mar 2017
Wow!! awesome Simon thanks a lot,
See if You can help me with this code here, I need to add green rectangular box for the face detection on every person. I need to compare for the accuracy of the face detector like the percentage of the faces which are matching. Here is the code
close all; clear all; clc; %% Simple Face Recognition Example % Copyright 2014-2015 The MathWorks, Inc. %% Load Image Information from ATT Face Database Directory faceDatabase = imageSet('Demo_ds','recursive');
%% Display Montage of First Face figure; montage(faceDatabase(2).ImageLocation); %montage displays multiple images title('Images of Single Face'); %% Display Query Image and Database Side-Side personToQuery = 2; % taken from the data set depending on the numbers for first person image given as 1, for 2nd person image given as 2 and so on galleryImage = read(faceDatabase(personToQuery),1);% in the taken data set of a person out of 10, the number represents that particular image in the respected dataset figure; for i=1:size(faceDatabase,2)% starting from 1 to 40 datasets,here taken 2nd peson dataset imageList(i) = faceDatabase(i).ImageLocation(2); %checking for the images from 1 to 40, for 2nd location end subplot(1,2,1);imshow(galleryImage); subplot(1,2,2);montage(imageList); diff = zeros(1,9);
%% Split Database into Training & Test Sets [training,test] = partition(faceDatabase,[0.8 0.2]);
%% Extract and display Histogram of Oriented Gradient Features for single face person = 5; [hogFeature, visualization]= ... extractHOGFeatures(read(training(person),1)); figure; subplot(2,1,1);imshow(read(training(person),1));title('Input Face'); subplot(2,1,2);plot(visualization);title('HoG Feature');
%% Extract HOG Features for training set trainingFeatures = zeros(size(training,2)*training(1).Count,167796); featureCount = 1; for i=1:size(training,2) for j = 1:training(i).Count trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j)); trainingLabel{featureCount} = training(i).Description; featureCount = featureCount + 1; end personIndex{i} = training(i).Description; end
%% Create 40 class classifier using fitcecoc faceClassifier = fitcecoc(trainingFeatures,trainingLabel);
%% Test Images from Test Set person = 6; queryImage = read(test(person),1); queryFeatures = extractHOGFeatures(queryImage); personLabel = predict(faceClassifier,queryFeatures); % Map back to training set to find identity booleanIndex = strcmp(personLabel, personIndex); integerIndex = find(booleanIndex); subplot(1,2,1);imshow(queryImage);title('Query Face'); subplot(1,2,2);imshow(read(training(integerIndex),1));title('Matched Class');
%% Test First 5 People from Test Set figure; figureNum = 1; for person=1:10 for j = 1:test(person).Count queryImage = read(test(person),j); queryFeatures = extractHOGFeatures(queryImage); personLabel = predict(faceClassifier,queryFeatures); % Map back to training set to find identity booleanIndex = strcmp(personLabel, personIndex); integerIndex = find(booleanIndex); subplot(2,2,figureNum);imshow(imresize(queryImage,3));title('Query Face'); subplot(2,2,figureNum+1);imshow(imresize(read(training(integerIndex),1),3));title('Matched Class'); figureNum = figureNum+2; end figure; figureNum = 1;
end
Link for the data set of the image is here http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html The one which is 4.5 MB file is the data set containing 40 sets Thank You
More Answers (1)
KSSV
on 26 Mar 2017
dinfo = dir('*.jpg');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name; %just the name of the image
%read the image
%do something with the image
end
20 Comments
Tousif Ahmed
on 26 Mar 2017
Thank you for replying, but the images are nor being read and images are in JPG extension, imread is not working as it is in folder and all images at once are not being read
Tousif Ahmed
on 26 Mar 2017
Edited: Jan
on 26 Mar 2017
dinfo = dir('*.jpg');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name; %just the name of the image
I=imread(thisimage);%read the image
Y=rgb2gray(I)
imshow(Y);
end
I am new to matlab as i dont know much about the code, please tel me wat changes do i have to make
Tousif Ahmed
on 26 Mar 2017
In this do i have to give any directory of the file location? The file is in C folder and there are 40 data sets each with different folders but in the same folder from s01 to s40
Jan
on 26 Mar 2017
Perhaps you want:
folder = 'C:\Temp\'; % Set accordingly
dinfo = dir(fullfile(folder, '*.jpg'));
or with modern Matlab versions:
folder = 'C:\Temp\'; % Set accordingly
dinfo = dir(fullfile(folder, '**', '*.jpg'));
to include subfolders.
Tousif Ahmed
on 26 Mar 2017
I have 40 data sets from S01 to S40, each set containing 10 images. I need to read the image and convert from RGB to Grayscale along with resizing it all at once. Please help me out with the code
Jan
on 26 Mar 2017
I cannot guess what "data sets" mean and the description "from S01 to S40" does not help. Please remember that the readers do not have the faintest idea about what you are doing.
Is a "data set" a folder? What does "it is not working" mean explicitely? Perhaps you use an older Matlab version, which does not support searching recursively with the "\**\" method. But I cannot guess this.
Please give us a chance to help you by providing the details.
Tousif Ahmed
on 26 Mar 2017
Data sets are image files of a person with different expressions containing 10 images of a person in a set, and there are 40 data sets of different persons.
Tousif Ahmed
on 26 Mar 2017
I need to convert images from rgb to gray and resizing of the converted images to 112x92 but i am unable to do that
Tousif Ahmed
on 26 Mar 2017
in a file i have 400 images all in RGB, i need to convert them to GRAY scale all at once using 'for loop'. i think this is clear hope this u can understand
Jan
on 26 Mar 2017
Okay. And when you use the method suggested by KSSV, what happens? Why does tis not satisfy you?
Jan
on 26 Mar 2017
It is less useful if you only explain, what the code does not do. Better explain, what happens instead of your needs.
Start with reading the images:
dinfo = dir('*.jpg');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name; %just the name of the image
%read the image
Img = imread(thisimage);
end
Does it work? If yes, insert some code to display the image. If no, please explain what happens instead.
Tousif Ahmed
on 26 Mar 2017
dinfo = dir('*.jpg');% image extension for K = 1 : length(dinfo) thisimage = dinfo(K).name; I=imread(thisimage); Y=imshow(I) %just the name of the image %read the image
end This code is able to read images thankfully, Unable to convert the images from RGB to gray scale
Jan
on 26 Mar 2017
Edited: Jan
on 26 Mar 2017
I proceed with an own answer. Please read https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099.
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)