1 preliminaries - ucsb · 1 preliminaries you can access matlab toolkits from either the...

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: cyan white white green black yellow red magenta blue 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

Upload: others

Post on 17-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Preliminaries - UCSB · 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

RUTGERS UNIVERSITYSchool of Electrical and Computer Engineering

ECE447 Fall CourseLaboratory 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 Room548 or from any computer with both Matlab and the Image Processing Toolbox. Theimage 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 theMatlab 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 thatyou will display. The color image should look like this:

cyan white white

green

black

yellow

red

magenta

blue

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

Page 2: 1 Preliminaries - UCSB · 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

(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 thesaturated primary colors RGB and CMY in the correct places. Print the values of thethree images by using the Matlab command

>> [r,g,b]

The resulting image plot is rather small (3 x 3 pixels). Use the magnification featureof 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, anddisplay 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 printoutand the values of the indexed image X verify that you understand how the colors arerepresented 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 happensto 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 requestedcolor image (to full magnification) is:

2

Page 3: 1 Preliminaries - UCSB · 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

% 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

Page 4: 1 Preliminaries - UCSB · 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

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 getssmaller, 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 haveread in.

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

(b) Now change the intensity range [0 255] to a lower range [0 N]. Display and examinethe 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, wenoted that if the image is f(x, y), then its corresponding negative image would be of theform f1(x, y) = K[f(x, y)]−γ. Compute and display (use 256 levels) the negative image

4

Page 5: 1 Preliminaries - UCSB · 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

(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 twonegatives). Report: What issues must be addressed in computing a negative imagethat can be displayed as an intensity image in Matlab? Give the Matlab statementsthat you used to compute and display the negative image. Does the displayed image looklike a photographic negative?

***************************************************************************SOLUTION

(a) and (b) and (c) The rgb color image of parrots has dimensions 256 x 384 x 3 and theintensity range gray-scale image has dimensions 256 x 384. These two images are shown inFigure 4, upper left (color image) and upper right (gray scale image). The lower right imageis for a reduced color range using N = 122 as the size of the color map, and the lower leftimage 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 scaleimage.

It can be seen in the lower right image of Fig. 4 that as N , the size of the color mapis reduced from N = 256 full range to N = 122, we start seeing some distortion almostimmediately.

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 negativefor a color image. The Matlab code for generating and displaying the gray scale and colornegatives is:

% obtain negative image

xpn=imcomplement(xp);

figure;imshow(xpn);

parrotsn=imcomplement(parrots);

figure;imshow(parrotsn);

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

5

Page 6: 1 Preliminaries - UCSB · 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

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 themin the class lecture on image processing.

(a) In particular look at Matlab help for the function imcrop. Test this command outon a display of the grayscale image x of the parrots. Crop out the head of one ofthe 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 parrothead.

(b) Write a script for generating a two-component plot that includes (at the top) the grayscale image of the parrots and at the bottom a slice of the image intensity profile atline 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 grayscaleimage x. Write the resulting image to a TIFF file using the function imwrite. Readthe file back into an image with a different name, and use subplot to plot both imagesside-by-side for comparison. Report: Give the Matlab statements for accomplishingthis 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 theimage (the head of one of the parrots) and when you double click inside the cropped section

6

Page 7: 1 Preliminaries - UCSB · 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

of the image, the cropped image is saved in the array named in the call. The cropped imageis 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 theintensity 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

Page 8: 1 Preliminaries - UCSB · 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

5 Exercise 4 – Display of Color Images

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

(a) Read in the file ’parrots.tif’ and display it on the screen as a reference image. Howlarge 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 withits associated color map, and then plot it using imshow. The issue is, of course, thecomputation of the color map. The conversion is done using the Matlab functionrgb2ind. The method of computing the color map depends on the number and typeof 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 originalRGB image.” Try different values for N and observe the effects. Try plotting the colormaps 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 thecolormap 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 fullyindexed file in the upper right. The original image requires 384 x 256 x 3 8-bit bytes or 2.359million bits for full image resolution. The fully indexed color image needs 384 x 256 integerpointers into the color map, along with 98304 color levels which is actually more than the2.359 million bits for the rgb image. However we can greatly reduce the size of the colormap (along with the size of the image pointers) by using a reduced number of colors. Theimage at the lower left is for N = 32 colors in the color map (this image shows a lot of imagedegradation) and the plot at the lower right shows the color map for this highly degradedimage. By experimenting with the number of colors in the color map, it can be seen thatas few as N = 128 colors give a reasonably good approximation to the full color resolutionimage.***************************************************************************

8

Page 9: 1 Preliminaries - UCSB · 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

Figure 8: Color image and color maps for full resolution color image (98304 colors in colormap) 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 thatblack and white television sets can display a color television signal. The conversion operationsare Y

IQ

=

.299 .587 .114.596 −.274 −.322.211 −.523 .312

RGB

(1)

and RGB

=

1 .956 .6211 −.272 −.6471 −1.106 1.703

YIQ

(2)

(a) Convert the color image ’parrot.tif’ from RGB to YIQ format using the relevantMatlab command yib_image=rgb2ntsc(rgb_image). Using the mutliplot capabilityplot 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) inanother 2 x 2 image plot.

(c) Recalling that R, G, and B are intensity images such that 0 ≤ R ≤ 1, 0 ≤ G ≤ 1, and0 ≤ 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 transformationwill give values of R, G, and B in the correct ranges. Report: Give the rangesdetermined for Y , I, and Q.

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

rgb_converted=ntsc2rgb(yiq_image)

9

Page 10: 1 Preliminaries - UCSB · 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

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

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

***************************************************************************SOLUTION

(a) Figure 9 shows the intensity levels of the r, g and b components along with the rgbcomposite color image for the parrots image.

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

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

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

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

10

Page 11: 1 Preliminaries - UCSB · 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

• 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 thefollowing:

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

• minimum possible value of I is −0.606 (R=0, G=B=1.0), and the maximum possiblevalue 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 possiblevalue 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 significantlydifferent than the theoretical minimum or maximum value of Y, I or Q.(d) After going through the double transformation, there is essentially no difference betweenthe 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