Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make three-dimensional dynamic photo album with Matlab

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Matlab to make three-dimensional dynamic photo albums, I hope you will gain something after reading this article, let's discuss it together!

Effect.

Tutorial part 1 Image Import and resizing

You need to have a folder named album in the same folder as the current m file, and at least one picture in jpg format in the ablum folder.

Path='.\ album\';% folder name files=dir (fullfile (path,'*.jpg')); picNum=size (files,1); for i=1:picNum fileName=strcat (path,files (I) .name); img=imread (fileName); img=imresize (img, [120120]); imgSet {I} = img;end

We noticed that imresize was used here to change the breakthrough to 120x120 size, and here resizing has three functions:

Change a picture that is not a square into a square

Set the image to a fixed size to facilitate the construction of a grid to place the picture.

The size of 120x120 is about the smallest size on the premise that the picture can be expressed clearly. If the picture is too large, it will be stuck, and if it is too small, it will not be clear.

2 fig axes setting% fig axes setting fig=figure ('units','pixels','position', [50 50 600 600],... 'Numbertitle','off','resize','off',... 'name','album3d','menubar','none'); ax=axes (' parent',fig,'position', [- 0.5-0.522], 'XLim', [- 66],... 'YLim', [- 66],... 'ZLim', [- 66],... 'Visible','on',... 'XTick', [],... 'YTick', [],... 'Color', [0 000],... 'DataAspectRatioMode','manual',... 'CameraPositionMode','manual'); hold (ax,'on')

Most of the settings are understandable. Here are some of the rarer settings:

2.1 Why is the 'position' property of axes not set [0 0 1 1]?

Because it is a 3D axis, when it is set to [0 011], the rotation effect is like this, so what we need to set for axes is one circle larger than figure:

2.2 Why set the strange property CameraPositionMode?

Because we have to change the CameraPosition property frequently later, and setting CameraPositionMode to manual allows the viewing angle to be adjusted exactly according to the CameraPosition value, so why adjust the viewing angle?

Of course, it is because the amount of data will be large if we deal with the image position data, so we might as well directly turn the axes perspective instead of turning the picture.

3 draw graphics handle

Is to draw small cubes, medium cubes and large cubes, in which the medium cube becomes a large cube when the mouse moves over the center of the medium cube, which can be changed by setting the XData,YData,ZData value of the graphic object

3.1 construct the grid

Because the surf surface map can paste the image on it and set the transparency, we decided to use the surf function to draw. In order to map the surface, we must first construct the surface mesh:

% grid used to draw pictures [XMesh,YMesh] = meshgrid (linspace (- 1men1120), linspace (- 1mem1120)); ZMesh=ones (120120)

3.2 draw a small cube

% draw picture cube surfPic (1) = surf (XMesh,YMesh,ZMesh,'CData',imgSet {mod (0pics picNum) + 1}, 'EdgeColor','none','FaceColor','interp'); surfPic (2) = surf (XMesh,YMesh (end:-1:1,:),-ZMesh,'CData',imgSet {mod (1pics picNum) + 1},' EdgeColor','none','FaceColor','interp') SurfPic (3) = surf (ZMesh,XMesh,YMesh (end:-1:1,:), 'CData',imgSet {mod (2pics picNum) + 1},' EdgeColor','none','FaceColor','interp'); surfPic (4) = surf (XMesh,ZMesh,YMesh (end:-1:1,:), 'CData',imgSet {mod (3pics picNum) + 1},' EdgeColor','none','FaceColor','interp') SurfPic (5) = surf (- ZMesh,XMesh,YMesh (end:-1:1,:), 'CData',imgSet {mod (4 scene picNum) + 1},' EdgeColor','none','FaceColor','interp'); surfPic (6) = surf (XMesh,-ZMesh,YMesh (end:-1:1,:), 'CData',imgSet {mod (5 scene picNum) + 1},' EdgeColor','none','FaceColor','interp')

3.3 draw a medium cube

With small cubes, medium-sized ones are easy to draw, and can even be solved with a for loop. You only need to cycle through the XData,YData,ZData data of the small cube and multiply it by 1.5 to draw the image, and set the transparency:

% rely on small cube data to draw medium cube for iSURFIPIC (I) 6 surfPicA (I) = surf (surfPic (I) .XData. * 1.5 parcels surfPic (I) .YData. * 1.5 cores surfPic (I) .ZData. * 1.5 'CData',surfPic (I) .CData,' EdgeColor','none','FaceColor','interp','FaceAlpha',0.7); end

3.4 Parameter settings for large cubes

It is not so easy to set the parameters of a large cube. If you multiply it directly by 2.5, there will be no gap between the image and the image. Therefore, although our XData,YData,ZData data are all larger, they have to be multiplied by different values, and the best values are different in different directions, so we can set up a matrix in advance to store its parameters:

The matrix resizeMat= used to adjust the magnification [2 2 2.5 10 2 2 2 10 2 2 10 2 2 5 2 10 2 2 2 5 2]

If you want to draw a large square directly, try the following code:

% maximum picture rendering% for iPicture 1 6% surfPicB (I) = surf (surfPic (I) .XData. * resizeMat (iMagne1),...% surfPic (I) .YData. * resizeMat (iMagne2),...% surfPic (I) .ZData. * resizeMat (iMagne3),...% 'CData',surfPic (I) .CData,' EdgeColor' ...% 'none','FaceColor','interp','FaceAlpha',0.7) % end4 cube rotation

We just need to set a timer function to constantly adjust the CameraPosition:

Fps=40;theta=0;rotateTimer=timer ('ExecutionMode',' FixedRate', 'Period',1/fps,' TimerFcn', @ rotateCube); start (rotateTimer) function rotateCube (~, ~) theta=theta+0.02; ax.CameraPosition= [cos (theta) * 5*sqrt (2), sin (theta) * 5*sqrt (2), 5]; end

5 get the distance between the mouse and the center point

I originally wanted to write get (fig,'CurrentPoint') directly in the function called by timer to get the current position of the mouse, but I found that only when the mouse clicked on the window would there be a response, not when the mouse moved, so we constructed a WindowButtonMotionFcn callback,! This part of the code should be written in front of the previous code!

LastDis=300;preDis=300;set (fig,'WindowButtonMotionFcn',@move2center) function move2center (~, ~) xy=get (fig,'CurrentPoint'); preDis=sqrt (sum ((xy- [300300]). ^ 2)); end

PreDis is the location of the mouse to the center of the picture, why should I set a lastDis, because it is too stuck to update the image every time I move the mouse, so we need to add a decision, if and only if the picture size is updated in the following two cases

Before, the mouse was away from the center > = 150, now

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report