I have a folder of binary images.I want to assign each image a variable so that it would be easy to extract features for the images.can someone please help me out?

myFolder = 'e:\\invertedimages';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.bmp');
bmpFiles = dir(filePattern);
for k = 1:length(bmpFiles)
baseFileName = bmpFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end

7 Comments

sir,this code helps to load all the images in one variable.but i need the code to create variable to each image using for loop and arrays concept
i had tried this out. error:dynamically naming variable for images.
Attach your code. You obviously did something wrong - not what I told you. Here is the code that I told you to run and it works fine:
myFolder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.tif');
bmpFiles = dir(filePattern);
for k = 1:length(bmpFiles)
baseFileName = bmpFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
Copy and paste that, then run it. Then compare it to yours and tell me what you did differently.
yes sir the code you said works fine but you said to update imageArray to imageArray{k} to allocate variables to multiple images this doesnt work
sir in the above code
if true
baseFileName = bmpFiles(k).name;
end
what does .name for name of image or folder in which the image is or a predefined word.
Sorry - I evidently pasted the original code instead of the code I changed and tested. Try this:
myFolder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.tif');
bmpFiles = dir(filePattern);
for k = 1:length(bmpFiles)
baseFileName = bmpFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray{k} = imread(fullFileName);
imshow(imageArray{k}); % Display image.
drawnow; % Force display to update immediately.
end
As to your latest question, bmpFiles is an array of structures. Each structure has several fields, one of which is name. So bmpFiles(k) is the k'th structure in the array and bmpFiles(k).name is the file name of the k'th image.

Sign in to comment.

 Accepted Answer

6 Comments

see i have 1000 images in a folder and i need to assign a variable to each image.help me out with simple code
Apparently he didn't like my suggestion of formatting the code, so I formatted it for him. Now you can see that the code is taken from the FAQ.
Uh, yes it should if you need to create a 4D array for training a neural network.
No, if you need to create a 4D array for training a neural network, then you do not need a separate variable for each image. You can read an image into one variable, validate that the image is the right size and data type, and then store that variable as a slice of the 4D array (a second variable.) The loop index adds a third variable, the filename to read probably adds a 4th variable, for a total of 4 variables, not one variable per image.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!