2017-05-16 66 views
0

我寫了這個代碼:提示用戶在MATLAB GUI選擇文件夾圖像計算PSNR和MSE

InputImage=imread('ground truth 1.jpg'); 
ReconstructedImage=imread('final1.jpg'); 
n=size(InputImage); 
M=n(1); 
N=n(2); 
MSE = sum(sum((InputImage-ReconstructedImage).^2))/(M*N); 
PSNR = 10*log10(256*256/MSE); 
fprintf('\nMSE: %7.2f ', MSE); 
fprintf('\nPSNR: %9.7f dB', PSNR); 

如何修改編碼提示用戶選擇圖像的InputImageOutputImage從一個文件夾?我已經試過這樣的事情

[InFile, InPath] = uigetfile('*.jpg', 'Import image file:'); 
if ~ischar(InFile) 
    disp('User aborted file import'); 
    return; 
end 
[OutFile, OutPath] = uigetfile('*.jpg', 'Export image file:', InPath); 
if ~ischar(OutFile) 
    disp('User aborted file export'); 
    return; 
end 
InFile = fullfile(InPath, InFile); 
OutFile = fullfile(OutPath, OutFile); 

之前,但我得到了一個錯誤:

Matirx dimension not agree error 

回答

1

該代碼會工作得很好。

[InFile, InPath] = uigetfile('*.jpg', 'Import image file:'); 
if ~ischar(InFile) 
    disp('User aborted file import'); 
    return; 
end 

[OutFile, OutPath] = uigetfile('*.jpg', 'Export image file:', InPath); 
if ~ischar(OutFile) 
    disp('User aborted file export'); 
    return; 
end 
InFile = fullfile(InPath, InFile); 
OutFile = fullfile(OutPath, OutFile); 

InputImage=imread(InFile); 
ReconstructedImage=imread(OutFile); 
n=size(InputImage); 
M=n(1); 
N=n(2); 
MSE = sum(sum((InputImage-ReconstructedImage).^2))/(M*N); 
PSNR = 10*log10(256*256/MSE); 
fprintf('\nMSE: %7.2f ', MSE); 
fprintf('\nPSNR: %9.7f dB', PSNR); 

確保InputImageReconstructedImage的大小相同。

+0

爲了您的信息,這兩個文件都在該文件夾中可用。當我運行編碼時不提示用戶輸入,它工作正常。但是,在我修改編碼以提示用戶輸入之後,它會給出矩陣尺寸不一致的錯誤。任何解決方案? – jolene

+0

這個錯誤是顯示上面發佈代碼的哪一行? –

+0

如果在MSE = sum(sum((InputImage-ReconstructedImage)。^ 2))/(M * N)中顯示錯誤,那麼這是因爲InputImage和ReconstructedImage的矩陣尺寸不同。查看matlab工作區來確認。 –