problem 8

In this problem you will get hands on experience with changing the resolution of an image. ie., down-sampling and up-sampling.

The original image is an 8-bit gray scale image of width 479 and height 359 pixels.

Convert the original image from type uint8 to double.

Recall from the lecture that in order to avoid aliasing (eg. jagged edges) when down sampling an image, you will need to first perform low-pass filtering on the original image.

For this step, create a 3x3 low pass filter with all coefficients equal to 1/9.

H1 = \[ \left( \begin{array}{ccc} 1/9 & 1/9 & 1/9 \\ 1/9 & 1/9 & 1/9 \\ 1/9 & 1/9 & 1/9 \\ \end{array} \right) \]

H1=[1/9, 1/9, 1/9; 1/9,1/9, 1/9; 1/9, 1/9, 1/9]

Perform low-pass filtering with this filter using matlab function “imfilter” with replicate as the third argument.

resultLowPass = imfilter(srcDouble,H1,‘replicate’);

Obtain the down-sampled image by removing every other row and column from the filtered image. The resulting image should be width 240 and height 180 pixels. ie. resultDownSample=element(0,0), element(2,2),…

Then you up-sample this low resolution image to the original resolution via spatial domain processing.

Create an all zero Matlab array of width 479 and height 359. For every odd valued i and low valued j set the value equal to (i,j) equal to to value of low-resolution image \((\frac{i+1}{2},\frac{j+1}{2})\)

Create another filter of form

H2 = \[ \left( \begin{array}{ccc} 1/4 & 1/2 & 1/4 \\ 1/2 & 1 & 1/2 \\ 1/4 & 1/2 & 1/4 \\ \end{array} \right) \]

or H2=[1/4, 1/2, 1/4; 1/2, 1, 1/2; 1/4, 1/2, 1/4]

Convolve the result obtained from the previous step with H2 but do not use replicate option.

resultBiLinearInterpolation = imfilter(resultUp,H2);

Compute the PSNR to two decimal points. OMG!

How to calculate MSE and PSNR

Given two \(N_1\) x \(N_2\) images \(x(n_1, n_2)\) and \(y(n_1, n_2)\)

MSE is computed via:

\(MSE = \frac{1}{N_1 N_2} \sum_{n_1 = 1}^{N_1} \sum_{n_2 = 1}^{N_2} [x(n_1,n_2) - y(n_1,n_2)]^2\)

I tried to find if my stats book had mean standard error with a similar equation. I did not find it, but here is the wikipedia definition which does have some variables from stats, ie. sample. Considering its squared, we have \(\frac{1}{n}\) instead of \(\frac{1}{\sqrt{n}}\)

\(MSE = \frac{1}{N} \sum_{i = 1}^{n} [\hat{Y_i} - Y_i]^2\)

\(\hat{Y_i}\) is preditions or outcomes. Y is true values or input. n is number of samples in this case its the number of pixels. Since this is a 2d, he is using double summartion.

PSNR is computed via:

\(PSNR = 10 \log_{10} (\frac{MAX_I^2}{MSE})\)

Where \(MAX_I^2\) is the maximum possible pixel value of the images. For 8-bit gray-scale images this is 255 or 1 depending upon your array.

Helpful MATLAB routines

  • imfilter - filter routine
  • imshow - display the image
  • imwrite - write the matrix to file
  • disp - display a message. >> disp([‘2+2 is’,num2str(2+2)])
  • imread - read an image into an array
  • im2double - convert a integer array to doubles
  • doc - cmdline help. >> doc imfilter
  • clc - clear cmd output
  • length - size of matrix. If Y is a matrix of M x N size, >> length(Y(:)) will return M*N
  • size - returns nrows and ncols of a matrix as a vector. >> [nRow,nCol] = size(source8);
  • if - conditional test. >> if 359 ~= nRow disp(‘# rows wrong’) end
  • vector operation for selection. Given vector x, y=(1:2:end) selects every other element.
  • zeros - create a all zero array of 20 rows and 50 cols. >> zeros(20,50)
  • downsample - downSample=img8(1:2:end,1:2:end);
  • upsample - upSample(1:2:end,1:2:end) = downSample(1:end,1:end)