How can I resolve the following error?

1 view (last 30 days)
clc;
close all;
clear all;
ref=imread('blood.jpg');
I=rgb2gray(ref);
n=size(I);
A=imnoise(I,'speckle',0.04);
A1 = wiener2(A,[3 3]);
figure;
subplot(1,2,1),imshow(I);
title('Original Image');
subplot(1,2,2),imshow(A);
title('Noisy Image(Speckle)');
figure
imshow(A1);
title('Filtered Image');
M=n(1);
N=n(2);
MSE = sum(sum((ref-A).^2))/(M*N);
MSE1 = sum(sum((ref-A1).^2))/(M*N);
PSNR = 10*log10(256*256/MSE);
PSNR1 = 10*log10(256*256/MSE1);
fprintf('\nMSE: %0.2f ', MSE);
fprintf('\nPSNR: %0.2f dB', PSNR);
fprintf('\nMSE: %0.2f ', MSE1);
fprintf('\nPSNR: %0.2f dB', PSNR1);
message = sprintf('MSE for Noisy Image.\nThe mean square error is %.2f.\nThe PSNR = %.2f\n', MSE, PSNR);
msgbox(message);
message = sprintf('MSE for Filtered Image.\nThe mean square error is %.2f.\nThe PSNR = %.2f\n', MSE1, PSNR1);
msgbox(message);
%%%%%%%%%%%%%Error%%%%%%%%%%%%%%%%%%
Error using -
Matrix dimensions must agree.
Error in spckl (line 20)
MSE = sum(sum((ref-A).^2))/(M*N);

Accepted Answer

Walter Roberson
Walter Roberson on 21 Sep 2018
You have
ref=imread('blood.jpg');
We can nearly guarantee that ref is 3D. There are very very few actual grayscale .jpg files in practice -- nearly all .jpg files that look like grayscale are actually RGB images.
I=rgb2gray(ref);
so I is 2D, and A is derived from I and is 2D as well.
Then you have
MSE = sum(sum((ref-A).^2))/(M*N);
remember that ref is 3D and A is 2D. For all versions of MATLAB up to and including R2016a, it is an error to subtract between a 3D array and a 2D array. Starting R2016b, it is no longer an error, and would be equivalent to as-if you had coded
MSE = sum(sum((bsxfun(@minus, ref,A)).^2))/(M*N);
However, you should really question whether it makes sense to subtract between the color components of an RGB image and the grayscale values in A and A1. And if you do proceed, then because you only sum() twice, you would get back a vector of 3 values, once for each of the color panes: it is clear you are not expecting that (if you were expecting it, you would be dividing by (3*M*N) )

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!