0

I have written this code:

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);

How do I modify the coding to prompt the user to select an image for InputImage and OutputImage from a folder? I have tried something like this before

[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);

but I got an error:

Matirx dimension not agree error
Adriaan
  • 17,741
  • 7
  • 42
  • 75
jolene
  • 29
  • 6

1 Answers1

1

This code will work just fine.

[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);

Make sure the size of InputImage and ReconstructedImage are same.

Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19
  • for your information, both files are available in the folder. When i run the coding without prompting user input, it works fine. But after I modify the coding to prompt for user input, it gives matrix dimension not agree error. Any solution to this? – jolene May 16 '17 at 19:27
  • That error is show at which line of the above posted code? – Rijul Sudhir May 16 '17 at 19:31
  • If the error is shown in `MSE = sum(sum((InputImage-ReconstructedImage).^2))/(M*N);` then it is because, matrix dimension of `InputImage` and `ReconstructedImage` are different. Look in the matlab workspace to confirm. – Rijul Sudhir May 16 '17 at 19:42
  • The code works fine now. And another thing, how do I display those PSNR and MSE values on 'edit text' in MATLAB GUI? – jolene May 16 '17 at 19:45
  • `set(handles.edit1, 'string', MSE);` where `edit1` is the tag name of edit text – Rijul Sudhir May 16 '17 at 19:55
  • what if I want to display PSNR on one edit text and MSE on another edit text? Do I use the same coding? – jolene May 16 '17 at 19:57
  • each edit text has a unique tag name. If the tag name of MSE is `edit1` and tag name of PSNR is `edit2`. Then `set(handles.edit1, 'string', MSE);` `set(handles.edit2, 'string', PSNR);` – Rijul Sudhir May 16 '17 at 19:59
  • In the matlab GUI editor click on the `edit text` and tag name will be shown at bottom left corner. Something like `Tag: edit1` – Rijul Sudhir May 16 '17 at 20:02
  • thank you so much for your help. Everything works just how I wanted it to be. thanks – jolene May 17 '17 at 04:14