You are on page 1of 12

Experiment

1

6

6 Digital Image Processing
Objectives:

1. To present some of the commands designed for loading an image ,saving the data
as different data types.
2. to learn how to display an image.
3. To learn the conversion between different image formats.

Software :

The software needed in this experiment is MATLAB software and MATLAB help.

Readings :

For further reference on image handling in MATLAB you are recommended to use
MATLAB's help browser.

Theory:

A digital image is composed of pixels which can be thought of as small dots on the
screen. A typical size of an image is 512-by-512 pixels. As you see, it is convenient to let
the dimensions of the image to be a power of 2. For example, 29 =512. In the general case
we say that an image is of size m-by-n if it is composed of m pixels in the vertical
direction and n pixels in the horizontal direction.
Locations in an image can be expressed in Pixel Coordinates ,which is the most
convenient method for expressing locations in an image, In this coordinate system, the
image is treated as a grid of discrete elements, ordered from top to bottom and left to
right, as illustrated by the fig.(1-1).

There is a one-to-one correspondence between pixel coordinates and the coordinates MATLAB uses for matrix subscripting. The first matrix has the same size as the image and one number for each pixel. Binary image: Here each pixel assumes one of only two discrete values: 1 or 0.An indexed image stores an image as two matrices. A binary image is stored as a logical array. This assigns a floating number ("a number with decimals") between 0 and 1 to each pixel.15) returns the value of the pixel at row 2. The value 0 . Indexed image: This is a practical way of representing color images. 2. Intensity image (gray scale image): It represents an image as a matrix where every element has a value corresponding to how bright/dark the pixel at the corresponding position should be colored. The numbers in the first matrix is an instruction of what number to use in the color map matrix. For example. There are two ways to represent the number that represents the brightness of the pixel: The double class (or data type). column 15 of the image IM. The other class is called uint8 which assigns an integer between 0 and 255 to represent the brightness of a pixel. Fig. the data for the pixel in the fifth row. the MATLAB code: IM(2. You use normal MATLAB matrix subscripting to access values of individual pixels.2). For example. second column is stored in the matrix element (5. The second matrix is called the color map and its size may be different from the image. This correspondence makes the relationship between an image's data matrix and the way the image is displayed easy to understand. The Image Processing Toolbox defines four basic types of images: 1. 3.(1-1) Pixel coordinates are integer values and range between 1 and the length of the row or column. The value 0 corresponds to black and the value 1 corresponds to white.

You might need to convert an image from one type to another. Gray2ind() Convert between indexed format to intensity format. green(G) or blue(B). It represents an image with three matrices of sizes matching the image format. Image format conversion (Within the parenthesis you have to type the name of the image you wish to convert. Ind2rgb() Convert a regular matrix to intensity format by scaling. Each matrix corresponds to one of the colors red(R). RGB image(true color): This is another format for color images. The following table shows how to convert between the different formats given above. corresponds to black and 255 to white. The class uint8 only requires roughly 1/8 of the storage compared to the class double. Rgb2gray() Convert between RGB format to indexed format. and display an image. read image data from a file. MATLAB simply applies the filter to the indices in the indexed image matrix. you must first convert it to true color format. For example. Dither() Convert between intensity format to indexed format. MATLAB filters the intensity values in the image. On the other hand. and the results might not be meaningful. and write image data to a file. many mathematical functions can only be applied to the double class. If you attempt to filter the indexed image. as is appropriate. we have to use standard graphics file format as shown below: . Mat2gray() Convert between RGB format to intensity format. if you want to filter a color image that is stored as an indexed image. Rgb2ind() Table (1-1) to get information about the contents of a graphics file. and gives an instruction of how much of each of these colors a certain pixel should use. When you apply the filter to the true color image. Ind2gray() Convert between indexed format to RGB format.) MATLAB Operation: command: Convert between intensity to binary format and indexed to RGB format. 4.

jpg) or info=imfinfo (L1_1. Type: help imfinfo and read the function for this command 2. in any of the supported bit depths.jpg) you will get the following: >> imfinfo('L1_1.JPG') ans = Filename: 'L1_1.JPG' FileModDate: '21-Oct-2005 16:57:42' FileSize: 88836 Format: 'jpg' FormatVersion: '' Width: 800 Height: 600 BitDepth: 24 ColorType: 'truecolor' FormatSignature: '' NumberOfSamples: 3 CodingMethod: 'Huffman' .  Getting information about a graphics file:  The imfinfo function enables you to obtain information about a graphics file and its contents.  Reading image data: The imread function reads an image from any supported graphics image file format.  Writing image data : The imwrite function writes an image to a graphics file in one of the supported formats. Procedure: A.  Displaying an image : The imshow function displays an image that has already been imported into the MATLAB workspace or displays an image stored in a graphics file. READING. DISPLAYING AND WRITING IMAGES 1. Type imfinfo (L1_1.

Type help imread and read the function for this command Note a summary of images types that IMREAD can read: BMP. Let us read the image (L1_3. CodingProcess: 'Sequential' Comment: {} Exercise: Try to get info about the image on your disk 'L1_2. HDF. Title(‘Figure (1)’).jpg) and display it on the screen. 3. TIFF. 5. PCX. type I=imread('L1_2. Imshow(I). Now.tif'). The result will be as displayed in Fig. XWD.tif and record your results. 4. go to the workspace browser where you will find the following figure: Fig(1-2) Exercise: Repeat the previous step with the image ‘L1_1.(1-3) .jpg’and record your results. Once we have the image being read. A simple code can be I=imread(‘L1_3. then to check that the image.jpg).To get information about IMSHOW we type Help imshow . we may depict it on the screen using imshow. 6. JPEG.

bmp’.[20 60]). One can change the image format to another by reading the image in its current format. This can be done using imwrite. displayed and may be processed. figure.jpg').tif’ as bmp file ‘L1_2. Imwrite(I.Exercise: Execute the following commands. figure. record your results. figure. to save ‘L1_2. title('Figure (D)'). imshow(I. title('Figure (B)').bmp’). imshow(I.4). imshow(I. and comment: figure. After the being read. 7. I=imread('L1_3. then to save it into another format. title('Figure (A)'). The option in imread era also available for imwrite. imshow(I). ‘L1_2. you may want to save the results. we may execute the simple code: I= imread(‘L1_2.tif’). For example. .16). title('Figure (C)').

