Sum of two images from two different 3D matrix

2 views (last 30 days)
Hello.
I have two 3D matrix of binary images loaded in matlab named ORIG and PORE. I have two 3D matrix of 16 images named ORIG(300x300x16) and PORE(300x300x16). I was wondering how can I extract sum of images in those two 3D matrix. I wanted to do the sum from the each of the coresponding images (first from ORIG matrix compared to first from PORE matrix, second from ORIG matrix compared to second from PORE matrix, etc.) from both matrix and to put them in a new (16x1) variable where I will have all the values as a result of sum computations of those two matrix. I have a code that I wrote for the comparison of two single images and it works fine. But now i want to try and do it for a whole batch of images (16 of them), since that will reduce time and manual labour.
Here is the snipet of my code that I have done so far. The problem lies somewhere here:
TargetImg=ORIG; % 3D matrix containing my original binary images
TestImg=PORE; % 3D matrix containing my test binary images
[u, v, q]=size(TargetImg);
TargetMask = logical(TargetImg);
NotTargetMask = logical(1-TargetMask);
TestMask= logical(TestImg);
FPimg = TestMask.*NotTargetMask;
FP=zeros(q,1);
for i=1:q
FP(q,1) = sum(FPimg(:)); %I think that here is the problem, but I am not sure how to solve it.
end
The problem that I have here is that FP returns (I think) the sum of all FPimg matrix(sum of all 16 images) as all values inside this FP variable.
FP=[24253 24253 24253 24253 24253........ 24253]. % this is what I got as a result.
And I need it to return sum of each two images compared from both matrix ORIG and PORE. I do not know how to perform this and I'm strugling with it.
I hope that I explained it clearly for everybody to understand.
Thanks in advance!
  1 Comment
Mario
Mario on 24 Feb 2015
Sorry, I did not explain correctly what I was trying to get. Let me correct myself.
I wanted to do the sum of each image in only ORIG matrix and then put those values in a new variable called FP which will be a 16x1 matrix. Later I will have to do it separately for the PORE matrix too, but for now I need this.
Thanks!

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 Feb 2015
FP = squeeze(sum(sum(ORIG, 1), 2))
should do it. sum along x, then along y and remove the singular dimensions

More Answers (0)

Community Treasure Hunt

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

Start Hunting!