Exploring Image Reconstruction using Walsh-Hadamard Transform with Different Window Sizes: An Analysis of MSE, RMSE, and PSNR
Introduction:
The Walsh-Hadamard transform is a mathematical method that transfers a signal or image from the spatial to the frequency domain. The transform is like the Discrete Cosine Transform (DCT) and the Discrete Fourier Transform (DFT), except instead of the cosine or sine functions, it uses the Walsh-Hadamard matrix. The Walsh-Hadamard transform has several applications in signal and image processing, such as compression, filtering, and feature extraction. It is commonly used in digital watermarking to embed and extract watermarks from photos. It is also used to compress video sequences in video coding standards such as H.263 and MPEG-4.
Explanation of the Code:
First, we transform the grayscale image to a double precision format. The image is then subjected to the Fast Walsh-Hadamard Transform (FWHT) to acquire its frequency domain representation.
Then, for each different sized rectangular window, we generate a separate mask with a varied number of coefficients set to one. We multiply the frequency domain image by each of these masks before applying the Inverse FWHT (IFWHT) to each masked image to generate the reconstructed image for that window size.
For each window size, we compute the Mean Squared Error (MSE), Root Mean Squared Error (RMSE), and Peak Signal-to-Noise Ratio (PSNR) between the original and reconstructed images. Finally, we plot these error metrics against window size.
MSE, RMSE, and PSNR:
- MSE is a measure of the average squared differences between the original picture and the reconstructed image. The formula is as follows:
MSE = (1/(M*N)) *∑(i=1 to M)∑(j=1 to N)(I(i,j)-R(i,j))²
Where M and N are the image’s dimensions, I(i,j) is the original image pixel value, and R(i,j) is the reconstructed image pixel value.
2. RMSE is the square root of MSE and represents the average difference between the original and reconstructed images. It is calculated as
RMSE = sqrt(MSE)
3. PSNR is the ratio of a signal’s highest achievable power to the power of corrupting noise. It is calculated as
PSNR = 10*log10((255²)/MSE)
Conclusion:
The Walsh-Hadamard transform is an effective tool for studying frequency-domain data and images. In this code, we demonstrated how to apply the transform to rebuild an image using various sized rectangular windows, and how to assess the resulting reconstructions using MSE, RMSE, and PSNR. The Walsh-Hadamard transform has several uses and is an integral component of many current signal and image processing systems.
%walsh-hadamard trasnform
original_img=imread('');
original_img=im2double(original_img);
x_walsh=fwht(original_img);
walsh_img=fwht((x_walsh)');
[h, w]= size(original_img);
%reconsturtion of image
%4x4 window
mask1 = zeros (h,w);
mask1(h/h:h/64,w/w:w/64)=1;
masked_walsh1=walsh_img.*mask1;
x_walsh1=ifwht(masked_walsh1);
re_img1=ifwht((x_walsh1)');
%8x8 window
mask2 = zeros (h,w);
mask2(h/h:h/32,w/w:w/32)=1;
masked_walsh2=walsh_img.*mask2;
x_walsh2=ifwht(masked_walsh2);
re_img2=ifwht((x_walsh2)');
%16x6 window
mask3 = zeros (h,w);
mask3(h/h:h/16,w/w:w/16)=1;
masked_walsh3=walsh_img.*mask3;
x_walsh3=ifwht(masked_walsh3);
re_img3=ifwht((x_walsh3)');
%32x32 window
mask4 = zeros (h,w);
mask4(h/h:h/8,w/w:w/8)=1;
masked_walsh4=walsh_img.*mask4;
x_walsh4=ifwht(masked_walsh4);
re_img4=ifwht((x_walsh4)');
%128x128 window
mask5 = zeros (h,w);
mask5(h/h:h/2,w/w:w/2)=1;
masked_walsh5=walsh_img.*mask5;
x_walsh5=ifwht(masked_walsh5);
re_img5=ifwht((x_walsh5)');
squaredErrorImage1 = (double(original_img) - double(re_img1)) .^ 2;
MSE1 = abs(sum(squaredErrorImage1(:)) / (h * w));
RMSE1=sqrt(MSE1);
PSNR1 = 10 * log10( 256^2 / MSE1);
squaredErrorImage2 = (double(original_img) - double(re_img2)) .^ 2;
MSE2 = abs(sum(squaredErrorImage2(:)) / (h * w));
RMSE2=sqrt(MSE2);
PSNR2 = 10 * log10( 256^2 / MSE2);
squaredErrorImage3 = (double(original_img) - double(re_img3)) .^ 2;
MSE3 = abs(sum(squaredErrorImage3(:)) / (h * w));
RMSE3=sqrt(MSE3);
PSNR3 = 10 * log10( 256^2 / MSE3);
squaredErrorImage4 = (double(original_img) - double(re_img4)) .^ 2;
MSE4 = abs(sum(squaredErrorImage4(:)) / (h * w));
RMSE4=sqrt(MSE4);
PSNR4 = 10 * log10( 256^2 / MSE4);
squaredErrorImage5 = (double(original_img) - double(re_img5)) .^ 2;
MSE5 = abs(sum(squaredErrorImage5(:)) / (h * w));
RMSE5=sqrt(MSE5);
PSNR5 = 10 * log10( 256^2 / MSE5);
MSE=[MSE1,MSE2,MSE3,MSE4,MSE5];
RMSE=[RMSE1,RMSE2,RMSE3,RMSE4,RMSE5];
PSNR=[PSNR1,PSNR2,PSNR3,PSNR4,PSNR5];
Window=[4,8,16,32,128];
subplot (3,3,1); imshow(original_img); title('Original');
subplot (3,3,2); imshow(re_img1); title ('window=4');
subplot (3,3,3); imshow(re_img2); title ('window=8');
subplot (3,3,4); imshow(re_img3); title ('window=16');
subplot (3,3,5); imshow(re_img4); title ('window=32');
subplot (3,3,6); imshow(re_img5); title ('window=128');
figure;
subplot (2,2,1); plot(Window,MSE); xlabel('Window Size'); ylabel('MSE'); title('MSE');
subplot (2,2,2); plot(Window,RMSE); xlabel('Window Size'); ylabel('RMSE'); title('RMSE');
subplot (2,2,3); plot(Window,PSNR); xlabel('Window Size'); ylabel('PSNR'); title('PSNR');