用户输入后续图像以存储坐标


function main()
    clear all;clc;
    path='.image_files';  %___________image files path
    path_posmap='.pos_maps';%_________stores positions of agents
    NumOfImages = length(dir(path)) - 2;
    w = dir(path);
    img_names={};      %________stores names of all images
    for i=3:NumOfImages+2,
        img_names{i-2} = w(i).name;
    end
    for i=1:numel(img_names),
        imname = [ path img_names{i}];
        im0 = imread(imname);
        imageHandle =imshow(im0);%_____________displays the image
        xlabel(num2str(i));
        set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
    end
end
function coordinates=ImageClickCallback ( objectHandle , eventData )
    axesHandle  = get(objectHandle,'Parent');
    coordinates = get(axesHandle,'CurrentPoint'); 
    coordinates = coordinates(1,1:2);
    message     = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
    disp(coordinates); %___ add these coordinates for each image
    close(gcf); 
end

我想向用户显示一系列图像。对于每个图像请求输入,用户以鼠标单击图像的形式输入。将每次点击的坐标存储在矩阵中。因此,最终得到一个维数为num_images x 2的矩阵。

但是在上面

。我无法获得从函数ImageClickCallback

返回的坐标

b。每当用户单击时,我都无法关闭图像并显示新图像。

我现在没有MATLAB,所以在我的答案中有几个猜测。

<<p> 关闭数据/strong>

你没有关闭图像,所以它不会关闭。只需在回调结束时添加close gcf;

传递数据的

现在要获得坐标,我建议使用基本工作区而不是全局变量,或者将参数传递给回调。意思是我会在你的回调结束时使用assignin('base','newcords',coordinates);

使用evalin从基本工作区获取坐标。您可以尝试访问没有evalin的新记录,但我很确定它不会工作。newcords=evalin('base','newcords');

现在创建一个新的变量(在for循环之外初始化),它保存所有坐标,假设2D-coordinates: allcords=zeros(2,numel(img_name));

将回调中的坐标写入new-allcord -变量。

allcords(1,i)=newcords(1);
allcords(2,i)=newcords(2);
第二个想法是你不需要从回调中传递坐标,使用:

初始化循环外的所有记录:

allcords=zeros(2,1);

在你的回调中:

allcords=evalin('base',allcords);
allcordssize=size(allcords):
if min(allcordssize)=1
    allcords(1,end)=coordinates(1);
    allcords(2,end)=coordinates(2);
    assignin('base','allcords',allcords);
else
    allcords(1,end+1)=coordinates(1);
    allcords(2,end+1)=coordinates(2);
    assignin('base','allcords',allcords);
end
    close gcf %close picture

这样你就有了所有来自回调的坐标。如前所述,另一种方法是将变量传递给回调函数。

确保数据与正确的数字匹配

第二个问题是,你的for循环打开所有的图片一次我猜?(我没有MATLAB可用)。那么,在for循环中使用uiwait(gcf);如何?(在你的set之后)。这样您就知道哪个坐标被分配给了哪个图形(如果所有的图片都被打开,您的坐标将与img-list-indexes相反)。

注::我不确定uiwait在这种情况下是否有效,可以肯定的是,如果你创建一个只有1个手柄的GUI(为你的图像),而不是关闭你的图形,只是在每次选择坐标时重新绘制你的图像。或者每次循环迭代加载GUI,并将图像名称传递给GUI。

最新更新