How to read image data sets from a folder at once ?

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

 採用された回答

Jan
Jan 2017 年 3 月 26 日
編集済み: Jan 2017 年 3 月 27 日

2 投票

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 件のコメント

Tousif Ahmed
Tousif Ahmed 2017 年 3 月 27 日
Great Simon!! it works. One last help can u please tel me how to save the individual converted file in a folder
Jan
Jan 2017 年 3 月 27 日
See [EDITED]
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 27 日
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

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2017 年 3 月 26 日

0 投票

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 件のコメント

Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
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
KSSV
KSSV 2017 年 3 月 26 日
Why it will not read? Show the code and error.
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
編集済み: Jan 2017 年 3 月 26 日
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
KSSV
KSSV 2017 年 3 月 26 日
What's the error it shall work...
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
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
KSSV
KSSV 2017 年 3 月 26 日
You copy the m file in the folder where your images are.
Jan
Jan 2017 年 3 月 26 日
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
Tousif Ahmed 2017 年 3 月 26 日
No bro its not working
Jan
Jan 2017 年 3 月 26 日
編集済み: Jan 2017 年 3 月 26 日
Then please explain the problem with more details. What exactly does "not working" mean? I do not understand this sentence:
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
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
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
Jan 2017 年 3 月 26 日
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
Tousif Ahmed 2017 年 3 月 26 日
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
Tousif Ahmed 2017 年 3 月 26 日
I need to convert images from rgb to gray and resizing of the converted images to 112x92 but i am unable to do that
Jan
Jan 2017 年 3 月 26 日
編集済み: Jan 2017 年 3 月 26 日
Sorry, I do not know image files which contain image files. I ask the last time for a clarification, then I give up. For you it is obvious and trivial what "contain in a set" means, for the readers it is a mysterium.
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
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
Jan 2017 年 3 月 26 日
Okay. And when you use the method suggested by KSSV, what happens? Why does tis not satisfy you?
Tousif Ahmed
Tousif Ahmed 2017 年 3 月 26 日
it will not display images or reads them
Jan
Jan 2017 年 3 月 26 日
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
Tousif Ahmed 2017 年 3 月 26 日
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
Jan 2017 年 3 月 26 日
編集済み: Jan 2017 年 3 月 26 日

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeRead, Write, and Modify Image についてさらに検索

質問済み:

2017 年 3 月 26 日

コメント済み:

2017 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by