Question

I have a 3D image I want to use as a colormap. I have real-time data streaming in and depending on my current position(s), I want my graph to display a certain color based on my 3D image.

I did this earlier with success using an image of size 1080x1920 (2D image) using the following code to convert it to an image.

Color=double(imresize(RGBparam.image,[81 144]))/255;

then plotting it by setting my data (I already created figure earlier...that code shouldn't matter) where size(myData(:,:1)) outputs 81x144...the Ydata and Zdata also have the same dimensions.

set(Fig.figim, 'CData', Color);
set(Fig.figim, 'XData', myData(:,:,1) + positionX)
set(Fig.figim, 'YData', myData(:,:,2) + positionY);
set(Fig.figim, 'ZData', myData(:,:,3) + positionZ);

So that worked fine! Now I have a new 3D image. The image has dimensions 512x512x351. I tried doing the same code but it does not work. I tried this:

Color=double(imresize(DICOMparam.Vd,[81 144]))/255;

Then I try to plot like this

set(Fig.figim, 'CData', Color);
set(Fig.figim, 'XData', myData(:,:,1) + positionX)
set(Fig.figim, 'YData', myData(:,:,2) + positionY);
set(Fig.figim, 'ZData', myData(:,:,3) + positionZ);

But then I get the error: "Color must be an M-by-N matrix or an M-by-N-by-3 array"

Now I tried changing things so it turns out to be an M-by-N-by-3 array, but first of all, I don't want this...I want to keep more than just 3 samples of the z-dimension of my color map....and second of all...it spit out a bunch of errors at me anyways. So in conclusion, I simply want this 3D colormap to work when I plot my data, which has x, y, and z components. Thanks for the help, let me know if anything is unclear!

Was it helpful?

Solution

Imagine your 2D/3D image as a square/cube of color. What you want to do (if I understand correctly), is color each point in myData depending on where it falls into that square/cube.

However, that's not how 'CData' works in Matlab. It maps colours based on indexed location, not the actual values in the matrix. Therefore if your data is M-by-N, your colormap is M-by-N - e.g. regardless of the actual values of the data, myData(1,1) is coloured according to myColormap(1,1).

In the case of the M-by-N-by-3 array, these are still just indexed locations, but the three values in each case define the colour in RGB.


It is possible to do what you want, you just have to tell Matlab directly what color in that 512 x 512 x 351 cube you want to apply to each point. Presuming you have some way of converting the x, y, z positions in myData into approximate coordinates in the 512 x 512 x 351 colorspace:

xData = myData(:,:,1); 
yData = myData(:,:,2);
zData = myData(:,:,3);

% some magic here happens to make xData 1-512, yData 1-512, zData 1-351
% may require shifts, scaling, rounding as appropriate
% depends on what your myData contains exactly
% values should be integers

% now we find the positions of those points within the 512 x 512 x 351 colorspace
% size of newcmap should be 81 x 144 in your case
cind = sub2ind(size(Color),xData,yData,zData);
newcmap = Color(cind);

set(Fig.figim, 'CData', newcmap);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top