reconstruction Error of 2 colored images

12 views (last 30 days)
fadams18
fadams18 on 31 Mar 2020
Answered: sanidhyak on 4 Feb 2025 at 9:02
I have an original image of dimension 300x300x3 (RGB)
after removing some entries, I apply an algorithm to reconstuct the original image.
Now i want to find the relative error between the 2 images.
....
% I call my function norm_fro to calcuate the error
Erec(i)= norm_fro( X_origin , X_r ecovered);
....
% here is my norm_fro function
function f = norm_fro( X , Xhat )
f = norm(X -Xhat,'fro')^2/norm(X,'fro')^2;
end
I get this error
Error using norm
Input must be 2-D.
Error in norm_fro (line 11)
f = norm(X -Xhat,'fro')^2/norm(X,'fro')^2;

Answers (1)

sanidhyak
sanidhyak on 4 Feb 2025 at 9:02
Hi fadmas18,
I understand that you are trying to find the relative error between 2 images using MATLAB’s “norm” function inside your custom-made function “norm_fro”.
When running the provided code, I too faced a similar error. This error occurs because the input images provided, “X and Xhat are 3D arrays (300×300×3 for RGB), while the norm function with the fro option expects a 2D matrix. To resolve this, you need to reshape the images into 2D matrices before applying the Frobenius norm.
Kindly refer to the following corrected code:
function f = norm_fro(X, Xhat)
% Reshape 3D image into 2D matrix
X = reshape(X, [], size(X, 3));
Xhat = reshape(Xhat, [], size(Xhat, 3));
% Compute Frobenius norm
f = norm(X - Xhat, 'fro')^2 / norm(X, 'fro')^2;
end
The command reshape(X, [], size(X, 3)) changes the 300×300×3 image into a 90000×3 matrix, making it compatible with the norm function. This method enables the correct computation of the Frobenius norm for the 2D representation. With this adjustment, the "Input must be 2-D" error is fixed, allowing for an accurate calculation of the relative error.
For further reference on the “norm” function in MATLAB, kindly refer to the below documentation:
Cheers & Happy Coding!

Community Treasure Hunt

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

Start Hunting!