Digital Image Processing Programming Exercise 2012 Part 2 Part 2 of the Digital Image Processing programming exercise has the same format as the first part. Check the web page http://www.ee.oulu.fi/research/imag/courses/dkk/pexercise/ occasionally since updates and errata of these instructions will be published on that page. If you experience problems that cannot be solved using the course material and Matlab help files, contact the programming exercise assistant at email address dkk-staff@ee.oulu.fi or by visiting the office TS315. Write your solutions and answers into an exercise report which must then be printed out, stapled together with the requested attachments and filled cover page (download the cover page from the web address mentioned above). You are encouraged to seek information in other places than the course book and lecture material but list all the sources you used in the report. When you have completed all the questions, 1. Return the exercise report (consisting of cover page, actual text and requested attachments) on paper to the mail box with sign Digital image processing on the 3 rd floor of Tietotalo 2. Send all the requested Matlab scripts by e-mail to dkk-staff@ee.oulu.fi. Put your student id number and your name to the subject of the email. The e-mail should contain the following files as attachments: 1) script2_1.m, 2) script2_2.m, 3) script2_3.m, 4) result2_4.jpg, 5) script2_4.m, 6) script2_5.m, 7) script2_6.m, 8) script2_7.m, 9) findgraythresh function. All the Matlab scripts must be attached also to the printed exercise report. The deadline for returning the report and sending the scripts by e-mail is 30.11.2012 15:45. Image restoration 1. The gray-scale images noise1.tif and noise2.tif and the binary image noise3.tif contain different types of noise. Utilizing your knowledge about image restoration in the presence of the noise, write a Matlab script script2_1.m that reduces the noise from the images and plots the resulting images into one figure. Save this figure and include it into the exercise report. Attach script2_1.m to the exercise report and e-mail. Note that there is different kind of noise in the images, so remember to choose a filter which is appropriate for each noise type. Hint: you can compare the results between different methods visually or numerically, e.g. by computing the root mean square error before and after filtering the image (original image for noise1.tif and noise2.tif is available).
2. The image periodic.tif contains a periodic (i.e. sinusoidal) perturbation. Write a Matlab script script2_2.m that loads the image, removes the perturbation as well as possible and displays the resulting image and its Fourier transform in same figure. Save this figure, and include it into the exercise report and explain your solution briefly. Attach script2_2.m to the exercise report and e-mail. Hint: compute the Fourier Transform of the noisy image and plot the magnitude of it using logarithmic scale and imagesc (like in the Programming Assignment 1) or in 3D using mesh function. You should see sharp peaks in the spectral image which should be filtered out. You can easily modify the ideal lowpass filter code example (from PA1) to implement a narrow band-reject filter when you have found the relative frequency that need to be filtered out. 3. Read the part of course material concerning Wiener filtering. Now create a noisy and motion blurred version of jetplane.png: I = imread('jetplane.png'); I = im2double(i); PSF = fspecial('motion', 13, 45); I_blurred = imfilter(i, PSF, 'circular'); I_blurredNoisy = imnoise(i_blurred, 'gaussian', 0, 0.0005); Your task is to write script script2_3.m that restores the image I_blurredNoisy to original as well as possible using the deconvwnr function and plots the original and the restored image into one figure. Note, however, that you will not get a perfect restoration because of the Gaussian noise! A good choice of the NSR parameter of deconvwnr function is important for a successful result because the result is dominated by the effects of noise, which can be easily seen if value NSR=0 is used. Try finding the value that gives the visually good result (e.g. how well you can distinguish the plane type written on the vertical stabilizer). Hint: you get a good estimate of the point spread function (PSF) of the blurring transformation (needed in Wiener filtering) when you take a look at how the image was created. Which value did you chose? What does the parameter NSR represent? Theoretically, what would the correct choice in stead of a scalar value? Plot the restored images obtained using values NSR=0 and your choice of NSR into one image an attach it to the report. Attach script2_3.m to the exercise report and e- mail. Color image processing 4. The colors in the image mandril.png appear too bright and the contrast is too low. As you know from the previous part of this exercise, this type of a problem can be corrected by doing contrast stretching using the imadjust function. Furthermore, the image is slightly
blurry, which can be corrected using a sharpening filter. When processing color images, the things are bit more tricky than in the first programming exercise, because the image now contains three channels. Your task is to write a script script2_4.m which loads the image mandril.png, applies first contrast stretching using the imadjust function followed by a sharpening filter and plots the original image and the enhanced image into one figure. Note that you need to convert the image to HSV (HSI) color space for contrast stretching and do the stretching only to the V channel and then convert the processed image back to RGB color space. You can access the individual channels using e.g. RGB(:,:,1) which gives first channel (red channel) of the RGB image. Why is the contrast stretching performed only to the V channel of the image? Plot the histograms of the V channel before and after contrast stretching into one figure and attach it into the exercise report. Save the processed color image as result2_4.jpg, and attach files script2_4.m and result2_4.jpg to the e-mail. Attach the file script2_4.m to your exercise report. You do not need to print out the color image! Segmentation 5. The course book introduces the following algorithm for finding a threshold T for global thresholding: 1. Select an initial estimate for T. (In this exercise, select T to be the mean gray value of the image to be segmented) 2. Segment the image using T. This will produce two groups of pixels: G1 consists of all pixels with gray level values >T and G2 consisting of pixels with values <= T. 3. Compute the average gray level values u1 and u2 for the pixels in regions G1 and G2. 4. Compute a new threshold value T=0.5 (u1 + u2) 5. Repeat steps 2 through 4 until the difference in T in successive iterations is smaller than a predefined parameter T0. (In this exercise, T0=1). Write a Matlab function findgraythresh that implements this algorithm. Test your function with the image eight.tif. Using the settings given above, you should obtain a threshold of 165.47. Segment the image using this threshold with function im2bw value and plot the resulting black-and-white image (Image 1.) As you can see, the resulting image contains unwanted noise (white dots in the coin area). Use morphological operators (function bwmorph) to remove the noise. Plot the resulting image from which noise has been removed (Image 2.). Which morphological operator did you use?
Another way of removing the noise is to process the image before segmentation. Use the fspecial function to create a convolution mask of a Gaussian filter and filter the image using this mask: >> h=fspecial('gaussian',7,2); >> eight2=imfilter(eight,h); % where eight = original image Plot the filtered image (Image 3.), find a threshold for this new image using findgraythresh and segment the filtered image with im2bw. What is the gray-level threshold that you got for the filtered image? Plot the resulting black-and-white image (Image 4.). Use morphological boundary extraction (see course book or lecture material) to the binary image to find the pixels on the boundary of the coins. Superimpose the result on the original image, assuming that the variable boundary contains the result of boundary extraction: eight3=eight; eight3(boundary==1)=0; and plot the resulting image (Image 5.). Plot images 1, 2, 3 and 4 in one figure and the Image 5 into one separate figure. Put all your Matlab code performing the required tasks into script2_5.m. Attach the figures and script2_5.m and findgraythresh.m to the exercise report and attach script2_5.m and findgraythresh.m to the e-mail. Watersheds 6. If the imaging conditions are not optimal or the image is cluttered, thresholding does not produce acceptable segmentation. Here we simulate difficult lighting conditions by multiplying the image with a lighting component : I=imread('eight.tif'); I=double(I); load('light.mat'); I2=I.*light; imshow(uint8(i2)); The resulting image cannot be segmented with a simple thresholding method. Try, what happens. Attach the resulting image to your report and explain the results. Instead, we compute the gradient magnitude and apply watershed segmentation. Read the part concerning watersheds in the lecture material and do the following: h=fspecial('sobel'); gx=imfilter( I2, h, 'replicate' ); gy=imfilter( I2, h', 'replicate' ); g=sqrt(gx.^2 + gy.^2); L=watershed(g); imshow(l==0); % horizontal % and vertical gradients % combined magnitudes
As you can see, watershed segmentation causes severe over-segmentation. This is because watersheds create an image segment around every local minimum in the input image. To see the locations of the local minima in the gradient image, try: imshow(imregionalmin(g)); To overcome this problem, we use so called markers. Make a set of internal markers, i.e. set of pixels that are certain to belong into foreground objects: im=zeros(size(g)); im(50,125)=1; im(150,75)=1; im(75,250)=1; im(200,200)=1; Note that the set of internal markers can also be obtained automatically using, e.g. texture, size or shape of the objects but here we placed the markers by hand for clarity. We also need external markers that are certain to belong to background. Here we choose to mark to background those pixels that lie exactly midway between internal markers. This is done by solving another watershed problem: Lim=watershed(bwdist(im)); %bwdist computes distance transform for each pixel - em=lim==0; %larger value means longer distance from marker points markers=em im; %(i.e. nearest nonzero pixel) imshow(markers); Next we use the function imimposemin to modify the input image so that local minima occur only at marked locations: g2=imimposemin(g,markers); L2=watershed(g2); I3=I2; I3(L2==0)=0; imshow(uint8(i3)); You should get a rather good segmentation between the coins and background. Attach the resulting image to your report, and your Matlab code as script2_6.m to the report and e-mail (script2_6.m should contain all the different phases of this segmentation task, i.e. the simple tresholding experiment and the watershed part). Wavelets and image compression 7. Load the image lena.tif into matrix I, plot it and convert it to double type. Now use the wavelet toolbox of Matlab (installed on the workstations of the Department of Electrical and Information Engineering) to create a 3-level decomposition of the image using Haar wavelets:
[C,S]=wavedec2(I,3,'haar'); Plot the coefficients with the function plotwaveletcoeff (from the zip-file): plotwaveletcoeff(c,s); Now, we demonstrate image compression with wavelets by setting most detail coefficients to zero (this is a very simple quantization method - in a real compression systems more sophisticated quantization would be used): % Approximation coefficients: a=c(1:s(1,1)*s(1,2)); % Detail coefficients: d=c(s(1,1)*s(1,2)+1:size(c,2)); % 90% of absolute values of detail coefficients are smaller than this threshold value thresh=qtile(abs(d),0.90) % Set these these detail coefficients to zero and plot the resulting coefficients: d(abs(d)<thresh)=0; C2=[a d]; plotwaveletcoeff(c2,s); % Reconstruct image: I2=waverec2(C2,S,'haar'); imshow(uint8(i2)); Now do the same steps using Daubechies 2 wavelet (use 'db2' instead of 'haar') in wavedec2 and waverec2. Which wavelets produce a better visual quality? To get some numerical evidence on your observations, compute the root mean square errors resulting from Daubechies 2 and Haar based compression (images in double type!). Attach your Matlab code as script2_7.m to the e-mail and report. Aftermath 8. How much time did you need to complete this exercise? 9. Did you experience any problems with the exercise? Was there enough help available? Should this document be more detailed?