Loading images in a variable..

37 views (last 30 days)
Alex
Alex on 30 Apr 2012
Commented: Walter Roberson on 14 Apr 2019
HEllo, we are working on DIP based signature verification project. We are having problem in loading images in a variable & how to access every image using that variable. Any help would be appreciated.
Regards Alex

Accepted Answer

Junaid
Junaid on 30 Apr 2012
let say your Directory where you image is
myDir = 'images/';
and extension of all images are jpg
ext_img = '*.jpg';
now load images in a;
a = dir([myDir fileExtension]);
nfile = max(size(a)) ; % number of image files
now loop to read the images
for i=1:nfile
my_img(i).img = imread([myDir a(i).name]);
end
Now my_img contains all the images in given directory.
  2 Comments
ayushi
ayushi on 21 Jul 2016
@ junaid if there is a image selected from a location and we want to check whether the image is image 1, and if the image is 1mage1 then imshow(image2) else if image is 1mage2 then open image 4
Walter Roberson
Walter Roberson on 21 Jul 2016
if strcmp(VariableWithSelectedImageName, 'image1')
imshow(image2);
elseif strcmp(VariableWithSelectedImagename, 'image2')
imshow(image4);
end

Sign in to comment.

More Answers (9)

Junaid
Junaid on 30 Apr 2012
to load image in a variable.
a = imread('myimage.jpg');
and you can check ...
imshow(a);
and you can do any operation on a. a contains single image. If you are storing 2-D image (gray scale image) then you can add multiple images on different channel.
For example. I have two 2-D images, a and b.
then c which contains both images.
c (:,:,1) = a;
c (:,:,2) = b;
Make sure a, and b both have same size (dimensions). This is as simple as you can. But there are many other ways like
And if you want to store the array of images. then following code can be usefull.
array(1).img = a;
array(2).img = b;
and so on.

Image Analyst
Image Analyst on 30 Apr 2012
See the FAQ for a variety of ways to load a sequence of files: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Junaid
Junaid on 1 May 2012
There are number of methods to compare two images. Most famous are Histogram based comparison, Entropy based comparison-- these methods captures the global properties of image.
Or now a days local features based comparison is used like SIFT based, SURF based etc.
%%
and if you want to compare two images from variables and you have difficulties to do any process then following example will subtract one image from the second (if both images have same resolution, dimensions).
C = my_img(1).img - my_img(2).img;
now c stores the an other image which is obtained by subtracting the image 1 and image 2.

Junaid
Junaid on 1 May 2012
you share what is your error and also post that in error as new Question.
In given code.
([*.jpg']);
should be this
(['*.jpg']);
and it should be written like this.
a = 'C:\Desktop\sai\*.jpg';
There should not be any semicolon.

Alex
Alex on 30 Apr 2012
Thanks but still facing problem here on how to load a database of 20 cheques. We need to load each cheque leaves separately and compare with other.
Or is there any code for loading the database & accessing each images separately to compare with the input image.

Alex
Alex on 30 Apr 2012
a = dir([myDir fileExtension]);
a = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';([*.jpg']);
Is the right way to express like this? or caould please explain in detail...
Also, when i replace jpg with png it is throwing up errors.
Thanks..

Alex
Alex on 30 Apr 2012
We got it working with this code//
a = 'C:\Desktop\sai\';([*.jpg']);
Another question:
I have two cropped images of a signature from bank cheque. What is the best way to compare the two images. I have loaded the images with your method. My old comparison technique is not working now..
I need a good image comparison technique.
Regards, Alex
  1 Comment
Image Analyst
Image Analyst on 1 May 2012
Use fullfile() and maybe dir instead of whatever mess it is that you have there. Didn't I already refer you to the USC bibliography on signature comparison yesterday?

Sign in to comment.


Alex
Alex on 1 May 2012
Thank you very much junaid.
One more question..
a = 'C:\Desktop\sai\';([*.jpg']);
I am getting an error on this line, can you tell me what is wrong with this?
Thanks much again.
  3 Comments
Alex
Alex on 1 May 2012
Thanks, but still having problem in accessing the image file from the directory.
*1)we can able to perform with the help of this code*
av_files = dir(fullfile(matlabroot, ...
'toolbox/matlab/audiovideo/*.m'));
for i=2:15
av_files(i).name
end
*2)why cant we go with this code? We are not able to access the image file with the variable..*
imagepath='C:\Users\SAITEJ\Desktop\arun\ait_picmatch\ait_picmatch\saitej';
patternname='*.jpg';
imagelist=dir(fullfile(imagepath,patternname));
for i=30:35
imagelist(i).name
end
Image Analyst
Image Analyst on 1 May 2012
Your first code prints out the names of m-files to the command window, while the second code prints out names of JPG files. Why did you choose these limits and do you know that you have that many files. That is not a very robust way. You should do something like
for k = 1 : length(av_files) or for k = 1 : length(imagelist). Other than that I don't see anything off the top of my head why either would not work, unless you just don't have 35 jpg images in the folder, or 15 m-files in the folder. Do you have a specific error? Do the names get printed out to the command window?

Sign in to comment.


Zeeshan Salam
Zeeshan Salam on 14 Apr 2019
can anyone tell me how to store image in I variable that run in that function? i cant run this code due to not load of image in I varibale?
function J = amedfilt2_calc(I) %#eml
% 2-D Adaptive Median Filter
% This filter ignores edge effects and boundary conditions, as such, the
% output is a cropped version of the original image, where the amount
% cropped is equal to the maximum window size vertically and horizontally.
% Define smax as a constant
smax = 9;
% Initialize Output Image (J)
J = I;
% Calculate valid region limits for filter
[nrows ncols] = size(I);
ll = ceil(smax/2);
ul = floor(smax/2);
% Loop over the entire image ignoring edge effects
for rows = ll:nrows-ul
for cols = ll:ncols-ul
window_ind = -ul:ul;
region = I(rows+window_ind,cols+window_ind);
centerpixel = region(ll,ll);
for s = 3:2:smax
% We can collapse the ROI calculations into a single function
[rmin,rmax,rmed] = roi_stats(region,smax,s);
% adapt region size
if rmed > rmin && rmed < rmax
if centerpixel <= rmin || centerpixel >= rmax
J(rows,cols) = rmed;
end
% stop adapting
break;
end
end
end
end
  1 Comment
Walter Roberson
Walter Roberson on 14 Apr 2019
That code assumes that the input array is 2D -- a grayscale image.
If you are using imread() to read a .jpg file, then it is quite unlikely that it is a grayscale image, even if it looks gray. The original JPEG specification did not permit grayscale images, and although it was added afterwards, most software did not bother implementing it. JPEG images that look gray are almost always stored as RGB images.
The general procedure would be something like,
filename = 'SomethingAppropriate.png'; %use appropriate name and extension
img = imread(filename);
if size(img,3) > 1
img = rgb2gray(img);
end
J = medfilt2_calc(img);

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!