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

24 次查看(过去 30 天)
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
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
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
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
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Modify Image Colors 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by