How to read image data sets from a folder at once ?
6 次查看(过去 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
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
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
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
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
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
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
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
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
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
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
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
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
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
2017-3-26
Okay. And when you use the method suggested by KSSV, what happens? Why does tis not satisfy you?
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
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
2017-3-26
编辑:Jan
2017-3-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modify Image Colors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)