draw line in 3d given orientation matlab

Show an image in a MATLAB 3D surface plot with a separate colormap

T he surface / surf plot in MATLAB tin can visualize data in 3D. When I took a form in grad school on statistical paradigm processing, I saw a very interesting plot where data is shown as a surf plot and underneath, on the basis or 10-y plane, an paradigm is shown. The pixels of the image corresponded to the points in the 3D surface and gave some extra data about the each point, sort of similar an prototype-based version of surfc or a 3D version of plotting over an epitome background. I e'er wanted to know how to make that plot simply rather than request the prof who made it (as one is supposed to after paying tuition), I decided to figure it out on my own (thus proving why I was never good at accounting). It took some experimentation but I finally figured out how this type of plot is accomplished. In this tutorial, I will show how to do this and how to make it then that the surface plot and the paradigm can utilise different colormaps, getting around the restriction that MATLAB merely has one colormap per effigy. In effect, this will simulate multiple colormaps. The result will await something like this:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

Note how the paradigm is mapped to a plane and is shown with a jet colormap while the 3d surf is shown with a gray colormap.

Bones way to show an image on the aforementioned axes as a 3D surface

I volition first go through the basic mode to do this without multiple colormap support. Take a look at this code:

            % the information that you desire to plot equally a 3D surface.            [x,y,z]            =            peaks;            % get the corners of the domain in which the data occurs.            min_x =            min            (            min            (x)            ); min_y =            min            (            min            (y)            ); max_x =            max            (            max            (x)            ); max_y =            max            (            max            (y)            );            % the image information yous want to evidence every bit a aeroplane.            planeimg =            abs            (z);            % set up concur on then nosotros tin show multiple plots / surfs in the figure.            figure;            agree            on;            % practice a normal surface plot.            surf            (x,y,z);            % set a colormap (simply this has no effect because the next colormap            % command overwrites it)            colormap            (            greyness            );            % desired z position of the prototype plane.            imgzposition = -10;            % plot the image plane using surf.            surf            (            [min_x max_x],[min_y max_y],repmat            (imgzposition,            [            2            two            ]            ),...            planeimg,'facecolor','texture'            )            % prepare a colormap for the effigy.            colormap            (            jet            );            % prepare the view bending.            view            (            45,30            );            % labels            xlabel            (            '10'            );            ylabel            (            'y'            );            zlabel            (            'z'            );

The higher up lawmaking volition produce this plot:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

I offset created the 3D information to be plotted by making utilise of the peaks function. Next, I constitute the domain of the information (extent in the x-y aeroplane). This is used to ascertain the plane that the image volition be mapped to in the terminal plot. The prototype to be shown is stored in planeimg (in this case I merely used abs(z) but the epitome can whatsoever arbitrarily sized assortment of data - it will be stretched or squeezed to fit the aeroplane defined by the extent of the domain of the data). I then plotted the 3D data by calling surf. Finally, the image is mapped to a plane parallel to the x-y plane by the following lines:

            % desired z position of the prototype plane.            imgzposition = -10;            % plot the image plane using surf.            surf            (            [min_x max_x],[min_y max_y],repmat            (imgzposition,            [            2            2            ]            ),...            planeimg,'facecolor','texture'            )

imgzposition sets where on the z axis the image plane is to exist placed; I set -ten and then it would not intersect with the existing 3D surface. The surf command hither has defined a planar surface with the following vertices:

(min_x, min_y, imgzposition)

(max_x, min_y, imgzposition)

(max_x, max_y, imgzposition)

(min_x, max_y, imgzposition)

To sympathize how this works, refer to MATLAB'south surf documentation, which states:

surf(X,Y,Z) creates a shaded surface using Z for the color data every bit well as surface summit. X and Y are vectors or matrices defining the x and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m, where [grand,n] = size(Z). In this example, the vertices of the surface faces are (X(j), Y(i), Z(i,j)) triples.

In any example, the planeimg is used every bit a texture map for the single face of the planar surface.

You will take noticed that I actually accept two colormap commands in the lawmaking, only both the 3D surface and the image take the same colormap. This is because there is only one colormap for the figure. The next department volition discuss how to go around this and use a different colormap for the 3D surface and the image aeroplane.

Show the image with a different colormap by faking multiple colormaps

To requite the image a dissimilar colormap than the 3D surface, all I need to exercise is convert the epitome (which is just a 2nd assortment of values) into a true color image according to the desired colormap. This is how:

            % scale the between [0, 255] in guild to utilize a custom colour map for it.            minplaneimg =            min            (            min            (planeimg)            );            % find minimum offset.            scaledimg =            (            floor            (            (            (planeimg - minplaneimg)            ./            ...            (            max            (            max            (planeimg)            )            - minplaneimg)            )            *            255            )            );            % perform scaling            % convert the image to a true color image with the jet colormap.            colorimg = ind2rgb(scaledimg,jet            (            256            )            );

Due to the manner colormap and ind2rgb work, the prototype must commencement be scaled to between [0, 255] before being converted to true color. I also take to specify a 256 element colormap (since [0, 255] has 256 possible values).

At present all we have to do is instead of showing planeimg, we show colorimg instead:

            % plot the image plane using surf.            surf            (            [min_x max_x],[min_y max_y],repmat            (imgzposition,            [            ii            2            ]            ),...            colorimg,'facecolor','texture'            )          

This volition produce a figure very similar to the one at the beginning of this tutorial. If you desire to get rid of the wireframe mesh (as I did), then simply specify 'edgecolor' to be 'none' in your surf commands:

            surf            (x,y,z,'edgecolor','none'            );

Consummate code snippet

Here it is altogether:

            % the information that you lot want to plot as a 3D surface.            [x,y,z]            =            peaks;            % get the corners of the domain in which the information occurs.            min_x =            min            (            min            (x)            ); min_y =            min            (            min            (y)            ); max_x =            max            (            max            (10)            ); max_y =            max            (            max            (y)            );            % the epitome data you lot want to bear witness as a aeroplane.            planeimg =            abs            (z);            % calibration image between [0, 255] in order to use a custom color map for it.            minplaneimg =            min            (            min            (planeimg)            );            % observe the minimum            scaledimg =            (            floor            (            (            (planeimg - minplaneimg)            ./            ...            (            max            (            max            (planeimg)            )            - minplaneimg)            )            *            255            )            );            % perform scaling            % catechumen the image to a true colour image with the jet colormap.            colorimg = ind2rgb(scaledimg,jet            (            256            )            );            % fix hold on so nosotros can evidence multiple plots / surfs in the effigy.            figure;            concur            on;            % practice a normal surface plot.            surf            (ten,y,z,'edgecolor','none'            );            % prepare a colormap for the surface            colormap            (            grayness            );            % desired z position of the epitome plane.            imgzposition = -10;            % plot the image plane using surf.            surf            (            [min_x max_x],[min_y max_y],repmat            (imgzposition,            [            ii            2            ]            ),...            colorimg,'facecolor','texture'            )            % set the view.            view            (            45,30            );            % label the axes            xlabel            (            'x'            );            ylabel            (            'y'            );            zlabel            (            'z'            );

This volition produce the figure shown at the beginning of the tutorial:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

rutterthansin1968.blogspot.com

Source: http://www.peteryu.ca/tutorials/matlab/image_in_3d_surface_plot_with_multiple_colormaps

0 Response to "draw line in 3d given orientation matlab"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel