Read out intensites and corresponding X,Y,Z-coordinates from a 3D-image

Hello,
this is a common problem. But there were no real answers to this question in this forum.
I have a 3D-image generated from a 3D-array - I want to get the intensity values plus their corresponding position (X,Y,Z) in a normal spreadsheet in MatLab.
Ultimately, I would get something like:
Intensity | X | Y | Z
and this for every point in the image.
Since there are millions of points it might be also a good idea to cut out points with 0 intensity.
Has somebody done something similar?
Best wishes.

 Accepted Answer

What is the size of the third dimension? If it's 3 then it can be considered a normal RGB color image and you can use impixelinfo() to see the values. If you still want it in a spreadsheet, then I suggest you extract each plane of your volumetric image in a loop and then send that to Excel. If you really, really need the x,y,z values also then I suggest you create an N by 4 array and then write out
[rows, columns, slices] = size(array3D);
array2D = zeros(numel(array3D), 4);
counter = 1;
for z = 1 : slices
for x = 1 : columns
for y = 1 : rows
array2D(counter, :) = [array3D(y, x, z), x, y, z];
counter = counter + 1;
end
end
end
xlswrite(filename, array2D);

10 Comments

Thank you so much!
I think this is exactly what I need!
Unfortunately, my 3D-array is rather big (512x512x160). I ran your script with my array-variable. After half an hour I shut down my laptop since it froze for intense calculation. The resulting variable would be something in the size of 512*512*160 x 4.
But the idea is really good. I just need to set an intensity threshold and hope that it runs faster than before. I would need to include an if-statement. Yet, I dont know much about MatLab-syntax.
...Addition: I ran it again. Turns out that the writing to excel is the problem not the generation of the 2D-array. This means I could somehow delete lines with 0-intensity value.
I tried the following:
shortArray(any(array2D==0),:) = []
as well as:
>> array2D(short(:,1)==0) = [];
But there is an error message popping up:
Index exceeds matrix dimensi
for those having a similar problem: managed it with the following sytax........
rows_to_remove = any (array2D == 0, 2); array2D (rows_to_remove, :) = [];
This works since my X,Y,Z-columns do not feature zeros (unlike the intensity column).
Yes, you can remove whole row or whole columns at a time, because then the matrix would still remain rectangular. But if you just remove random isolated single elements from around the matrix, it would have ragged edges and would no longer be rectangular, and that is not allowed.
you have written the following line: array2D(counter, :) = [array3D(y, x, z), x, y, z];
is there a reason for writing y,x,z - or is it a mistake and it should also read x,y,z??
Thanks
No - no mistake on my part. What is y? It's the row or line number, right? So it's the first index since the row is the first index for arrays. Don't worry - that's a common novice mistake, getting row, column confused with x,y. So any time you are dealing with a matrix, it should be matrix(row, column) which is matrix(y, x), NOT matrix(x,y). Understand now?
I understand - very confusing but I learnt my lesson - Thank you!
That's specifically why I use row/col/page. It helps with this confusion that will come back to bite many times.
Hesham Shehata comments,
I tried to use this code with various types of images but i always get zero values only! can you please advise how to define the image with this code? thanks for your cooperation.
Then your image is all zero. Obviously the x, y, and z cannot be printed as zeros - that's impossible.

Sign in to comment.

More Answers (1)

load mri
D = squeeze(D);
idx = logical(D); % Where in D is nonzero?
vals = D(idx); % extract values
ind = find(idx); % linear index
[row,col,pag] = ind2sub(size(D),ind); % sub indices
xlswrite('file.xlsx',[row col pag vals])

Categories

Community Treasure Hunt

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

Start Hunting!