How to texture map to a rotated surface in matlab? - matlab

I would like to texture map a 2D image to a 3D surface. I would like to do it in GUI so the user can rotate/shift/zoom the surface with the camera toolbar and then the image will be mapped to the visible portion of the surface.
I know I can get the camera position with campos command, but how do I get the camera orientation?
Any idea? Or maybe you can suggest a better approach?
Thanks!

To answer the question from your title (from what I understand), you can assign a texture map to 3D data using the accurately named texturemap value of the FaceColor property (original right?) as well as the appropriate CData (here a 2D image).
Simple example:
clear
clc
A = imread('peppers.png');
%// Generate dummy surface plot
[X,Y] = meshgrid([-2:.25:2]);
Z = X.*exp(-X.^2 -Y.^2);
surf(X,Y,Z,'CData',A,'FaceColor','texturemap')
It looks like this:
By default in a figure window you can zoom/move around as you want. For the 2nd part of your question, I think you should carefully read the docs for campos and related functions to get/set the camera position. Since building a GUI to perform that task requires much effort I think your best bet would be to try something on your own and ask questions about it here if you are stuck somewhere.
Hope that helps!

Related

How can I find the boundary surface in this image

I am new to image processing. I want to find the surface between black and white pixels which separates them. Here is the link of image.
The size of image is (21,900,900)
https://drive.google.com/file/d/1zUWK0Fb_n6f1JZou5mrUJq0x3h2X8mBK/view?usp=sharing
I tried to use boundarymask command of MATLAB in one plane of image but I am getting noise and also it works for 2d image only. Please suggest me how to find boundary 3d surface here. Thank you.
This is the output image after applying boundarymask.
Your first step should be to get rid of your noise. Since you got some kind of salt and pepper noise you can to that using the median filter on a 2D-image with medfilt2() in matlab. After that you can use an edge ditector to find your edge pixels. The code for this could look like this. If you want the surface, you need to loop this, over the 3rd dimension of your 3D-image. The code will look like this:
for ii=1:16
I=imread('image.tif',ii);
I_bs=boundarymask(I);
I_filt=medfilt2(I_bs,[7 7]);
boundarysurface(:,:,ii)=edge(I_filt,'Canny');
end
The edge detector I used here is certainly overkill for this easy case, but was the easiest thing I could think of in short term. If performance is relevant let me know, and I will give you another approach.

Change figure view in matlab

Is there some way of using the surf() style data format to create a heat map?
I have a bunch of scattered data of the form z=f(x,y) (so I used TriScatteredInterp to make it work with meshgrid) and I'd like to visualize z with a heat map. surf already does something similar to what I want, except you have to manually rotate the graph so the view is top down looking at the XY plane.
Basically, I'd like something similar to this:
But surf gives you this by default:
Although the answers here already show how to do this with surf, rendering the 3d surface feels a little like overkill...
pcolor creates the required images directly
(with slightly better results -surf has a gap next to the axes)
code
figure(1)
pcolor(peaks)
figure(2)
surf(peaks)
view(2)
results
pcolor
surf
Adding to Ben's answer, you can use the view command. view allows you to rotate your plot to whatever camera angle you wish.
In general, you call the command like so:
view(AZ, EL);
AZ is the azimuth or horizontal rotation, while EL is the vertical elevation. These are both in degrees.
In your case, once you plot your surf plot, use view(0, 90); before you go to the next subplot. view(0, 90); is the default 2-D view, and this looks directly overhead.
By doing this, you avoid having to rotate your plot manually, then using campos to determine what the camera position is at given your plot. view(0, 90); should give you what you need.
Sidenote
Doing view(2); also gives you the default 2D view, which is equal to view(0, 90); as we talked about. By doing view(3);, this gives you the default 3D view as seen in your plots. FWIW, the default azimuth and elevation for a 3D plot is AZ = -37.5, EL = 30, in degrees of course.
Rotate the view to what you want. Then type campos in the terminal. This shows you the camera position. You can then use campos( your_desired ) to set the camera position for future plots.
For example, the x, y view is usually:
campos([4.0000 2.5000 86.6025])

[MATLAB]: How would I mathematically and visually reproduce the 3D surface of the new King's Cross 'Western Concourse'?

