Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лекция Images.docx
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
1.43 Mб
Скачать

How to display an image in Matlab

Here are a couple of basic Matlab commands (do not require any tool box) for displaying an image.

Displaying an image given on matrix form

Operation:

Matlab command:

Display an image represented as the matrix X.

imagesc(X)

Adjust the brightness. s is a parameter such that -1<s<0 gives a darker image, 0<s<1 gives a brighter image.

brighten(s)

Change the colors to gray.

colormap(gray)

Sometimes your image may not be displayed in gray scale even though you might have converted it into a gray scale image. You can then use the command colormap(gray) to "force" Matlab to use a gray scale when displaying an image.

If you are using Matlab with an Image processing tool box installed, I recommend you to use the command imshow to display an image.

Displaying an image given on matrix form (with image processing tool box)

Operation:

Matlab command:

Display an image represented as the matrix X.

imshow(X)

Zoom in (using the left and right mouse button).

zoom on

Turn off the zoom function.

zoom off

Exercise

Load your favorite image into Matlab (if it is on any of the format described in the section "Image formats supported by Matlab" above). Now experiment with this image, using the commands given in this worksheet.

A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. The k-th row of the colormap defines the k-th color by specifying the intensity of red, green and blue, i.e., map(k,:) = [r(k) g(k) b(k)]. For example, [0 0 0] is black, [1 1 1] is white, [1 0 0] is pure red, and [.5 1 .83] is aquamarine.

colormap(map) sets the colormap to the matrix map. If any values are outside the interval [0 1], an error message results.

Indexed Color and Palettes

What's an Index? What's a Palette?

This topic of Indexed Color is NOT unique to GIF.   TIF and PNG files can save Indexed color too, but GIF requires it, GIF has no other mode. We speak of GIF files here, but it could be TIF or PNG too.

File formats like TIF and JPG store a 24 bit RGB value for each of the millions of image pixels. But GIF files only store a 4 or 8 bit index at each pixel, so that the image data is 1/6 or 1/3 the size of 24 bits.

Indexed Color is limited to 256 colors, which can be any 256 from the set of 16.7 million 24 bit colors. Each color used is a 24 bit RGB value. Each such image file contains its own color palette, which is a list of the selected 256 colors (or 16 colors in a smaller palette). Images are called indexed color because the actual image color data for each pixel is the index into this palette. Each pixel's data is a number that specifies one of the palette colors, like maybe "color number 82", where 82 is the index into the palette, the 82nd color in the palette list of colors. We have to go to the palette to see what color is there. The palette is stored in the file with the image.

The index is typically a 4 bit value (16 colors in palette) or 8 bit value (256 colors in palette) for each pixel, the idea being that this is much smaller than storing 24 bits for every pixel. But an 8 bit number can only contain a numerical value of 0 to 255, so only 256 colors can be in the palette of possible colors. The size of most graphics files can be limited to use 16 colors, which only uses 4 bit indexes, making the file smaller yet, half the 8 bit size for the index for each pixel.

The command iminfo can be used to retrieve information about image files: