This problem is from coursera's course on image processing
Fundamentals of Digital Image and Video Processing
Northwestern University
So the question goes like this
In this problem and the next, you will implement spatial-domain low-pass filtering using MATLAB, and evaluate the difference between the filtered image and the original image using two quantitative metrics called Mean Squared Error (MSE) and Peak Signal-to-Noise Ratio (PSNR). Given two N1×N2 images x(n1,n2) and y(n1,n2) , the MSE is computed as MSE=1N1N2∑N1n1=1∑N2n2=1[x(n1,n2)−y(n1,n2)]2 .
The PSNR is defined as PSNR=10log10(MAX2IMSE) , where MAXI is the maximum possible pixel value of the images. For the 8-bit gray-scale images considered in this problem, MAXI=255 .
Follow the instructions below to finish this problem.
(1) Download the original image from here. The original image is a 256×256 8-bit gray-scale image.
(2) Convert the original image from type 'uint8' (8-bit integer) to 'double' (real number).
(3) Create a 3×3 low-pass filter with all coefficients equal to 1/9, i.e., create a 3×3 MATLAB array with all elements equal to 1/9.
(4) Low-pass filter the original image (converted to type 'double') with the filter created in step (3). This can be done using the built-in MATLAB function "imfilter". The function "imfilter" takes three arguments and returns one output. The first argument is the original image (converted to type 'double'); the second argument is the low-pass filter created in step (3); and the third argument is a string specifying the boundary filtering option. For this problem, use 'replicate' (including the single quotes) for the third argument. The output of the function "imfilter" is the filtered image.
(5) Compute the PSNR value between the original image (converted to type 'double') and the filtered image by using the formulae given above.
I made 2 functions so that they can be used later also.
1st one is calpsnr:
The code:
function [ PSNR ] = calcpsnr( MAXI,MSE ) %This funtion calculates the PSNR value i.e. peak signal to nose ratio PSNR=10*log10(MAXI/MSE); end
function MSE=calcmse(n1,n2,I1,I2) %this calculates mean square error MSE=1/(n1*n2)*sum(sum((I1-I2).*(I1-I2)));
I1=imread('lena.gif'); %Read the image I1=im2double(I1);%converting to double filter1=ones(3)/9;%Create a 3x3 filter each sample having values of 1/9 filter2=ones(5)/25;%lookup ones() in matlab documentation help I2=imfilter(I1,filter1,'replicate');%apply 3x3 filter I3=imfilter(I1,filter2,'replicate');%apply 5x5 filter %uncomment to show images %figure('name','Original');imshow(I1);figure('name','3x3');imshow(I2);figure('name','5x5');imshow(I3) MSE1=calcmse(256,256,I1,I2); MSE2=calcmse(256,256,I1,I3); PSNR_1=calcpsnr(1,MSE1) PSNR_2=calcpsnr(1,MSE2)
PSNR_1 = 29.2951 PSNR_2 = 25.7335
.
*Note: All matlab functions must be under same directory and make sure it is your working directory. Also I have renamed the image to 'lena.gif' for convenience and is also present the the working directory.
Output Images(The effect of filtering;image gets blurry):
Download the code files:Google Drive Download Link
Note: I have paused this course, but I will resume soon after completing Digital Signal Processing course in my college.

No comments:
Post a Comment