From: Uyen Pham on
Hi everyone,
I am a newbie in Matlab as well as Image processing field. I have 2 problems which may need your helps:
1. As I read in some document, there are about 4 image formats supported by Matlab. These are: index, grayscale, binary, RGB. And there are also many kinds of images, such as: *.jpeg, *.bmp, *.gif, *.tiff. I can figure out the differences between 2 definitions?

2. Another problem is about function in Matlab. I am writing a tool to convert the image format ( eg: convert from *.jpeg to *.bmp). I draw a list box in order for the users to choose which format they want to convert from and to. However, how can I get the value of their choice?
Here is my code, but it seems to have something wrong:
(format1 and format2 are 2 buttons)

% Read the input image
old_format = get(handles.format1, 'String');
[filename path] = uigetfile(old_format,'Open');
if (filename == 0)
return
end

% Show the image on the axes
processing_image = imread (fullfile(path, filename));
axes(handles.axes1);
image(processing_image);
handles.img = processing_image;
guidata(hObject, handles);

% Image converting
save processing_image;
new_format = get(handles.format2, 'String');
if (strcmp('bmp',new_format))
imwrite(processing_image, 'filename.bmp')
elseif (strcmp('hdf',new_format))
imwrite(processing_image, 'filename.hdf')
elseif (strcmp('jpg',new_format))
imwrite(processing_image, 'filename.jpg')
elseif (strcmp('pcx',new_format))
imwrite(processing_image, 'filename.pcx')
elseif (strcmp('tiff',new_format))
imwrite(processing_image, 'filename.tiff')
elseif (strcmp('xcb',new_format))
imwrite(processing_image, 'filename.xcb')
else (strcmp('gif',new_format))
imwrite(processing_image, 'filename.gif')
end

3. How can I change the directory to copy the file into? If I have a pop-up menu for the user to choose the output directory?

Thank you very much for your help. I am waiting for your reply.
From: Walter Roberson on
Uyen Pham wrote:

> I am a newbie in Matlab as well as Image processing field. I have 2
> problems which may need your helps:
> 1. As I read in some document, there are about 4 image formats supported
> by Matlab. These are: index, grayscale, binary, RGB. And there are also
> many kinds of images, such as: *.jpeg, *.bmp, *.gif, *.tiff. I can
> figure out the differences between 2 definitions?

JPEG and so on are formats for storing images on disk, often in
compressed format, and often which create approximations of the original
image (e.g., skipping detail that would be unnoticeable to most humans.)

indexed, grayscale, truecolor (RGB), and binary, are the ways that
Matlab supports for storing and manipulating images in memory, and for
displaying images stored in memory. An image stored in memory is stored
in full, not compressed, and is considered to be have all available
detail in it.

Limiting the number of ways an image can be stored in memory reduces the
number of different tools that have to be written to work with images.

There is also the issue that if Matlab stored memory images in one of
the file formats that approximates images, then when the code changed
part of the image, the approximation process could throw away the change
or alter nearby pixels, and would be continually re-doing the
approximation as the code continued to change the image: the result
might not look _anything_ like the code intended, and the results would
depend upon the order of operations. By only storing exact (not
approximate) memory formats, no approximation would be done until the
code indicated it was finished changing the image and indicated what
level of approximation was acceptable in storing the changed image to disk.

> [filename path] = uigetfile(old_format,'Open');

> if (strcmp('bmp',new_format))
> imwrite(processing_image, 'filename.bmp')

imwrite(processing_image, [filename '.bmp'])

That is, _construct_ the new filename by adding '.bmp' on to the end of
the current contents of the variable "filename".

Warning: this will not actually work the way you want unless you have
altered the original filename by removing its existing suffix. You can
get the name without the suffix by using fileparts()

> 3. How can I change the directory to copy the file into? If I have a
> pop-up menu for the user to choose the output directory?

Consider using uiputfile() to allow the user to select the new file name
and directory.

If you do not wish to do that, then use uigetdir() to get the new
directory, and then use fullfilename() to put together the directory and
the base filename and the suffix into a complete output file name.
 | 
Pages: 1
Prev: Matpower
Next: dynamic matrix variables in loop