tags:

views:

1824

answers:

5

In my previous question I asked about Blind Deconvolution in Matlab.

Now I have a new problem where I know how imread and imwrite works but I don't know where do the function call the image file from? in other words where do I store the image in order to call it using imread?

A: 

cd displays the current working directory. Place your files in there.

Alternately, change the directory to the one you want first:

www.mathworks.com/access/helpdesk/help/techdoc/ref/cd.html

Adam Davis
thanks adam, but does it change the directory permanently or just as long as the work is concern only?
noob88
It changes the directory for all scripts and commands run after the change.
Adam Davis
oh, buti don't intend to do that..i thought there's a default folder where images are placed, where i can do the same and call using imread it'd be easier to me in the future as well..
noob88
+1  A: 
Dima
+4  A: 

As Adam suggests you could change the Matlab working directory to the location of your images or what I tend to do is to get the user to select the file to be read using uigetfile

>> [fn,pn]=uigetfile({'*.TIFF,*.jpg,*.bmp','Image files'}, 'Select an image');
>> I = imread(fullfile(pn,fn));

or if you know the directory of to the images you want to read you could store it in a variable then you could get a list of images in that directory using dir

>> imageDir = 'c:\path\to\my\images';
>> imageList = dir(fullfile(imageDir,'*.tif')); % store all files with extension tif  
                                               % in a structure array imageList

from there you can loop through imageList and process each image found. Finally, you can use uigetdir to ask the user for the direcotory containing the image set.

Azim
+1 for introducing me to uigetfile() :)
kigurai
@kigurai: then you should also look at uigetdir as well.
Azim
+1  A: 

Other responses have discussed the idea that matlab will look in the current directory for any files to be loaded.

If you are unsure what the current directory is, at the command prompt, type

pwd

This will return the current directory. The command cd, when used with no arguments, will also display the current directory name. cd can also help you to move to a different directory when that is appropriate. And of course, if you are unsure of the names of those files in a directory, dir will help you there.

If you prefer to use a directory stack, pushd and popd are nice functions to download.

http://www.mathworks.com/matlabcentral/fileexchange/8103

If matlab does not find the file in question, and you have not specified an absolute path, perhaps generated by uigetfile, then it will look at other directories on your search path. So another idea is, if you frequently want to load files from a specific directory, you can just add that directory to your search path. addpath, rmpath, savepath, and pathtool will all be useful tools here.

woodchips
+1 for the reference to the path tools in MATLAB
Azim
+2  A: 

You can pass both absolute and relative file paths to IMREAD and IMWRITE. For example, an absolute path would be:

filePath = 'C:\mywork\matlab\images\picture1.jpg';

If you have already changed the current working directory in MATLAB to "C:\mywork\matlab", then a relative path (relative to the current directory) would be:

filePath = 'images\picture1.jpg';

Also, one additional thing to consider is how well your path strings will work on different platforms. For example, a Windows relative path would be:

filePath = 'toolbox\matlab\iofun';

But a UNIX relative path would be:

filePath = 'toolbox/matlab/iofun';

Notice one uses a backslash and one uses a forward slash. To ensure that your code will work across multiple platforms, I would check out these MATLAB functions for dealing with file paths (to name just a few): FULLFILE, FILEPARTS, FILESEP.

gnovice