I am using the command WARP in image processing in MATLAB.
[x,y,z] = cylinder;
I = imread('testpat1.png');
warp(x,y,z,I);
Above is the example code for using WARP given in MATLAB. But I am not able to save the output of this command. If I do imwrite, just 1 X 1 matrix is saved.
Can anyone help me this ?
Thanks in advance
You should be able to use the following line of code to get the handle to the resulting surface object:
h = warp(x, y, z, I);
You can then access properties of that surface using get(h, 'property')
A list of the available properties is here
For example if you want to get the X coordinates you would do: Xcoords = get(h, 'XData');
hope that helps!
This is based on the comments below.
fig = figure, warp(x, y, z, I);
print(fig, '-r80','-dtiff','image2.tif')
Related
i'm currently trying to plot this function:
Z^2 = (X.^2+Y.^2+2*w.*Z.*Y.*a)./(1-w^2*a^2)
Geogebra gives a lightcone https://en.wikipedia.org/wiki/Light_cone but crushes if i change the parameters a bit. I tried then matlab:
[X,Y] = meshgrid(-10:.5:10);
a=2;
w=1;
Z = (X.^2+Y.^2+2*w.*sqrt(abs(Z)).*Y.*a)./(1-w^2*a^2);
surf(X,Y,Z)
zlim([-5,5])
And it has too few points. I wish i could add some changing meshgrid, like (-5:.1:5), but it gives:
Arrays have incompatible sizes for this operation.
Probably due to sqrt(abs(Z)) in the equation. I don't know how to fix it.
Thanks
It's easier to directly generate the cone data with command cylinder
t = 0:pi/10:2*pi;
r =linspace(2,0,numel(t))
[X,Y,Z] = cylinder(r);
figure
hs1=surf(X,Y,Z)
Note that I have added the handle hs1 which is output of surf.
This way you can change any property of the generated cone surface explained in detail here :
https://uk.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.surface-properties.html
I have a imagesc image in which each pixel represent a data vector. The image itself is from a data cube squeezed into 2D matrix. I can use impixelinfo to navigate through the image and get pixel coordinates when inquiring the image. The code to execute this is below,
load data.mat; % data cube of size 512x256x12000
figure; imagesc(squeeze(mean(abs(data),3)))
axis equal; colormap jet;
impixelinfo
What I want to do is to be able to plot the underlying data vector (laying into the 3rd dimension) in a second figure using the pixel coordinates. This second figure should update automatically when I move the cursor in the image.
Any help is highly appreciated. Thank you in advance.
Thanks guys, I found a solution with ButtonDownFcn and posting it here for folks out there with a similar problem.
x = (-10:10); y = x; z = x;
[mx, my, mz] = ndgrid(x,y,z)
r = sqrt(mx.^2 + my.^2 + mz.^2);
figure;
imagesc(squeeze(r(:,:,1)),'ButtonDownFcn', {#test_func,r});
with the following function
function test_func(hObject, eventdata, r);
P = get(gca,'CurrentPoint');
X = round(P(1,1));Y = round(P(1,2));
figure;plot(squeeze(r(X,Y,:)));
end
My question is similar to the post: matlab curve with label
I have some data (acquired using a function too long to show here), which gives me 2 arrays: Nv4 (337x1) and t (337x1) and I want to plot 'a=40' on the plot line.
I should be able to use contour label, but I need to convert my data as matrix format first. The post above gives a link to explain how to convert our data, unfortunately the link has expired and I have no idea how I should convert my data to. An example would be useful !
I'm posting this as a new question because I don't have enough reputation to post a comment
I guess there is another way, simply with text. Here is a sample:
% Create a sample curve
x = 1:337;
y = sqrt(x);
plot(x,y);
% Define position to display the text
i = round(numel(x)/2);
% Get the local slope
d = (y(i+1)-y(i))/(x(i+1)-x(i));
X = diff(get(gca, 'xlim'));
Y = diff(get(gca, 'ylim'));
p = pbaspect;
a = atan(d*p(2)*X/p(1)/Y)*180/pi;
% Display the text
text(x(i), y(i), 'a=40', 'BackgroundColor', 'w', 'rotation', a);
And here is the result:
Best,
So I have a simple loop in MATLAB that does the following:
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
figure(1)
plot(randn(1,100));
figure(2);
plot(randn(1,100));
end
The x and y are made up, but that is the jist of it. Anyway, when I run this code, not surprisingly, MATLAB will make two figures and plot accordingly. The problem is, I get a sort of 'blinking' between figures when I do this, and it makes the quality of seeing x and y evolve over time poorer.
I discovered a way to make one of the plots smoother like this:
figure(1);
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
plot(randn(1,100));
drawnow
end
If I do this, then of course figure(1) will plot very smoothly showing x nicely, without figure(1) 'blinking' between plots, but now I cant show figure(2) or y!
How can I plot both those quantities on different figures (not subplots) smoothly without 'blinking'?
EDIT:
Thanks Geodesic for your answer, the solution works, however there is a subtlety that I did not think would be an issue, however it is.
1) I am unable to use 'imagesc' with this solution.
For example,
figure(1);
aone = axes;
figure(2);
atwo = axes;
for p = 1:100
x = 4.*randn(1,100);
y = 7.*rand(10,100);
plot(aone,x);
drawnow;
imagesc(atwo,y);
drawnow;
end
In this case the part with imagesc(atwo, y) crashes.
Your flicker is because you're generating each figure window again and again through the loop, which is forcing the window to come to the foreground each time. Generate the figures first, attach some axes to them, and plot your data to each axis like so:
figure(1);
aone = axes;
figure(2);
atwo = axes;
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
plot(aone,randn(1,100));
drawnow;
imagesc(y,'Parent',atwo);
drawnow;
end
Edit: functions like plot take an axis argument directly, but imagesc does not. In this particular case you'll need to send a Property Name/Value pair in as an argument. The 'Parent' of the image generated will be our axis atwo (see above).
For p = 1, create the plots you need, using the plot command or the imagesc command. Keep the handle of the resulting graphics object by getting an output argument: for example h = plot(.... or h = imagesc(..... This will be a Handle Graphics lineseries or image object, or something else, depending on the particular plot type you create.
For p = 2:100, don't use the plotting commands directly, but instead update the relevant Data properties of the original Handle Graphics object h. For example, for a lineseries object resulting from a plot command, set its XData and YData properties to the new data. For an image object resulting from an imagesc command, set its CData property to the new image.
If necessary, call drawnow after updating to force a flush of the graphics queue.
I try to read a text files with 20x20 data into variable C, and attempt to plot a histogram on the left Y-axes, and a ecdf/ksdensity on the right X-axes.
Using textscan, the data is read into a 400x1 array. However, when I tried to call plotyy to plot histogram, the code belows return
Error using plot
Vectors must be the same lengths.
Error in pt (line 11)
axes = plotyy(x,C{1},x,C{1});
I guess it is due to C{1}. But have no clue on how to resolve it. I am new to matlab, would anyone point out the correct way to perform the above operations?
fid = fopen('t1_error.txt','r');
C = textscan(fid, '%.3f');
fclose(fid);
disp(C{1});
x = -1:7; % <-- change to x = length(C{1}); then histogram is plotted.
axes = plotyy(x,C{1},x,C{1});
hold on
hist(axes(1), C{1}, x);
ylim(axes(1),'auto');
set(axes(1),'mode','auto');
hold off
The length of x does not equal the length of C{1}. Try x = 1:length(C{1}) or x = -1:8/length(C{1}):7.