How to convert a uint8 image to complex double?

10 次查看(过去 30 天)
I want to convert a png file into a complex valued number. The current dimension and format of the png file is:
320x320x3 uint8
I want to convert into the format like the format below:
320x320 complex double
When I use the
A = imread("01.png");
Img = im2double(A);
The output comes as:
320x320x3 double
But I want the third dimension to be removed and merge into 320x320.
  3 个评论
Gulfam Saju
Gulfam Saju 2023-2-17
It can be calculated using the two matlab functions:
real(Img);
imag(Img);
Walter Roberson
Walter Roberson 2023-2-17
An array that shows up as 320x320x3 uint8 is not complex-valued
A = randi([1 255], [5 5 3], 'uint8');
Ar = real(A);
Ai = imag(A);
nnz(Ar), nnz(Ai)
ans = 75
ans = 0
Ari = complex(Ar, Ai);
whos A Ar Ai Ari
Name Size Bytes Class Attributes A 5x5x3 75 uint8 Ai 5x5x3 75 uint8 Ar 5x5x3 75 uint8 Ari 5x5x3 150 uint8 complex
Notice that A, the original image, does not show up as complex, and the the number of non-zero complex parts of it was zero. You can deliberately put a complex layer onto it, but as soon as any math is done on it, it will strip off the all-zero complex layer
Ard = im2double(Ar);
Aid = im2double(Ai);
Arid = complex(Ard, Aid);
Arid_plus = Arid + 0;
whos Ard Aid Arid Arid_plus
Name Size Bytes Class Attributes Aid 5x5x3 600 double Ard 5x5x3 600 double Arid 5x5x3 1200 double complex Arid_plus 5x5x3 600 double

请先登录,再进行评论。

采纳的回答

Sulaymon Eshkabilov
One of the dimensions can be removed using squeeze() fcn or taking averaged pixel values from R, G, B layers:
D = imread('01.png');
Dd = im2double(D);
Ds = squeeze(Dd(:,:,1)); % Red layer is considered
DD = mean(Dd, 3); % Averaged pixel values computed from Red, Green, Blue
  3 个评论
Walter Roberson
Walter Roberson 2023-2-17
The squeeze() is not doing anything useful there in creating Ds. Also, Ds is not used afterwards.
This code creates DD as the mean of R, G, and B (which, by the way, is not what you would do to convert to grayscale), but DD will not be complex.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by