(1-4). shown below. The result will see like that shown in fig. Img1=imread(‘L2_1. then it was checked if it is an RGB image using the commend isrgb. In the following code.jpg’). Then it was converted into the HSV model .imshow(Img1).1 with a title ‘’ the input RGB image’’. The quality of the resulting image depends on the approximation method you see. the RGB version is first displayed on Fig.  The tolerance if the method of quantization is ‘’uniform quantization’’. the range of colours in the input image.jpg' then record your result with comments . Here. CONVERSION BETWEEN IMAGE TYPES & FORMATS 1. Note that different methods work better for different images. Fig(1-4) Exercise: Repeat step 1 With the image 'L2_2.title(‘The input RGB Image’). figure. .Finally.jpg’’ is first read using imread. reducing the number of colours in the process. and whether or not you see dithering . the rgb2ind command converts an RGB image to an indexed image.B. Flag=isrgb(Img1). Img2=rgb2hsv(Img1). Thus one may define  The number of output reconstructed colours if the method of quantization is ‘’minimum variance’’. Indexed images give an ability to display and process colour images using only a pixel depth of N bits (usually N<24). the image ‘’L2_1.

subplot(1. to change the number of bits used to represent the indexed image obtained in step 2. Next a code for recording and displaying the indexed version of image ‘’L2_3.map1]=rgb2ind(Img1. 2. [x. 128.subimage(Img1).2). subplot(1. then converts it into indexed image. and 32. the following code reads the image ‘’L2_2. Now.256). figure.jpg’’ using N=256.  Perform dithering or no dithering to increase the apparent colour resolution of the output image.title('The input RGB Image').2. Fig (1-5) 3.map]=rgb2ind(Img1).2.subimage(x.1). [x1.jpg').map).title('The obtained Indexed Image'). Img1=imread('L2_1. For better visual comparison both images are displayed on the same Figure using the command subplot and subimage giving each one a suitable title: Img1=imread('L2_2.jpg” which is an RGB image. .jpg'). 64.

2.2. With the image ('L2_3.map3]=rgb2ind(Img1. The command ind2rgb is useful in this case. The result is shown in Fig(1-6) below: Fig (1-6) Exercises:  Now.subimage(x2.jpg').4).subimage(x1.2). Sometimes.map2).title('N=16'). subplot(2. subplot(2. refer to the help of rgb2ind.4).subimage(x4. [x4. compare with ‘’dithering on’’ (the default for rgb2ind) and comment. continue the resulting image [x. Record your results. Now.title('N=4'). subplot(2. Record your results.map4]=rgb2ind(Img1. the image available is indexed and it is required to convert it to 24 bit ture- color image.[x2. remove the axes labels around images (if possible) then record your results as shown in 3. .map2]=rgb2ind(Img1.2.map3).64).map1).map] obtained in step 2 and convert it into RGB again.2.title('N=256'). However.subimage(x3.title('N=64'). subplot(2.3).map4).1). the output image will not be better than the original (Explain why!?).16). [x3. then repeat the above code with adding the option ‘’nodither’’.  Repeat 3.

example of which are: YCbCr format Hue. examples of which are shown below: Isgray -------------------> Return true for intensity image.The functions in the image processing toolbox that work with colour assume that the images use the RGB colour space.>Creat indexed image from intensity image by thresholding. the image processing toolbox provides a variety of commands. The toolbox provides support for other colour spaces through a set of conversion functions. C. Rgb2gray -------------. Mat2gray --------------> Convert matrix to intensity image. 4.Value(HSV) format. YCBCR=rgb2ycbcr(RGB). National Television Systems Committee (NTSC) format. RGB=imread('L2_3.saturation. we may be asked to extract the grayscale version of an image from a colored image (as in monochrome TV). Grayslice --------------. . In the following code.jpg’’. Exercise: try referring to the workspace to define the size and type of the variables then record your results. Ind2gray --------------. WORKING WITH GRAYSCALE IMAGES Sometimes it is required to save space or the output to be plotted in a grayscale version (regardless the input is colored or not).jpg'). a summary of conversion operations applied to the image ‘’L2_3.>Convert RGB image or colormap to gray scale. HSV=rgb2hsv(RGB).>Convert indexed image to intensity image. YIQ=rgb2ntsc(RGB). You can use these functions to convert between RGB and these color spaces. For this or that. Also.

Follow the commands in the given code below. . 1. 2. for j =1:n. Let us start with the command mat2gray. Types help mat2gray and see what you will get. Follow the instructions to understand what is going on n=256. x=zeros(n. % creating a 200-point cosine wave t=0:pi/100:2*pi.n). try to understand it step by step. end end I=mat2gray(x). Write the following example that depicts a gray scale bar as in Fig(1-7). for i=1:n. for i=1:200 y(i. execute then record the results. end. x=cos(t). Img=mat2gray(y). imshow(I) Fig (1-7) 3.:)=x.j)=j-1. x(i.

map]=dicomread('L1_7. Img1=ind2gray(X1.map). sometimes.2.title('Indexed Version'). For example. when the image is multiple (as in DICOM).2.map). Another command which works opposite to ind2gray is gray2ind.:.10).dcm'). X1=X(:.Imshow(Img) 4.subimage(Img1). one can extract a single indexed image after which this single image may be first converted into the gray scale version then displayed. [X. Fig (1-8) .:. type help ind2gray and see what you will get. Now. The importance of such command is in that give a flexibility in dealing with images.1). subplot(1. subplot(1.2). Now let us turn to the command ind2gray.subimage(X1.title('Grayscale Version').