How to adjust the code to work with 3D stack of images?

3 次查看(过去 30 天)
Hi everyone,
I am currently trying to modify the following code to work with the 3D stack of images needed for my image analysis:
m= imread('ImageA.tif');
o= imread('ImageB.tif');
m=m(:);
o=o(:);
common=sum(m & o);
union=sum(m | o);
cm=sum(m); % the number of voxels in m
co=sum(o); % the number of voxels in o
Jaccard=common/union; % može i ovako Jaccard = sum(m(:) & o(:)) / sum(m(:) | o(:));
Dice= 2*nnz(o&m)/(nnz(o) + nnz(m));
This current code above is for analysis of only one image. I need it to be for a 3D stack of images. I came up with the following code:
%My inputs are a 3D stack of reference images named "ground" and the images that I would like to compare against the golden truth named "segm".
m=zeros(numImages,1); %numImages is a number of images present in 3D stack, it is the same number in both "ground" and "segm"
o=zeros(numImages,1);
imSize = size( ground, 1 ) .* size( ground, 2 );
m = reshape( ground, imSize, size( ground, 3 ) );
o = reshape( segm, imSize, size( segm, 3 ) );
common=sum(m & o);
common=transpose(common);
union=sum(m | o);
union=transpose(union);
cm=sum(m);
cm=transpose(cm);
co=sum(o);
co=transpose(co);
Jaccard=zeros(numImages,1);
Dice=zeros(numImages,1);
Jaccard=common/union;
Dice=(2*common)/(cm+co); %this works the same as Dice for one image analysis above.
but when I compare the results from my code for 3D stack with the "one image at a time" approach, the results are not the same. I can not figure out where is the error.
Any help would be greatly appreciated.
Thanks!
  2 个评论
Rik
Rik 2018-3-8
You need to make sure your processing steps are element-wise and that you apply the sums over the correct dimensions. Am I correct to think that you have two binary arrays of size [sizeIM(1) sizeIM(2) numImages]?
Mario
Mario 2018-3-8
I am sure that the error lies somewhere within those processing steps regarding applying correct dimensions (element-wise), but can not figure it out correctly.
I basically have two binary sets of images that I named "ground" and "segm" with identical number of images inside them which I named numImages.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-3-8
When you have problems with dimension, you should use the debugger to go through your code line by line and check in the Workspace window if any variable have unexpected shapes.
sizeIM=[20,30];numImages=10;
%generate random images to test the code
ground=rand([sizeIM numImages])<0.3;
segm=rand([sizeIM numImages])<0.35;
common=ground & segm;
common=squeeze(sum(sum(common,1),2));
total=ground | segm;
total=squeeze(sum(sum(total,1),2));
%calculate Jaccard index and Sorensen-Dice coefficient
Jaccard=common./total;
%direct calculation:
%Dice=(2*common)./(squeeze(sum(sum(ground,1),2))+squeeze(sum(sum(segm,1),2)));
%derivated calculation:
Dice=2*Jaccard./(1+Jaccard);
  9 个评论
Mario
Mario 2018-3-15
Hi Rik, I debugged your code and it works fine.
However, I used a small batch of images and tested your code against the first method ("one image at a time" approach) and interestingly even though the results are different for each image, the mean value of Jaccard and Dice are the same for that batch of images using your approach and the first method.
They are indentical.
Mario
Mario 2018-3-15
I have another similar problem, but I will start a new thread. Hope you can help me there to.
Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by