I am facing a problem doing some animation for a assignment in matlab
Let say that for instance I have a matrix 3D where the last index determine the color and the the others are determining the x,y,z coordinates.
a(:,:,1,1) =
0.9124 0.8790 0.8823
0.3242 0.7791 0.4257
0.2905 0.3944 0.4664
a(:,:,2,1) =
0.4249 0.0956 0.4965
0.4552 0.7335 0.2597
0.6954 0.1300 0.5917
a(:,:,3,1) =
0.2276 0.1832 0.1372
0.9551 0.6242 0.1889
0.0630 0.2914 0.9566
a(:,:,1,2) =
0.2966 0.0043 0.2240
0.2372 0.0782 0.6953
0.6602 0.3096 0.7002
a(:,:,2,2) =
0.8518 0.5309 0.3834
0.5591 0.8589 0.5954
0.5703 0.4463 0.3050
a(:,:,3,2) =
0.1011 0.6432 0.6211
0.3719 0.7767 0.2791
0.2222 0.4300 0.4780
a(:,:,1,3) =
0.3147 0.1443 0.7440
0.8272 0.0683 0.8357
0.7432 0.5321 0.7207
a(:,:,2,3) =
0.8876 0.8820 0.7249
0.1629 0.4620 0.8836
0.2012 0.1870 0.7980
a(:,:,3,3) =
0.8430 0.5304 0.7167
0.5380 0.8433 0.8627
0.2096 0.2153 0.4713
Now I need a 3D image where all the point should be like the result we get when we use imagesc matlab command.
To display slice #3 (z=3):
zind = 3;
imagesc(squeeze(a(:,:,zind,:)))
Related
In the script below I create a mask based on coordinates and plot them in the original position and also starting at position 0,0. How can I plot another region mask (mask1venter) center?
Code:
xCoord = [354 500 100 363];
yCoord = [309 500 600 360];
if max(xCoord)>max(yCoord)
matrixLength = max(xCoord);
else
matrixLength = max(yCoord);
end
xCoordMin = xCoord-min(xCoord);
yCoordMin = yCoord-min(yCoord);
xCoordCenter = xCoord-round((max(xCoord))/2);
yCoordCenter = yCoord-round((max(yCoord))/2);
mask1 = poly2mask(yCoord,xCoord,matrixLength,matrixLength);
mask1Min = poly2mask(yCoordMin,xCoordMin,matrixLength,matrixLength);
mask1Center = poly2mask(yCoordCenter,xCoordCenter,matrixLength,matrixLength);
imshowpair(mask1,mask1Min)
You can either use subplot, add the two masks or logical OR the two masks
Subplot
figure
subplot(2,2,1)
imshow(mask1)
subplot(2,2,2)
imshow(mask1Min)
subplot(2,2,3)
imshow(mask1Center)
Add the two image
figure
imshowpair(mask1,mask1Min + mask1Center)
Logical OR the two masks
figure
imshowpair(mask1,mask1Min | mask1Center)
I build an alpha shape from some points (example given in code) and want to export the shape to a raster graphics format. I need the shape only, not the plot markings (axis, scales ect).
I need only the resulting triangle on white ground as a bitmap.
Scale needs to be 1 unit = 1 pixel.
x = [0 10 20 30 30 30 15];
y = [0 0 0 0 15 30 15];
shape = alphaShape (x',y');
plot (shape, 'FaceColor', 'black');
I have not found anything on how to export shapes or how to rasterize them. Is there any way to do that?
Run the following code after yours.
imgwidth = max(1, ceil(max(x) - min(x)));
imgheight = max(1, ceil(max(y) - min(y)));
ax = gca;
ax.Visible = 'off';
ax.XTickMode = 'manual';
ax.YTickMode = 'manual';
ax.ZTickMode = 'manual';
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
ax.ZLimMode = 'manual';
ax.Position = ax.OuterPosition;
af = gcf;
figpos = getpixelposition(af);
resolution=get(0, 'ScreenPixelsPerInch');
set(af, 'paperunits','inches', ....
'papersize',[imgwidth imgheight]/resolution, ....
'paperposition',[0 0 [imgwidth imgheight]/resolution]);
print(af,'out.png','-dpng',['-r',num2str(resolution)],'-opengl')
Things done:
Fetch data range and convert to image dimensions.
Turn off axes and ticks.
Minimize/remove padding space surrounding the actual content.
Map 1 unit in data into 1 pixel in output image.
Things not done:
Guarantee aspect ratio. (should work, though)
This screenshot shows non-unity aspect ratio output:
References
Mathworks - Save Figure at Specific Size and Resolution
MATLAB Central - saving a figure at a set resolution
Mathworks - print
Mathworks - Save Figure with Minimal White Space
i have some datas which are included some deflection and force values. Like deltaX deltaY and forces measured at that points as Fx and Fy. I want to create a colormap at that points with the magnitude of forces and color transition between points in 2D. For example if point1 is red(high value, big deflection) and point2 is blue(low value, small deflection) i want color transition between them. Do you have any suggestion for that?
Data are given below.
First column positionX
second column positionY
third column forceX
forth column forceY
I need to plot this map according to X and Y positions and force magnitudes.
***When i take the magnitude of vectors we have positionX positionY and only 1 force value.
filein =
0 0 -0.0395 0.1189
0 1.5053 0.2127 -11.3568
-0.0008 3.0082 0.6719 -22.0470
-0.0048 4.5093 0.9231 -32.7004
0.0069 6.0033 1.2499 -43.2750
-0.0029 7.5008 1.6960 -53.4941
1.4981 0.0102 -1.5213 1.2031
1.4979 1.5003 -1.2326 -10.0738
1.5071 3.0043 -0.6965 -20.7386
1.4896 4.4943 -0.2563 -31.5026
1.5020 5.9921 0.0480 -42.3186
1.5021 7.4909 0.7614 -52.7354
3.0016 0.0022 -2.6099 1.9455
3.0022 1.4960 -2.6157 -9.3388
2.9959 3.0087 -1.8898 -20.1823
2.9955 4.4977 -1.3670 -30.7842
2.9923 6.0041 -0.8444 -41.7370
2.9976 7.5055 -0.2241 -52.1361
4.4995 -0.0016 -4.0576 2.5489
4.5009 1.4961 -3.8135 -8.6871
4.4930 2.9939 -3.0315 -19.4825
4.4986 4.5045 -2.6034 -30.2974
4.5046 5.9931 -1.9570 -40.9145
4.4972 7.5023 -1.1994 -51.4071
5.9931 -0.0014 -5.1986 3.2395
5.9954 1.5000 -5.1224 -7.9289
6.0017 2.9977 -4.3153 -18.7471
6.0045 4.4939 -3.6613 -29.4662
6.0030 6.0081 -2.9086 -40.3400
6.0003 7.5006 -2.1704 -50.6973
7.4974 -0.0018 -6.5690 4.0048
7.4977 1.5043 -6.5230 -7.0994
7.5047 3.0058 -5.5833 -18.0435
7.5083 4.5058 -4.8070 -28.6861
7.5024 6.0059 -4.0150 -39.4321
7.5006 7.5023 -3.1837 -49.8617
You can use meshgrid and surf or mesh command to plot it:
[X,Y] = meshgrid(filein(:,1),filein(:,2));
M = sqrt(filein(:,3).^2+filein(:,4).^2);
Z=meshgrid(M,M);
C = gradient(Z);
figure
surf(X,Y,Z,C);colorbar
You can remove the mesh and interpolate as follows:
surf(X, Y, Z,'EdgeColor', 'None', 'facecolor', 'interp');
Then view it from above:
view(2)
I have a movie file, in which I am interested in recording the movement of a point; center of a circular feature to be specific. I am trying to perform this using edge detection and corner detection techniques in Matlab.
To perform this, how do I specify a region of interest in the video? Is subplot a good idea?
I was trying to perform this using the binary masks as below,
hVideoSrc = vision.VideoFileReader('video.avi','ImageColorSpace', 'Intensity');
hEdge = vision.EdgeDetector('Method', 'Prewitt','ThresholdSource', 'Property','Threshold', 15/256, 'EdgeThinning', true);
hAB = vision.AlphaBlender('Operation', 'Highlight selected pixels');
WindowSize = [190 150];
hVideoOrig = vision.VideoPlayer('Name', 'Original');
hVideoOrig.Position = [10 hVideoOrig.Position(2) WindowSize];
hVideoEdges = vision.VideoPlayer('Name', 'Edges');
hVideoEdges.Position = [210 hVideoOrig.Position(2) WindowSize];
hVideoOverlay = vision.VideoPlayer('Name', 'Overlay');
hVideoOverlay.Position = [410 hVideoOrig.Position(2) WindowSize];
c = [123 123 170 170];
r = [160 210 210 160];
m = 480; % height of pout image
n = 720; % width of pout image
BW = ~poly2mask(c,r,m,n);
while ~isDone(hVideoSrc)
dummy_frame = step(hVideoSrc) > 0.5; % Read input video
frame = dummy_frame-BW;
edges = step(hEdge, frame);
composite = step(hAB, frame, edges); % AlphaBlender
step(hVideoOrig, frame); % Display original
step(hVideoEdges, edges); % Display edges
step(hVideoOverlay, composite); % Display edges overlayed
end
release(hVideoSrc);
but it turns out that the mask applied on frame is good only for the original video. The edge detection algorithm detects the edges those are masked by binary mask. How can I mask other features permanently and perform edge detection?
Is this what you mean?
BW = poly2mask(c,r,m,n);
frame = dummy_frame .* BW;
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