BME185 How to use Matlab
How to use the Matlab help for the image processing toolbox:
How to open an image file:
At the Matlab prompt, type the following:
>> [filename, pathname] = uigetfile('*.*', 'Anyfile'); filename
= [pathname filename];
This will open up a dialog box where you can browse for the file that
you want to open. The only supported types are, *.jpeg, *.jpg, *.bmp, *.png,
*.tiff, and *.bmp.
Once you hit okay, filename will contain the full path to the
file that you want to load.
Then in order to read the file, type:
>> img1 = imread(filename);
Or, if you want to read in the file and the colormap:
>> [img1, map1] = imread(filename);
How to display an image:
Once you've opened the image file you can view it by typing:
>> imshow(img1);
This will use a grayscale colormap (a pixel value of 0 is black, a pixel value
of 255 is white, 128 is a medium gray).
Or, if you have a colormap along with the image:
>> imshow(img1, map1);
Also, if you have a colormap that you prefer, you can use the command:
>> colormap(map);
to set the displayed colormap to anything you like.
Histogram Equalization:
Some images don't have very good values for display. One method of improving
the contrast is to spread the values out over the full range [0, 255]. This
is known as histogram equalization. In Matlab it can be done with the
command:
>> img2 = histeq(img1);
This will attempt to spread the values in img1 to cover 64 different levels.
If you want more levels you can use the command
>> img2 = histeq(img1, n);
where n contains the number of levels that you want to use.
Fourier Transforms:
To find the two-dimensional Fourier transform of an image, use:
>> f_img1 = fft2(img1);
To go from a Fourier representation of an image, back to a normal representation
use the inverse transform:
>> img2 = ifft2(f_img1);
Normally when displaying a Fourier transform, we like to have the origin (zero-frequency)
at the center of the image. This can be accomplished with
>> f2_img1 = fftshift(f_img1);
I believe that applying fftshift twice will revert the image back to normal,
however, in case that doesn't work, Matlab does provide the inverse function
>> f_img1 = ifftshift(f2_img1);
When processing images, make sure you are aware of whether the transform has
been shifted or not. Whenever you use fft() or ifft()
it should be done on an unshifted image. However, a lot of your own
processing will probably expect it to be on a shifted version.