Multiple Image reconstruction with real and imaginary part in 2d array

I have two mat files one with real and another with imaginary part of 500 images. I am using the following line to reconstruct a single image :
" imshow(reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]) "
Each mat file is a 2D array (16384x500) with grayscale images of size 128x128. 'i' indiactes the image/column number.
I am trying to reconstruct all the 500 images and save each of them in .jpg format in the same order from 1-500. I tried this code and I am recieving an error.
a_real = load("img_real.mat");
a_imag = load("img_imag.mat");
for i = 1 : 500
I(:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);
end
save('image', num2str(i),'.jpg');
Error:
Operator '.^' is not supported for operands of type 'struct'.
Error in image_recon (line 6)
I(:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);

 Accepted Answer

What is the name of the variable stored inside the .mat files? If I assume it is X, then,
a_real = load("img_real.mat").X;
a_imag = load("img_imag.mat").X;

9 Comments

The variables are img_real and img_imag same as the file name.
Then you should change .X to .img_real and .img_imag accordingly.
@Matt J Thank you for your response. I replaced the X and I am recieving the following error.
Error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I(:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);
You are reshaping to a 128 by 128 array, but expecting to be able to store the result in a vector I(I,i)
Consider
I(:,:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);
@Walter Roberson Thank you for the solution. I made the changes tried the code and I am getting the following errors.
a_real = load("img_real.mat").img_real;
a_imag = load("img_imag.mat").img_imag;
for i = 1 : 500
I(:,:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);
end
save('image', num2str(i),'.jpg');
Error:
Error using save
'500' is not a valid variable name.
Error in image_recon (line 11)
save('image', num2str(i),'.jpg');
I also tried for i = 1:size(a_real,2) and recieved the same error
@Matt J Thank you. I made the changes and I was able to execute the code. But the images are'nt right I get error when I try to view the images.
a_real = load("img_real.mat").img_real;
a_imag = load("img_imag.mat").img_imag;
for i = 1 : 500
I(:,:,i) = reshape(sqrt(a_imag(:,i).^2+a_real(:,i).^2),[128,128]);
save(['image', num2str(i),'.jpg'], 'I');
end
Error reading the images:
a = imread('image99.jpg');
Error using imread>get_format_info (line 545)
Unable to determine the file format.
Error in imread (line 391)
fmt_s = get_format_info(fullname);
a_real = load("img_real.mat").img_real;
a_imag = load("img_imag.mat").img_imag;
I = reshape( sqrt(a_imag.^2+a_real.^2), 128,128,[]);
N=size(I,3);
filenames="image"+(1:N)+".jpg";
for i=1:N
imwrite(I(:,:,i),filenames(i));
end
a = imread('image99.jpg');

Sign in to comment.

More Answers (0)

Asked:

on 22 Sep 2022

Commented:

on 27 Sep 2022

Community Treasure Hunt

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

Start Hunting!