How can I resolve the following error?

조회 수: 1 (최근 30일)
Syed Zenith Rhyhan
Syed Zenith Rhyhan 2018년 9월 21일
댓글: Syed Zenith Rhyhan 2018년 9월 21일
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);

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 21일
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) )
  댓글 수: 1
Syed Zenith Rhyhan
Syed Zenith Rhyhan 2018년 9월 21일
Thanks a lot for your instant reply/feedback

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by