Anyone have any starting tips for me? I want to learn from this (ie Don't want to be lazy and have someone answer this for me).
I would like to develop my understanding of mathematical 3D surfaces. My own personal project is to produce a 3D surface/graph of the concourse structure in MATLAB.
I found a link with good pictures of its geometry here. I am not expecting to get it 100% perfectly but I'd like to come close!
At the end of this exercise I would like to have a mathematical definition of the geometry as well as a visual representation of the surface. This can involve cartesian equations, parametric equations, matrices, etc.
Any help would be very much appreciated!
To give some specific advice for MATLAB:
I would load in the 'section' image from the web page you have linked, and display this in a MATLAB figure window. You can then try plotting lines over the top until you find one that fits nicely. So you might do something like:
A = imread('~/Desktop/1314019872-1244-n364-1000x707.jpg');
imshow(A)
hold on
axis on
%# my guess at the function - obviously not a good fit
x = [550:900];
plot(x, 0.0001*x.^2 + 300)
Of course, you might want to move the position of the origin or crop the picture and so on.
As an arguably better alternative to this trial-and-error method, you could trace the outline of the section (e.g by clicking points with something like ginput), and then use one of MATLAB's curve-fitting tools (e.g. fit) to fit a function to the data.
The final 3D shape looks to me (at a casual glance) to be a 3D revolution of the section shape around a central axis. Use of a cylindrical coordinate system could therefore be a good idea.
The final plotting of your 3D shape could be done with a function such as surf or mesh.
I would start by defining a function that defines for each x, y coordinate whether there is a point z, and if so with which altitude.
The shape reminds me a bit of a log or a square root.

Matlab: how to plot 3D text ( not 2D as function text does)

I know it is not a trivial task however I was wondering if anyone has already done this:
the text(x,y,z,'text') function plots a 2d text attached to the coordinate system of the 2d figure. What I need is a 3D text as a 3D entity which changes in accordance with the camera view.
Or if there is a way to extract the 2D data (x,y) of a 2D text and transform it ( rotate + translate) in 3D coordinate system.
Thanks a lot in advance
PS. I only have matlab basic version, with no toolbox in it.
You should be looking for [1].
1. http://www.mathworks.com/help/toolbox/mupad/plot/PRIMITIV_Text3d.html

matlab: correct approach to add geometrical objects to image

please help with Matlab beginner challenge
i need to create an image with few geometrical objects (circles,
ellipses) and then to apply some projective transforms
my problem is that i cant understand how to actually "draw" on image
image is AFAIU generally defined as [X;Y;3] matrix,
functions as SCIRCLE1 can compute/return collection of points
representing circle, but the problem is that points are not discrete ,
coordinates are real numbers and not pixels
how can i recompute the scircle output to be valid in image
coordinates system ? i.e. how can i "pixelize" it?
thanks for your attention, i really missing some basic concept and
will appreciate your help
John
well, below is an answer i received on Matlab newsgroups
BOTTOM LINE-no built-in way in Matlab
======================================
'getframe' can be used to merge axes even though it is more commonly used to create movie frames.
MATLAB is really weak in this area. There are some primitive
functions for drawing into the overlay (such as rectangle() if you
want to draw a circle, and line() if you want to draw a line) but no
real way that I know of to draw right into the underlying image. So
you have to use "tricks" such as getframe and then apply logical
operations. And you have to be careful with that since I think when
it gives you the rasterized version of the overlay it might be the
size of the image on the screen, not the true original matrix size of
the underlying image (I'd have to recheck this).
full thread here : http://www.mathworks.com.au/matlabcentral/newsreader/view_thread/261232
I found this example which give you a easy way to put simple geometrical object onto pictures.
Read the input image.
I = imread('cameraman.tif');
Define the rectangle dimensions as [x y width height].
rectangle = int32([10 10 30 30]);
Draw the rectangle and display the result.
J = step(shapeInserter, I, rectangle);
imshow(J);
see this link
by the way..
I didn't get you point about points not being discrete and images being a matrix. The way I see it. It much the same. you could try to explain it more in depth ?
The insertShape function in the Computer Vision System Toolbox is what you need. It lets you draw rectangles, circles, and polygons into the image.
There is also insertText, insertMarker, and insertObjectAnnotation.