0% found this document useful (0 votes)
41 views11 pages

1 Preliminaries

The document describes exercises from an image processing laboratory involving color images. 1) Students are asked to create a 3x3 color image from red, green, and blue component arrays and display the image. They scale the combined image and observe the colors darkening as the scale value decreases. 2) The color image "parrots.tif" is loaded and converted to grayscale. Negatives of the grayscale and color images are generated by inverting the pixel values. 3) The "imcrop" function is used to extract and display the head of one parrot from the grayscale image. A script is written to display the full image and an intensity profile. 4) Color image representations as RGB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views11 pages

1 Preliminaries

The document describes exercises from an image processing laboratory involving color images. 1) Students are asked to create a 3x3 color image from red, green, and blue component arrays and display the image. They scale the combined image and observe the colors darkening as the scale value decreases. 2) The color image "parrots.tif" is loaded and converted to grayscale. Negatives of the grayscale and color images are generated by inverting the pixel values. 3) The "imcrop" function is used to extract and display the head of one parrot from the grayscale image. A script is written to display the full image and an intensity profile. 4) Color image representations as RGB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

RUTGERS UNIVERSITY

School of Electrical and Computer Engineering

ECE447 Fall Course


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

1 Preliminaries
You can access MATLAB toolkits from either the departmental computers in CoRE Room
548 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:
cronos.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


>> r=[0,1,0;0,1,1;0,1,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?

***************************************************************************
SOLUTION
(a) The Matlab code that generates the rgb components for and displays the requested
color image (to full magnification) is:

2
% yellow=red+green
% magenta=red+blue
% cyan=green+blue
r=[0 1 0;0 1 1;0 1 1];
g=[0 0 0;1 1 0;1 1 1];
b=[0 0 1;0 0 1;1 1 1];
rgb_image=cat(3,r,g,b);
imshow(rgb_image,’InitialMagnification’,’fit’);
[r,g,b]

The resulting full magnification image is shown in Figure 2:

Figure 2: Specified color image.

(b) The color displays of the rgb and the indexed image are identical.
(c) The map prints out as:

map =

0 0 0
1 0 0
1 1 0
0 1 0
1 1 1
0 0 1
1 0 1
0 1 1

showing the 8 distinct colors in the indexed image.

3
Figure 3: Color plots with scaled intensities; scaling - 0.8, 0.6, 0.4 and 0.2.

(d) The scaled rgb images are displayed in Figure 3 for c=0.8, 0.6, 0.4 and 0.2. As c gets
smaller, the images become darker and the colors tend to black.
***************************************************************************

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 255 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

4
(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?
***************************************************************************
SOLUTION
(a) and (b) and (c) The rgb color image of parrots has dimensions 256 x 384 x 3 and the
intensity range gray-scale image has dimensions 256 x 384. These two images are shown in
Figure 4, upper left (color image) and upper right (gray scale image). The lower right image
is for a reduced color range using N = 122 as the size of the color map, and the lower left
image is the negative of the gray scale image.

Figure 4: Upper left: rgb color image of parrots; upper right: gray scale image of parrots;
lower right: reduced range using N = 122 gray scale image; lower left: negative of gray scale
image.

It can be seen in the lower right image of Fig. 4 that as N , the size of the color map
is reduced from N = 256 full range to N = 122, we start seeing some distortion almost
immediately.
The negative of the color image is shown in Figure 5. The color negative shows comple-
mentary colors in place of original colors and looks very similar to a photographic negative
for a color image. The Matlab code for generating and displaying the gray scale and color
negatives is:
% obtain negative image
xpn=imcomplement(xp);
figure;imshow(xpn);
parrotsn=imcomplement(parrots);
figure;imshow(parrotsn);
***************************************************************************

5
Figure 5: Color negative of parrots.

4 Exercise 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.
(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.
***************************************************************************
SOLUTION
(a) The Matlab calls to crop an image are:
figure;imshow(xp);
xpc=imcrop(xp);
figure;imshow(xpc);
The call to imcrop initiates a screen cursor that can be moved to encompass an area of the
image (the head of one of the parrots) and when you double click inside the cropped section

6
of the image, the cropped image is saved in the array named in the call. The cropped image
is shown in Figure 6 below.

Figure 6: Cropped parrot head.

(b) The Matlab script that generates and plots both the gray-scale parrot image and the
intensity profile at line 150 is:
xp=rgb2gray(parrots);
figure;subplot(211),imshow(xp);
subplot(212),plot(xp(150,:));
The resulting plot is shown in Figure 7 below.

Figure 7: Gray scale parrots along with image intensity along row 150.

***************************************************************************

7
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?

***************************************************************************
SOLUTION
Figure 8 shows the original rgb image in the upper left along with the map of the fully
indexed file in the upper right. The original image requires 384 x 256 x 3 8-bit bytes or 2.359
million bits for full image resolution. The fully indexed color image needs 384 x 256 integer
pointers into the color map, along with 98304 color levels which is actually more than the
2.359 million bits for the rgb image. However we can greatly reduce the size of the color
map (along with the size of the image pointers) by using a reduced number of colors. The
image at the lower left is for N = 32 colors in the color map (this image shows a lot of image
degradation) and the plot at the lower right shows the color map for this highly degraded
image. By experimenting with the number of colors in the color map, it can be seen that
as few as N = 128 colors give a reasonably good approximation to the full color resolution
image.
***************************************************************************

8
Figure 8: Color image and color maps for full resolution color image (98304 colors in color
map) and for highly degraded color image (32 colors in color map).

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


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)

9
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?
***************************************************************************
SOLUTION
(a) Figure 9 shows the intensity levels of the r, g and b components along with the rgb
composite color image for the parrots image.

Figure 9: Intensity levels of r (upper left), g(upper right), b(lower left) components of the
color image parrots.tif (lower right).

(b) Figure 10 shows the intensity levels of the Y, I and Q components along with the rgb
composite color image for the parrots image.

Figure 10: Intensity levels of Y (upper left), I (upper right), Q (lower left) components of
the color image parrots.tif (lower right).

(c) The ranges (minimums and maximums) for the YIQ components are as follows (for this
image):

10
• minimum of Y=0.08, maximum of Y=1

• minimum of I=−0.19, maximum of I=0.42

• minimum of Q=−0.22, maximum of Q=0.14

The theoretical maximum and minimum values of Y, I and Q for any possible image are the
following:

• minimum possible value of Y is 0 (R=G=B=0.0), and the maximum possible value of


Y is 1 (R=G=B=1.0)

• minimum possible value of I is −0.606 (R=0, G=B=1.0), and the maximum possible
value of I is 0.596 (R=1.0, G=B=0)

• minimum possible value of Q is −0.523 (R=B=0, G=1.0), and the maximum possible
value of Q is 0.523 (R=B=1.0, G=0)

It can be seen that the minima and maxima for the given image are often significantly
different than the theoretical minimum or maximum value of Y, I or Q.
(d) After going through the double transformation, there is essentially no difference between
the original rgb color image and the doubly transformed image.
(e) The maximum error in conversion is 0.
***************************************************************************

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

11

You might also like