現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)