I have a directory that contains several folders. Each of these folders contains several images.
If we suppose that we have a directory that contains images, I know how to read these images (see my code below); If we suppose that these images are placed in subdirectories in this directory, then I don't know how to edit my code.
directory = 'my_direct';
dnames1={directory};
c{1} = dir(dnames1{1}); % struct array with 5 fileds (name, isdir,...)
if length(c{1}>0)
if c{1}(1).name =='.'
c{1} = c{1}(3:end); %ignore the '.' and '..'
end
end
for k = 1: length(c{1})
image= double(imread([dnames{1} '/' h{1}(L).name]))./255;
end
assuming that you want to go inside each sub-directory and read the images
Root_directory='The directory location';
sub_directories=dir(Root_directory);
sub_directories(1,2)=[]; % to remove . and ..
for sub_dir_index=1:length(sub_directories)
images=dir(fullfile(Root_directory,sub_directories(sub_dir_index).name));
the rest of your code
end
You can use this code for reading multiple images, all you should know is the path to that directory. Just write the path and the code will read your images according to the pattern you stored them. I hope you will find it useful.
myFolder = 'path';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for i = 1:length(jpegFiles)
baseFileName = jpegFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Input_image = imread(fullFileName);
end
If you have the R2014b release of MATLAB with the Computer Vision System Toolbox, you can use the imageSet object:
imgSets = imageSet('my_direct','recursive')
imgSets will be an array of imageSet objects, each of which contains the paths to all image files in the sub-directories of my_direct. You can then read the j-th image from the i-th sub-directory as follows:
im = read(imgSets(i), j);
Related
I have multiple geopoint data inside of a (1x2) data cell. How can I plot all of this data onto one map within MATLAB?
I have tried to append before but obviously it isn't what I want. I'm really trying to understand the best method.
clear;
clc;
% Specify folder where the files live
myFolder = 'C:\Users\J87662\Desktop\GPX Data Files';
% Check to make sure folder exists. Warns user if doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
theFiles = dir(fullfile(myFolder, '*.gpx')); % Using .gpx as the desired file type.
% Number of files in the folder
n = numel(theFiles);
data = cell(1,n);
for k=1:n
% Read each file
data{k} = gpxread(fullfile( myFolder, theFiles(k).name ));
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
end
When trying to use data(1,1).Latitude, I get the error "Struct contents reference from a non-struct array object."
Indexing into cell arrays requires the use of {} rather than (). Try data{1, 1}.Latitude instead (since you have only a single column, data{1}.Latitude should also work).
I am stuck on something that is supposed to be so simple.
I have a folder, say main_folder with four sub folders, say sub1, sub2, sub3 and sub4 each containing over 100 images. Now am trying to read and store them in an array. I have looked all over the internet and some MATLAB docs:
here, here and even the official doc.
My code is like this:
folder = 'main_folder/**'; %path containing all the training images
dirImage = dir('main_folder/**/*.jpg');%rdir(fullfile(folder,'*.jpg')); %reading the contents of directory
numData = size(dirImage,1); %no. of samples
arrayImage = zeros(numData, 133183); % zeros matrix for storing the extracted features from images
for i=1:numData
ifile = dirImage(i).name;
% ifolder = dirImage(i).folder;
I=imread([folder, '/', ifile]); %%%% read the image %%%%%
I=imresize(I,[128 128]);
...
If I try the code in the above snippet, the images are not read.
But if I replace the first two lines with something like:
folder = 'main_folder/'; %path containing all the training images
dirImage = dir('main_folder/sub1/*.jpg'); %rdir(fullfile(folder,'*.jpg'));
then all images in sub1 are read. How can I fix this? Any help will be highly appreciated. I want to read all the images in the four sub folders at once.
I am using MATLAB R2015a.
I believe you will need to use genpath to get all sub-folders, and then loop through each of them, like:
dirs = genpath('main_folder/'); % all folders recursively
dirs = regexp(dirs, pathsep, 'split'); % split into cellstr
for i = 1:numel(dirs)
dirImage = dir([dirs{i} '/*.jpg']); % jpg in one sub-folder
for j = 1:numel(dirImage)
img = imread([dirs{i} '/' dirImage(j).name]);
% process img using your code
end
end
I have certain images in a directory and I want to load all those images to do some processing. I tried using the load function.
imagefiles = dir('F:\SIFT_Yantao\demo-data\*.jpg');
nfiles = length(imagefiles); % Number of files found
for i=1:nfiles
currentfilename=imagefiles(i).name;
I2 = imread(currentfilename);
[pathstr, name, ext] = fileparts(currentfilename);
textfilename = [name '.mat'];
fulltxtfilename = [pathstr textfilename];
load(fulltxtfilename);
descr2 = des2;
frames2 = loc2;
do_match(I1, descr1, frames1, I2, descr2, frames2) ;
end
I am getting an error as unable to read xyz.jpg no such file or directory found, where xyz is my first image in that directory.
I also want to load all formats of images from the directory instead of just jpg...how can i do that?
You can easily load multiple images with same type as follows:
function Seq = loadImages(imgPath, imgType)
%imgPath = 'path/to/images/folder/';
%imgType = '*.png'; % change based on image type
images = dir([imgPath imgType]);
N = length(images);
% check images
if( ~exist(imgPath, 'dir') || N<1 )
display('Directory not found or no matching images found.');
end
% preallocate cell
Seq{N,1} = []
for idx = 1:N
Seq{d} = imread([imgPath images(idx).name]);
end
end
I believe you want the imread function, not load. See the documentation.
The full path (inc. directory) is not held in imgfiles.name, just the file name, so it can't find the file because you haven't told it where to look. If you don't want to change directories, use fullfile again when reading the file.
You're also using the wrong function for reading the images - try imread.
Other notes: it's best not to use i for variables, and your loop is overwriting I2 at every step, so you will end up with only one image, not four.
You can use the imageSet object in the Computer Vision System Toolbox. It loads image file names from a given directory, and gives you the ability to read the images sequentially. It also gives you the option to recurse into subdirectories.
So I just started with image processing/computer vision in MATLAB.
So my first task is to convert a series of images(frames) into a video. So I went through online sources (MATLAB website more specifically) to get a way to do it.
So the one that I implemented is http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html which solved the problem for me.
However, when I play it, the video seems jumpy in some places. Like it would bring a different frame in the middle and make the whole video jumpy for that split second. It happens a couple of places in the video.
Any anyone knows why this happens?
Thanks
PS below is the code I use:
myFolder = 'C:\Users\owner\Desktop\MATLAB GUI\Color\Color'; %Specify Directory
filePattern = fullfile(myFolder, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
size = length(jpegFiles); % length of the size of the file
outputVideo = VideoWriter(fullfile(myFolder,'video1.avi'));
outputVideo.FrameRate = 30;
open(outputVideo);
for i = 1:length(jpegFiles) %load all the files in the directory
j = i; %%accumulating the number of jpegfiles into handles.j
baseFileName = jpegFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName); %filename of image
imageArray = imread(fullFileName); %image being read
%imageArray = rgb2gray(imageArray);
imagecell{i} = imageArray; %storing the images in imagecells
writeVideo(outputVideo,imagecell{i});
end
close(outputVideo);
video1 = VideoReader(fullfile(myFolder,'video1.avi'));
mov(video1.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:video1.NumberOfFrames
mov(ii) = im2frame(read(video1,ii));
end
set(gcf,'position', [150 150 video1.Width video1.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 video1.Width video1.Height])
image(mov(1).cdata,'Parent',gca);
axis off;
movie(mov,1,video1.FrameRate);
Given that there may be too many files to be renamed (padded with zeros) here is a quick function that will do it for you: you just need to provide the directory/folder where the images are stored, the padding (if less 100 files, then padding can be 2; if less than 1000 files, then padding can be 3; etc.), and a common pattern. The code assumes that there is a common pattern in each file (like 'frame' or 'image') that when removed, leaves just the number:
renameFiles(directory,padSize,fileNamePattern)
filePattern = fullfile(directory, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
for k=1:size(jpegFiles)
% get the source file that will be moved/renamed
fileSrc = jpegFiles(k).name;
% get the parts of the file
[path,name,ext] = fileparts(fileSrc);
% where does the pattern fit in the name?
idx = strfind(name,fileNamePattern);
% remove the pattern from the name
if idx==0
% pattern does not exist in file so skip it
continue;
elseif idx==1
% pattern is at the beginning of name so remove it
frameNumberStr = name(length(fileNamePattern)+1:end);
else
% pattern is at the end of name so remove it
frameNumberStr = name(1:idx-1);
end
% get the number of digits
numDigits = length(frameNumberStr);
% create the new file name
paddedName = [fileNamePattern repmat('0',1,padSize-numDigits) frameNumberStr];
fprintf('%s\n',paddedName);
% only move if the destination is different
if strcmp(paddedName,name) ~= 1
% set the destination file
fileDest = fullfile(directory,[paddedName ext]);
% move the file
movefile(fileSrc, fileDest,'f');
end
end
end
An example - if all files have the common pattern of 'frame' and there are less than 1000 files, then run this function as
rename(pwd,3,'frame')
All files that were named as frame1.jpg or frame99.jpg are now named as frame001.jpg and frame099.jpg.
Hope this helps!
If you have the Computer Vision System Toolbox you can use the vision.VideoFileWriter object. You can simply feed images into its step() method one at a time, and they will be written to the video file as video frames. Note that the images must all be the same size and have the same data type.
i know how to read all the image files from a folder which are of the same type, e.g TIFF. My question: is there any way to read all kinds of image files in a folder? The images can be TIFF, BMP, JPEG. It may also happen that at a given point only JPEGs are present in the folder or maybe all three types are there.
Thanks
the function imread can read most of the existing standard image types.
So, what you can do is
cd( folder );
fls = dir( '*' ); % list ALL files
ii = 1;
imgs = {};
for fi=1:numel(fls)
if fls(fi).isdir, continue; end; % skip sub directories
try
tmp = imread( fls(fi).name );
imgs{ii} = tmp; % got an image
ii = ii+1;
catch em
% not an image - ignore
end
end