Optimizing nested for loops for image analysis

1 view (last 30 days)
I am analyzing 4-dimensional images. The four dimensions are x and y for the resolution of the image, z for the depth of the image, and f for how many frames there are at each z (depth). I need to go through each z and sum all of the frames into one single frame. Right now, I am using nested for loops to go through each pixel of each image but this is very slow. Does anyone know how to optimize the code so that I can do this operation faster?
This is the code below:
Thank you!
clear all;
res = 1024;
frames = 20;
z = 15;
mkdir('SHG');
mkdir('TPEF');
% mkdir('Composite');
A = TIFFStack('freshex_6x_100mW_zstack_00001.tif', [], [1 z frames]);
% SHGstack = im2single(reshape(A(:,:,1,:,:), [res,res,z,frames]));
% TPFstack = im2single(reshape(A(:,:,2,:,:), [res,res,z,frames]));
%SHGstack = reshape(A(:,:,1,:,:), [res,res,z,frames]);
TPFstack = reshape(A(:,:,1,:,:), [res,res,z,frames]);
for i =1:res
for j = 1:res
for k = 1:z
%SHGsum(i,j,k) = sum(SHGstack(i,j,k,:));
%SHGmean(i,j,k) = mean(SHGstack(i,j,k,:));
TPFsum(i,j,k) = sum(TPFstack(i,j,k,:));
%TPFmean(i,j,k) = mean(TPFstack(i,j,k,:));
end
end
end

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 20 Jun 2019
TPFsum=sum(TPFstack(:,:,1:z,:));
Is this?
  1 Comment
Akarsh Lal
Akarsh Lal on 20 Jun 2019
I think I didn't phrase my question correctly...what I'm trying to do is at each depth (z), I am trying to add up all of the frames that are there. So for example, at z = 3 there are 15 frames. It's almost like there is a video at each depth and I need to add up each frame of that video at each depth. I hope that clarifies what my goal is.
Thank you for your response!

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!