Error using the immse function

8 views (last 30 days)
curtis baldwin
curtis baldwin on 6 Nov 2019
Answered: Subhadeep Koley on 9 Nov 2019
I have used this function for other images / filters and not come across this error no matter what i change the error keeps appearing and wont work
code --
clc
close all
clear all
P = imread('PandaOriginal(1).bmp');
a =imread('PandaNoise(1).bmp'); %Load in the PandaNoise image into matlab
b = fft2(double(a)); % Perform FFT of the PandaNoise Image
a1=fftshift(b); % frequency scaling
[X1, Y1]=size(b); % Define the size of the images X and Y
R=10;
X2=0:Y1-1;
Y2=0:X1-1;
[X2, Y2]=meshgrid(X2,Y2); %Define a mesh gride of the size X & Y
Cx=0.5*Y1; %* the filter by the original Y of the image
Cy=0.5*X1; %* the filter by the original X of the image
Low=exp(-((X2-Cx).^2+(Y2-Cy).^2)./(2*R).^2);
High=1-Low; % In order to apply the high pass filter - the low pass filter from the image
% Filtered image=ifft(filter response*fft(original image))
f=a1.*Low;
F1=ifftshift(f);
c1=ifft2(F1);
e=a1.*High;
d1=ifftshift(e);
c2=ifft2(d1);
subplot(221),imshow(a),title('Original Noisy Image'); %Plotting onto graph Image of the original noisy image
subplot(222),imshow(abs(a1),[-12 300000]),title('Fast Fourier Transformation Of Image'); %Plotting onto graph the FFT of the noisy image
subplot(223),imshow(abs(c1),[12 290]),title('LowPass Filtered Image'); %Plotting onto graph the results of LowPass filtter
subplot(224),imshow(abs(c2),[12 290]),title('HighPass Filtered Image'); %Plotting onto graph the results of HighPass filter
err1 = immse(c1, P);
fprintf('\n the mean square error for low pass filter is %f\n', err1);
err2 = immse(c2, P);
fprintf('\n the mean squared error for high pass filter is %f\n', err2);
Error message --
Error using immse (line 34)
A and B must have the same class.
Error in Untitled4 (line 32)
err1 = immse(c1, P);

Answers (1)

Subhadeep Koley
Subhadeep Koley on 9 Nov 2019
Curtis, your original image 'PandaOriginal(1).bmp' is of type uint8. Use the function im2double() to convert your image to double precision. Refer the code below.
clc; close all
P = im2double(imread('PandaOriginal(1).bmp')); % uint8 to double conversion
a = imread('PandaNoise(1).bmp'); %Load in the PandaNoise image into matlab
b = fft2(double(a)); % Perform FFT of the PandaNoise Image
a1=fftshift(b); % frequency scaling
[X1, Y1]=size(b); % Define the size of the images X and Y
R=10;
X2=0:Y1-1;
Y2=0:X1-1;
[X2, Y2]=meshgrid(X2,Y2); %Define a mesh gride of the size X & Y
Cx=0.5*Y1; %* the filter by the original Y of the image
Cy=0.5*X1; %* the filter by the original X of the image
Low=exp(-((X2-Cx).^2+(Y2-Cy).^2)./(2*R).^2);
High=1-Low; % In order to apply the high pass filter - the low pass filter from the image
% Filtered image=ifft(filter response*fft(original image))
f=a1.*Low;
F1=ifftshift(f);
c1=ifft2(F1);
e=a1.*High;
d1=ifftshift(e);
c2=ifft2(d1);
subplot(221),imshow(a),title('Original Noisy Image'); %Plotting onto graph Image of the original noisy image
subplot(222),imshow(abs(a1),[-12 300000]),title('Fast Fourier Transformation Of Image'); %Plotting onto graph the FFT of the noisy image
subplot(223),imshow(abs(c1),[12 290]),title('LowPass Filtered Image'); %Plotting onto graph the results of LowPass filtter
subplot(224),imshow(abs(c2),[12 290]),title('HighPass Filtered Image'); %Plotting onto graph the results of HighPass filter
err1 = immse(c1, P);
fprintf('\n the mean square error for low pass filter is %f\n', err1);
err2 = immse(c2, P);
fprintf('\n the mean squared error for high pass filter is %f\n', err2);
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!