My data type changes to uint8 after if condition I want to keep it int16

5 次查看(过去 30 天)
I'm running a for loop with an if condition. In this loop I apply conditions to an int16 image after reading it. However, for some reason the data type changes to uint8 which I don't want. I want to keep the data type the same (int16). Help!!!!
Here is the code I'm using:
%I want to keep data type the same!!
for TrialFrame=1:num_stackimages;
image=imread(fullFileName,TrialFrame); %Here the data type is int16
if Trials==1
SubVideoGREEN(:,:,TrialFrame)=image; %Here data type changes to uint8
else
TrialFrame2=TrialFrame+num_stackimages.*(Trials-1);
SubVideoGREEN(:,:,TrialFrame2)=image; %Here data type changes to uint8
end
end
  7 个评论
dpb
dpb 2021-6-29
But that's not the variable complained over...it'll be a double unless it's been cast somewhere earlier...which, of course, without rest of code and without the data files it's pretty-much a guess as to why/what is happening...
Lina Koronfel
Lina Koronfel 2021-6-30
@Bjorn Gustavsson,@Stephen Cobeldick,@Joseph Cheng,@dpbThank you so much for the replies. I did preallocate SubVideoGREEN as uint8 which was way earlier in the main code so I missed it (my bad!!). However, as you recommedned, if the type of variable is not identified, it will result in a double SubVideoGREEN matrix . Thus it is important to identify it as 'int16' when preallocating. I also changed 'image' to something else. Now it is working fine.
@dpb The bit size matters because what this code does is concatenating a stack of images into a bigger stack, which I convert back into images (frames), and I don't want to lose the original information, which is recorded as int16.
Here is the code as it works now. Thank you!!
SubVideoGREEN=zeros(H,W,F,'int16');
for TrialFrame=1:num_stackimages;
FrameinStack=imread(fullFileName,TrialFrame); %reads int16
if Trials==1
SubVideoGREEN(:,:,TrialFrame)=FrameinStack; %now allocated as int16
else
TrialFrame2=TrialFrame+num_stackimages.*(Trials-1);
SubVideoGREEN(:,:,TrialFrame2)=FrameinStack; %now allocated as int16
end
end

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2021-6-30
编辑:Jan 2021-6-30
A simplification of your code:
SubVideoGREEN = zeros(H, W, F, 'int16');
for TrialFrame=1:num_stackimages;
TrialFrame = TrialFrame + num_stackimages * (Trials - 1);
SubVideoGREEN(:, :, TrialFrame) = ...
imread(fullFileName, TrialFrame);
end
There is no need to distinguish the case Trial==1 from the other frames, because the formula to get the TrialFrame replies the same value for both cases.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by