0
votes

I am trying to remove blank (=0) cols and rows from 2D images in 3D stack of images and then generate a new 3D stack:

for i=1:numVols;

     for j=1:numFrames; % Crop black boundaries
        tempvol = VolStack(:,:,j,i);
        tempvol(:,all(tempvol==0,1))=[];
        tempvol(all(tempvol==0,2),:)=[];
                VolStackTemp(:,:,j,i) = tempvol;
     end

end

The strange thing is that it works sometimes but most of the time I get an error due to the line:

VolStackTemp(:,:,j,i) = tempvol;

Subscripted assignment dimension mismatch

Any ideas why?

1
That's not possible because tepvol has a different size in each iteration. A matrix has always the same size in each slice. - Daniel
You'll have to make VolStackTemp a cell matrix - Dan
Thanks..should have realized that! Sometimes for my data tempvol is the same size for all 2D images in the 3D stack but other times it varies as you mention (e.g. for j = 1 to 130, size(tempvol) = 487 391, but then for j = 131, size(tempvol) = 486 391). I think, as a work around, for the first 2D image of the stack, I’ll crop as above but then use the same 2D dimensions (of first 2D image) for all subsequent 2D images. - 2one

1 Answers

2
votes

With the additional content from your comment, I would solve it this way:

%get all cols which are zero in all slices
h=all(all(VolStack==0,1),2)
cidx=all(h,3)
%same for rows
ridx=all(h,4)
%delete zero only rows and cols:
VolStack=VolStack(:,:,~ridx,~cidx)

This way your code will run faster and no nonzero data is deleted.