dongkai057
2013-03-11, 09:10
% 主函数
function test_mouse_track()
figure;
imshow('1.bmp');
axis();
set(gcf,'WindowButtonDownFcn',@ButttonDownFcn);
% 回调函数
%function ButttonDownFcn(src,event)
%pt = get(gca,'CurrentPoint');
%x = pt(1,1);
%y = pt(1,2);
%fprintf('x=%f,y=%fn',x,y);%Matlab的ginput()函数
%axes('XLimMode','manual','yLimMode','manual');
hold on;
function ButttonDownFcn(src,eventdata)
ud=get(src,'userdata');
cp=get(gca,'currentpoint');
x=cp(1,1);y=cp(1,2);
plot(x,y,'r*');
% 连接两点,画一条直线
if ~isempty(ud)
line([ud(1),x],[ud(2),y]);
end
set(src,'userdata',[x,y]);
% 设置起始点和末尾点
x1 = ud(1); y1 = ud(2);
x2 = x; y2 = y;
% 计算直线的斜率和b的值
k = (y1 - y2)/(x1 - x2);
b = y1 - k*x1;
% 读入一副图像
I = imread('1.bmp');
% 将直线上所有点的坐标值和像素值放入矩阵c中
h = 1;
for i = x1:x2;
j = k*i+b;
i = ceil(i);
j = ceil(j);
c(h,1) = i;
c(h,2) = j;
c(h,3) = I(i,j);
h = h+1;
end
% 将矩阵c保存到一个txt格式的文档中,并输出
[m,n]=size(c);
fid = fopen('two.txt','wt');
for i = 1:1:m;
for j = 1:1:n;
if j==n
fprintf(fid,'%g\n',c(i,j));
else
fprintf(fid,'%g\t\t',c(i,j)); % \t,table键
end
end
end
fclose(fid);
在这个程序中有个问题,就是 line([ud(1),x],[ud(2),y]); 这句代码中可以用ud(1)和ud(2),而我下面又引用了ud(1)和ud(2),为什么会出现
??? Attempted to access ud(1); index out of bounds because numel(ud)=0.
Error in ==> test_mouse_track>ButttonDownFcn at 35
x1 = ud(1); y1 = ud(2);
??? Error while evaluating figure WindowButtonDownFcn
这种溢出的错误。。。
可以解决这个问题的可以和我联系下,我的QQ934354773
多谢大侠。。。
function test_mouse_track()
figure;
imshow('1.bmp');
axis();
set(gcf,'WindowButtonDownFcn',@ButttonDownFcn);
% 回调函数
%function ButttonDownFcn(src,event)
%pt = get(gca,'CurrentPoint');
%x = pt(1,1);
%y = pt(1,2);
%fprintf('x=%f,y=%fn',x,y);%Matlab的ginput()函数
%axes('XLimMode','manual','yLimMode','manual');
hold on;
function ButttonDownFcn(src,eventdata)
ud=get(src,'userdata');
cp=get(gca,'currentpoint');
x=cp(1,1);y=cp(1,2);
plot(x,y,'r*');
% 连接两点,画一条直线
if ~isempty(ud)
line([ud(1),x],[ud(2),y]);
end
set(src,'userdata',[x,y]);
% 设置起始点和末尾点
x1 = ud(1); y1 = ud(2);
x2 = x; y2 = y;
% 计算直线的斜率和b的值
k = (y1 - y2)/(x1 - x2);
b = y1 - k*x1;
% 读入一副图像
I = imread('1.bmp');
% 将直线上所有点的坐标值和像素值放入矩阵c中
h = 1;
for i = x1:x2;
j = k*i+b;
i = ceil(i);
j = ceil(j);
c(h,1) = i;
c(h,2) = j;
c(h,3) = I(i,j);
h = h+1;
end
% 将矩阵c保存到一个txt格式的文档中,并输出
[m,n]=size(c);
fid = fopen('two.txt','wt');
for i = 1:1:m;
for j = 1:1:n;
if j==n
fprintf(fid,'%g\n',c(i,j));
else
fprintf(fid,'%g\t\t',c(i,j)); % \t,table键
end
end
end
fclose(fid);
在这个程序中有个问题,就是 line([ud(1),x],[ud(2),y]); 这句代码中可以用ud(1)和ud(2),而我下面又引用了ud(1)和ud(2),为什么会出现
??? Attempted to access ud(1); index out of bounds because numel(ud)=0.
Error in ==> test_mouse_track>ButttonDownFcn at 35
x1 = ud(1); y1 = ud(2);
??? Error while evaluating figure WindowButtonDownFcn
这种溢出的错误。。。
可以解决这个问题的可以和我联系下,我的QQ934354773
多谢大侠。。。