How can I create a camera fly-through effect in MATLAB? - matlab

I would like to implement the fly-through effect as shown in this nice example. First I created a figure from matrix hm, a 300X300 matrix by using the surf function:
surf(hm);
Then, I defined an animated line from variables x, y, and z and displayed on the figure as follows:
curve = animatedline;
curve.LineWidth = 6;
curve.Color = [ 1 0 1];
for i = 1:length(x)
addpoints(curve, x(i), y(i), z(i));
drawnow;
end
Then, I wanted to implement the fly-through effect so that the camera will move along the line. I tried this code piece I took from the example above and I slightly modified it:
for i = 1:length(x)
campos([(x(i) -5), (y(i)-5), 0]);
camtarget([x(i), y(i), z(i)]);
drawnow;
end
But the camera doesn't move as I intended. What am I doing wrong?

If you want to mimic the behavior of the linked example, you need to have both the camera target and camera position moving along your curve defined by (x, y, z). The way you've written it above, the camera target moves along the curve, but the camera position is always offset from the target by (-5, -5) in the xy plane and sitting at z = 0. If you want the camera to follow the curve behind the target, you should try something like this:
for iPoint = 6:numel(x)
campos([x(iPoint-5) y(iPoint-5) z(iPoint-5)]); % Note the index is shifted, not the value
camtarget([x(iPoint) y(iPoint) z(iPoint)]);
drawnow;
end
If you don't want the camera moving along the same curve, and instead want it to always be at a fixed offset from your moving camera target, you could try this:
offset = [-5 -5 0]; % X, Y, and Z offset from target
for iPoint = 1:numel(x)
campos([x(iPoint)+offset(1) y(iPoint)+offset(2) z(iPoint)+offset(3)]);
camtarget([x(iPoint) y(iPoint) z(iPoint)]);
drawnow;
end
Finally, if you'd like to control the speed of the animation, you can replace the drawnow command with a call to pause. Note that a call to pause is equivalent to a call to drawnow in that it will force an update of graphics objects. You can also animate graphics using a timer object, as I illustrate in this answer.

Related

Dot moves from corner to corner (around monitor) in matlab

I am trying to do create a ball or dot or whatever that goes from left bottom corner to right bottom corner then top right corner then left top corner then on the start so left bottom corner. Basically around the screen. And in total fullscreen that important. For fullscreen graph i am using WindowAPI.
So here is my code.
try
% Create a figure to operate on: --------------------------------------------
% The OpenGL renderer is confused by the alpha blending, so Painters is used:
FigH = figure('Color', ones(1, 3), 'Renderer', 'Painters');
axes('Visible', 'off', 'Units', 'normalized', 'Position', [0, 0, 1, 1]);
% Set topmost status:
WindowAPI(FigH, 'topmost'); % Command is not case-sensitive
drawnow;
WindowAPI(FigH, 'TopMost', 0);
drawnow;
WindowAPI(FigH, 'front');
drawnow;
% Nicer to have the figure on topmost for the rest of the demo:
WindowAPI(FigH, 'topmost');
% Special maximizing such that the inner figure fill the screen:
WindowAPI(FigH, 'Position', 'full'); % Complete monitor
% START MOVING BALL
X = 2;
Y = 0;
for i=1:1490
X = X + 0.1;
Y = 2
plot(X,Y,'or','MarkerSize',20,'MarkerFaceColor','r')
axis([0 151 0 85])
pause(0)
end
% END MOVING BALL
end
For simplicity that dot goes only from left to right bottom corner.
But there are two problems.
that dot lag sometimes which is a problem.
There are visible black lines (from the graph).
And I don't know how to fix these two problems. So if u know how to fix them or any better way how to animate ball in Matlab please post it here. Thanks for your time.
To reduce the amount of 'Lag' you can do a few of the following:
Move the plot line outside of the loop and assign it to a handle, additionally move the axis commandos out with it
pH = plot(X,Y,'or','MarkerSize',20,'MarkerFaceColor','r');
axis([0 151 0 85])
axis off
Now you can update the X Y data directly in the loop.
set(pH,'XData',X,'YData',Y)
Replace the pause with (replace 0 with the length in seconds that you want to delay the frame by):
tic;while toc<0;end
This is faster, more precise, and does not cause an interrupt to MatLab.
Add the drawnow command in the loop to cause the figures to update.

Is there a way to control distortion in Matlab's 3D viewer?

The background of this problem relates to my attempt to combine output from a ray tracer with Matlab's 3d plotters. When doing ray tracing, there is no need to apply a perspective transformation to the rendered image. You see this in the image below. Basically, the intersections of the rays with the viewport will automatically adjust for the perspective scaling.
Suppose I've gone and created a ray-traced image (so I am given my camera, my focal length, viewport dimensions, etc.). How do I create exactly the same view in Matlab's 3d plotting environment?
Here is an example:
clear
close all
evec = [0 200 300]; % Camera position
recw = 200; % cm width of box
recl = 200; % cm length of box
h = 150; % cm height of box
% Create the front face rectangle
front = zeros(3,5);
front(:,1) = [-recw/2; 0; -recl/2];
front(:,2) = [recw/2; 0; -recl/2];
front(:,3) = [recw/2; h; -recl/2];
front(:,4) = [-recw/2; h; -recl/2];
front(:,5) = front(:,1);
% Back face rectangle
back = zeros(3,5);
back(:,1) = [-recw/2; 0; recl/2];
back(:,2) = [recw/2; 0; recl/2];
back(:,3) = [recw/2; h; recl/2];
back(:,4) = [-recw/2; h; recl/2];
back(:,5) = back(:,1);
% Plot the world view
figure(1);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
plot3(evec(1), evec(2), evec(3), 'bo');
xlabel('x'); ylabel('y'); zlabel('z');
title('world view'); view([-30 40]);
% Plot the camera view
figure(2);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
xlabel('x'); ylabel('y'); zlabel('z');
title('Camera view');
campos(evec);
camup([0 1 0]); % Up vector is y+
camproj('perspective');
camtarget([evec(1), evec(2), 0]);
title('camera view');
Now you see the world view
and the camera view
I know how to adjust the camera position, the camera view angle, and orientation to match the output from my ray tracer. However, I do not know how to adjust Matlab's built-in perspective command
camproj('perspective')
for different distortions.
Note: within the documentation, there is the viewmtx command, which allows you to output a transformation matrix corresponding to a perspective distortion of a certain angle. This is not quite what I want. I want to do things in 3D and through Matlab's OpenGL viewer. In essence, I want a command like
camproj('perspective', distortionamount)
so I can match up the amount of distortion in Matlab's viewer with the distortion from the ray tracer. If you use the viewmtx command to create the 2D projections, you will not be able to use patch' orsurf' and keep colours and faces intact.
The MATLAB perspective projection works just like your raytracer. You don't need any transformation matrices to it use it. Perspective distortion is determined entirely by the camera position and direction of projection.
In the terminology of the raytracer diagram above, if the CameraPosition matches your raytracer's pinhole coordinates and the vector between CameraPosition and CameraTarget is perpendicular to your raytracer's viewport, the perspective distortion will also match. The rest is just scaling and alignment.

How to draw multiple shapes using Matlab vision toolbox and step function?

I'm trying to insert 2 shapes (circle and rectangle) to an image using these functions. But I'm unable to do that.
Here's my code
J = step(shapeInserter, I, bbox); %bbox is rectangle which is already defined
J = step(shapeInserter, I, circle); %circle is circle which is already defined
imwrite(J,'image.jpg','jpg'); % it draws only the circle
I have a long way which is to save the rectange image then load again to draw the circle and resave. Which i wish to avoid as it's really time consuming.
I'm trying to do something like this (similar to the plotting graph function)
hold on
%draw circle
%draw rectangle
hold off
imwrite(J,'image.jpg','jpg');
Please advise, thanks
The vision.ShapeInserter object has a property Shape, which can either be set to
'Rectangles'
'Circles'
'Lines'
'Polygons'
By default, it is set to 'Rectangles'. To use the same ShapeInserter object to place a circle, you will have to release it first by calling release(shapeInserter); and modifying the Shape property by set(shapeInserter,'Shape','Circles'). Then you can call the step method again to insert the circle.
Here is a small example:
I = imread('cameraman.tif');
rectangle = int32([10,10,50,60]);
circle = int32([200,200,40]);
shapeInserter = vision.ShapeInserter('Fill',true);
J = step(shapeInserter,I,rectangle);
release(shapeInserter);
set(shapeInserter,'Shape','Circles');
K = step(shapeInserter,J,circle);
imshow(K);
I'm a total noob to Matlab Image stuff but here is some very strait forward way of doing it :
frame=imread( '/home/omido/DeepLearning/matlab_segmentation/track/track/Image2.jpg' );
result = insertShape(frame, 'FilledRectangle', [100,100,100,100], 'Color', 'green');
result = insertShape(result, 'FilledRectangle', [200,200,200,200] , 'Color', 'yellow');
imshow(result);
and this is the result:
and as in the previous answer there are multiple shapes, you can find them here.

Lighting Stays with Animated Surface in Matlab

I'm attempting to animate a rotating sphere in Matlab, however the lighting on the sphere rotates with it. I instead want the sphere to rotate while the lighting remains fixed with the coordinate system. Here's a gif of what my code is currently producing: Animation. And here is my code:
% Simulation Time
dt = 0.05;
time = 0:dt:5;
% Prep Figure
figure('Color',[1 1 1],'Renderer','zbuffer','ColorMap', [1,0,0; 0,0,1])
% Generate Sphere
[X,Y,Z] = sphere(20);
r = 0.75*25.4;
h = surf(r*X,r*Y,r*Z,Z,'FaceColor','interp');
hold on
% Adjust Axes, Lighting, and Shading
axis equal
view([40 25]);
light('Position',[1 1 1])
set(findobj(gca,'type','surface'),...
'FaceLighting','phong',...
'AmbientStrength',.3,'DiffuseStrength',.8,...
'SpecularStrength',.9,'SpecularExponent',25,...
'BackFaceLighting','unlit','EdgeColor','k')
filename = 'Rotation.gif';
for n = 1:36
rotate(h,[0 0 1],10,[0 0 0])
im = frame2im(getframe(1));
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',dt);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',dt);
end
end
As already mentioned in the comments:
Seems like it might be an issue with the surface VertexNormals not
updating.
The solution was to download the rotate.m function fixed File Exchange submission.
The description:
Bug evidence:
[x,y,z] = sphere(20);
hs=surf(x,y,z,'facecolor','y');
view(2)
axis equal
hl=light;
lightangle(hl,0,0)
% light is on -Y axis, thus at the
% bottom
rotate(hs,[0 0 1],30)
% rotate sphere to the right from 30°
It looks like the light has moved. This is due to a bug in rotate.m
function. The "VertexNormals" property of the surf object is not
updated as "xdata", "ydata" and "zdata" properties are.
This is fixed in the submitted version of rotate.m.

Moving point along a curve (3D-Animation plots)

I am trying to make an animation of the trajectory (circular orbit of 7000 km altitude) of a satellite orbiting the Earth. The following vectors x,y,z represents the coordinates of it (obtained integrating the acceleration due to the nonspherical gravitational potential) in the reference system.
fh = figure('DoubleBuffer','on');
ah = axes('Parent',fh,'Units','normalized','Position',[0 0 1 1],...
'DataAspectRatio',[1 1 1],'DrawMode','fast');
x = 1.0e+003 * [ 1.293687086462776 1.355010603320554 ...
1.416226136451621 1.477328806662750 1.538313743926646...
1.841302933101510 2.140623861743577 2.435680048370655...
2.725883985836056 3.830393161542639 4.812047393962632...
5.639553477924236 6.285935904692739 6.778445814703028...
6.981534839226300 6.886918327688911 6.496619397538814...
5.886899070860056 5.061708852126299 4.051251943168882...
2.891621923700204 1.551975259009857 0.148687346809817...
-1.259946709379085 -2.614876359324573 -3.789635985368149...
-4.822735075152957 -5.675398819678173 -6.314344260262741...
-6.725008970265510 -6.860046738669579 -6.714044347581475...
-6.291232549137548 -5.646225528669501 -4.790489239458692...
-3.756316068441812 -2.581710448683235 -1.257064527234605...
0.118190083177733 1.488198207705392 2.797262268588749...
3.943218990855596 4.943060241667732 5.760107224604901...
6.363435161221018 6.741208871652011 6.844507242544970...
6.669637491855506 6.222229021788314 5.549112743364572...
4.665587166679964 3.605338508383659 2.407805301565781...
1.076891826523990 -0.297413079432155 -1.658804233546807...
-2.950960371016551 -4.105336427038419 -5.093651475630134...
-5.875676956725480 -6.417825276834068 -6.694317613708315...
-6.702354075060146 -6.441476385534835 -5.920328191821120...
-5.149356931765655 -4.165756794143557 -3.010476122311884...
-1.730623521107957 -0.547981318845428 0.651933236927557...
1.830754553013015 2.950797411065132];
y = 1.0e+003 *[ -6.879416537989226 -6.867600717396513...
-6.855237614338527 -6.842328214064634 -6.828873545169439...
-6.753459997528374 -6.664593892931937 -6.562452270514113...
-6.447238135027323 -5.857768973060929 -5.080802144227667...
-4.141502963266585 -3.069449548231363 -1.712593819793112...
-0.283073212084787 1.157789207734001 2.547934226666446...
3.733185664633135 4.781256997101091 5.653507474532885...
6.316540958291930 6.760480121739906 6.924451844039825...
6.801366712306432 6.393950562012035 5.763652137956600...
4.918852380803697 3.890903548710424 2.717191733101876...
1.385839187748386 -0.001786735280855 -1.388680800030854...
-2.717513794724399 -3.877348086956174 -4.892062889940518...
-5.723943344458780 -6.341064412332522 -6.729295147896739...
-6.844976271597333 -6.684181367561298 -6.252308741323985...
-5.600523241569850 -4.741636145151388 -3.707934368103928...
-2.537101251915556 -1.208445066639178 0.169057351189467...
1.539102816836380 2.845512534980855 3.993289528709769...
4.989150886098799 5.795183343929699 6.379362665363127...
6.723976759736427 6.794165677259719 6.586864956951024...
6.108394444576384 5.387403581100790 4.449452017586583...
3.332306147336086 2.080126804848620 0.757432563194591...
-0.595089763589023 -1.923045482863719 -3.172486599444496...
-4.302442851663575 -5.254127434062967 -5.988250483410006...
-6.472859710456819 -6.675113607083117 -6.664054266658221...
-6.440275312105615 -6.010308893159839];
z = [ -1.348762314964606 -1.416465504571016 -1.484053975854905...
-1.551522350691171 -1.618865254528658 -1.953510294130345...
-2.284215283426580 -2.610320163346533 -2.931177500785390...
-4.153679292291825 -5.242464339076090 -6.162825517200489...
-6.884797354552217 -7.440577139596716 -7.680358197465111...
-7.594616346122523 -7.183952381870657 -6.529293328494871...
-5.637062917332294 -4.540678277777376 -3.279180600545935...
-1.817413221203883 -0.280548741687378 1.268253040429052...
2.764251377698321 4.066975661566477 5.218214283582148...
6.174673504642019 6.899157495671121 7.375688520371054...
7.548875108319217 7.410793523141250 6.965068314483629...
6.271309946313485 5.343254095742233 4.215431448848456...
2.928028129903598 1.469574073877195 -0.048649548535536...
-1.563638474934283 -3.013536101911645 -4.285161526803897...
-5.397128342069014 -6.308837263463213 -6.985946890567337...
-7.415475222950275 -7.542406523585701 -7.363021555333582...
-6.884639818710263 -6.158276823110702 -5.199186592259776...
-4.043958234344444 -2.736923814690622 -1.283388986878655...
0.219908617803070 1.712828428793243 3.135072606759898...
4.411790351254605 5.510842969067953 6.387336537361380...
7.004133661144990 7.332163450286972 7.366696289243980...
7.105258174916579 6.555393588532904 5.727091807637045...
4.660073989309112 3.399622357708514 1.999243120787114...
0.701744421660999 -0.620073499615723 -1.923270654698332...
-3.164705887374677 ];
load('topo.mat','topo','topomap1');
[x1,y1,z1] = sphere(50);
x1 = 6678.14*x1;
y1 = 6678.14*y1;
z1 = 6678.14*z1;
props.AmbientStrength = 0.1;
props.DiffuseStrength = 1;
props.SpecularColorReflectance = .5;
props.SpecularExponent = 20;
props.SpecularStrength = 1;
props.FaceColor= 'texture';
props.EdgeColor = 'none';
props.FaceLighting = 'phong';
props.Cdata = topo;
surface(x1,y1,z1,props);
light('position',[-1 0 1]);
light('position',[-1.5 0.5 -0.5], 'color', [.6 .2 .2]);
view(3);
handles.p1 = line('parent',ah,'XData',x(1),'YData',y(1),'ZData',...
z(1),'Color','red','LineWidth',2);
handles.p2 = line('parent',ah,'XData',x(end),'YData',y(end),...
'ZData',z(end),'Marker','o','MarkerSize',6,'MarkerFaceColor','b');
oaxes([0 0 0],'Arrow','extend','AxisLabelLocation','side',...
'Xcolor','green','Ycolor','green','Zcolor','green');
axis vis3d equal;
handles.XLim = get(gca,'XLim');
handles.YLim = get(gca,'YLim');
handles.ZLim = get(gca,'ZLim');
set([handles.p1,handles.p2],'Visible','off');
xmin = handles.XLim(1);
ymin = handles.YLim(1);
zmin = handles.ZLim(1);
xmax = handles.XLim(2);
ymax = handles.YLim(2);
zmax = handles.ZLim(2);
set(ah, 'XLim', [xmin xmax],'YLim', [ymin ymax],'Zlim',[zmin zmax]);
view(3);
handles.hsat = line('parent',ah,'XData',x(1), 'YData',y(1),...
'ZData',z(1),'Marker','o', 'MarkerSize',6,'MarkerFaceColor','b');
k = uint8(2);
u2 = uint8(length(x));
while k<u2
handles.htray(k) = line([x(k-1) x(k)],[y(k-1) y(k)],[z(k-1) z(k)],...
'Color','red','LineWidth',3);
set(handles.hsat,'XData',x(k),'YData',y(k),'ZData',z(k));
drawnow;
k = k + 1;
end
where oaxes is a FEX application that allows getting an axes located (in this case) at the origin (0,0,0) of the PlotBox.
I have read the User Guide's Graphics section in the Matlab Help Browser. It recommends to use low-level functions for speeding the graphics output (this is the reason for which I use the line function instead of plot3) and the renderer painters for line graphics. In my case, I can not use it because I have a surface (the Earth) which is not well drawn by it. I want to get something similar to this (I have tried to get in touch with the author but I have not got response). The final result is a slow (it takes 11.4 seconds in my computer with microprocessor intel core i5) and discontinuous animation (perhaps I need more points to get the blue point's movement looks like continuous but the integrator's output points are invariable). I would like to know what I should make to improve it. Thank you for your attention. Cheers.
A couple of things here.
DrawMode=fast probably doesn't do what you think it does. It's turning off depthsorting. I think that you really want depthsorting here.
You're creating line objects in the inner loop. You really want create a small number of graphics objects and reuse them. Could you create a single line object and set the XData, YData, & ZData, in the loop?
You can use hgtransform to avoid modifying the coordinates of hsat (as described here), but that would only make a difference if hsat was much more complex. I don't think it would buy you anything in this case.
You could reduce the resolution of your surface.
You probably want to set the figure's Renderer property to OpenGL.
In this case, but I'm getting almost 20 frames per second on my system with your code. After making those changes, I'm getting about 100 frames per second. What sort of framerate are you shooting for here?
I believe the main reason your animation is slow is because you are using the Phong lighting algorithm which is computationally expensive. To see the effect it has on performance, try specifying Gouraud shading instead:
%#lighting('gouraud');
props.FaceLighting = 'gouraud'; %# faster interpolating method