Labview and Matlab for PT

Just a place to discuss with Matlab and Labview

ad

ginput -- A Useful and Convenient Function


Yesterday night, one of junior classmates told me that he found a useful function called "ginput" which can make the thing getting mouse position easier than using "get(gca......)". It's very convenient when designing some program such as calculating angle in coordinate plane. So, here is a very simple example about how to calculate the angle in a certain image.

global flag h_im h_1
filename='D:\07020203_mpg2-0000281.jpg';
im=imread(filename);
h_im=image(im);
while 1
    cal_cord_ang;
end

function cal_cord_ang
global flag h_im h_1
cla(h_im)
[x,y] = ginput(1);
a=[x,y];
x0=a(1,1);
y0=a(1,2);
hold on
h_1=plot(x0,y0,'color','r','marker','+');
[x,y] = ginput(1);
b=[x,y];
x1=b(1,1);
y1=b(1,2);
h_1=plot([x0 x1],[y0 y1],'color','r','marker','+');
[x,y] = ginput(1);
c=[x,y];
x2=c(1,1);
y2=c(1,2);
h_1=plot([x0 x1 x2],[y0 y1 y2],'color','r','marker','+');

% calculate angle by using this formula --> A dot B = ABcos
A=a-b;
B=c-b;
angle=acosd(dot(A,B)/(norm(A)*norm(B)));
x_t=mean([x0 x2]);
y_t=mean([y0 y2]);
text(x_t,y_t,num2str(angle),'color','r')
ginput(1)
cla(h_im)

However, I almost forget the concept of vector, even the product of two vectors. You can review the concept in this url: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm.

Try it, it's very funny!!!


0 意見: