You are on page 1of 5

RUTGERS UNIVERSITY

School of Electrical and Computer Engineering

ECE477 Fall Course


Laboratory No. 1
Image Processing in MATLAB – Processing of Color Images

1 Preliminaries
You can access MATLAB toolkits from either the departmental computers in CoRE Room
536 or from any computer with both Matlab and the Image Processing Toolbox. The
image files referenced in this lab can be accessed from the course website at:

www.caip.rutgers.edu\~lrr

If you are a new Matlab user, try running the built-in demos. Just type demos at the
Matlab prompt.

2 Exercise 1 – Color Image Creation


To begin you are requested to create three 3 × 3 ”images” having only 0 and 1 as elements.
These images will be the red, green, and blue component images of a 3 × 3 color image that
you will display. The color image should look like this:

black red blue

green yellow magenta

cyan white white

Figure 1: Desired 3 × 3 image.

For example, the red image could be created by the statement

1
>> r=[0,1,0;0,1,1;0,1,1];

(a) After you have created the g and b images, use the Matlab statements

>> rgb_image=cat(3,r,g,b);
>> imshow(rgb_image);

to display the image. Look at the image carefully to confirm that you have placed the
saturated primary colors RGB and CMY in the correct places. Print the values of the
three images by using the Matlab command

>> [r,g,b]

The resulting image plot is rather small (3 x 3 pixels). Use the magnification feature
of the imshow command to scale the color image to a larger size for viewing.
Report: Give the Matlab statements used to generate the green and blue images.
Also include the scaled image plot to show that you have the correct component arrays

(b) Now use the Matlab statement

>> [X,map]=rgb2ind(rgb_image);
>> figure(2);
>> imshow(X,map);

to convert to an indexed image representation, open a new image display window, and
display that representation. Compare the two displays. They should be the same.

(c) Now use the Matlab statement

>> map

to print the color map that was generated by the function rgb2ind. Using this printout
and the values of the indexed image X verify that you understand how the colors are
represented in the indexed image.

(d) Finally, scale each of the images by a constant that is less than one and display it; e.g.,
scaling by 0.8 is implemented as:

>> c=0.8;
>> rgb_scaled=cat(3,r*c,g*c,b*c);
>> imshow(rgb_scaled)

Use values of c = 0.8, 0.6, 0.4, 0.2 to observe the effect. Report: What happens
to the colors as c is varied over the range of values?

2
3 Exercise 2 – Importing, Displaying, and Converting
Images
Load and display the color image parrots.tif using the statement

>> parrots=imread(’parrots.tif’);
>> figure,imshow(parrots);

Examine the size of parrots by typing whos to find out the size of the image that you have
read in.

(a) Convert the class uint8 color image parrots to a gray scale image, and display the full
intensity range gray-scale image using the imshow command.

(b) Now change the intensity range [0 255] to a lower range [0 N]. Display and examine
the resulting gray-scale image for different values of N ranging from 256 down to 8.
Report: For what value of N do you begin to see some distortion?

(c) In class we discussed “density images” as in photographic negatives. For example, we


noted that if the image is f (x, y), then its corresponding negative image would be of the
form f1 (x, y) = K[f (x, y)]−γ . Compute and display (use 256 levels) the negative image
(with γ = 1 and K = 1) corresponding to the grayscale image x determined above.
(Also compute and display the color negative image and compare and contrast the two
negatives). Report: What issues must be addressed in computing a negative image
that can be displayed as an intensity image in Matlab? Give the Matlab statements
that you used to compute and display the negative image. Does the displayed image look
like a photographic negative?

4 Exercies 3 – imcrop – A Useful Image Processing


Tool
Matlab has many useful functions for image processing. We have discussed some of them
in the class lecture on image processing.

(a) In particular look at Matlab help for the function imcrop. Test this command out
on a display of the grayscale image x of the parrots. Crop out the head of one of
the parrots and display it on the screen using the Matlab function imshow. Report:
Briefly describe the operation of the imcrop call and include a plot of the cropped parrot
head.

(b) Write a script for generating a two-component plot that includes (at the top) the gray
scale image of the parrots and at the bottom a slice of the image intensity profile at
line 150. In doing so, you will find use for the Matlab function subplot. Report:
Give a listing of your script for generating this plot along with the plot itself.

3
(c) Try using the function imcrop to extract the head of one of the parrots in the grayscale
image x. Write the resulting image to a TIFF file using the function imwrite. Read
the file back into an image with a different name, and use subplot to plot both images
side-by-side for comparison. Report: Give the Matlab statements for accomplishing
this task.

5 Exercise 4 – Display of Color Images


In class we discussed the two most common representations of color images, namely an RGB
array or as an indexed array with a colormap. In this section we compare and contrast these
two color image representations.

(a) Read in the file ’parrots.tif’ and display it on the screen as a reference image. How
large an image is created when we use the RGB representation of this color image?

(b) A better way to display an RGB image is to convert it to an indexed image with
its associated color map, and then plot it using imshow. The issue is, of course, the
computation of the color map. The conversion is done using the Matlab function
rgb2ind. The method of computing the color map depends on the number and type
of arguments given. Use help rgb2ind to see the possibilities. For example

>> [X1,map1]=rgb2ind(parrots);
>> imshow(X1,map1);

generates a map containing entries for every pixel in the image, while

>> N=256;
>> [X2,map2]=rgb2ind(parrots,N);
>> imshow(X2,map2)

generates a colormap of N entries by a process that “best approximates the original


RGB image.” Try different values for N and observe the effects. Try plotting the color
maps using rgbplot(map). Report: How many colors yields a good looking image?
Describe what happens to pixel colors in the image as the number of possibilities in the
colormap decreases. Where do you see the effects of color quantization the most?

6 Exercise 5 – Conversion of RGB to YIQ


As discussed in class, NTSC television uses a YIQ representation instead of RGB so that
black and white television sets can display a color television signal. The conversion operations
are     
Y .299 .587 .114 R
 I  =  .596 −.274 −.322   G  (1)
    

Q .211 −.523 .312 B

4
and     
R 1 .956 .621 Y
 G = 1
  
−.272 −.647   I 

 (2)
B 1 −1.106 1.703 Q

(a) Convert the color image ’parrot.tif’ from RGB to YIQ format using the relevant
Matlab command yib_image=rgb2ntsc(rgb_image). Using the mutliplot capability
plot the R, G, B and RGB composite components in a 2 x 2 image plot format.

(b) Plot the Y, I, Q components of the image (along with the composite RGB image) in
another 2 x 2 image plot.

(c) Recalling that R, G, and B are intensity images such that 0 ≤ R ≤ 1, 0 ≤ G ≤ 1, and
0 ≤ B ≤ 1, determine the ranges of the YIQ variables. These ranges are important,
since only if Y , I, and Q satisfy them can we guarantee that the inverse transformation
will give values of R, G, and B in the correct ranges. Report: Give the ranges
determined for Y , I, and Q.

(d) Convert the YIQ image back to RGB format using the Matlab command

rgb_converted=ntsc2rgb(yiq_image)

and plot the reconverted R, G, B components, along with the composite RGB image
on another 2 x 2 image plot. How close to the original RGB components does the
double transformation come?

(e) Compute the differences between corresponding images and use the max function to
determine the maximum error in the conversion. Report: What was the size of the
errors that you observed?

7 Report
Turn in a typewritten report on this experiment. At many places in the above discussion, the
word Report: was followed by some italicized questions. Your report should answer these
questions. Be brief, but show me that you did the experiments.

You might also like