在列表中保存 class 个像素,并在用户关闭带标签的矩阵图像时将其传递给 .m 程序

saving class of pixels in a list and passing it to the .m program when user closes the labeled matrix image

如何在我点击某个像素时抓取该像素的 class 并在用户关闭图像时将其发送回我的程序 array/list?

h=figure
image(result);
loc=[];
if ishandle(h) ~= 0
    loc(end+1)=get(0, 'PointerLocation');
    pause(0.01)
end

**h = 

  Figure (1) with properties:
  Number: 1
    Name: ''
   Color: [0.9400 0.9400 0.9400]
Position: [680 558 560 420]
   Units: 'pixels'

显示所有属性

下标赋值维度不匹配。

two_human_new 中的错误(第 23 行) 位置(结束+1)=得到(0,'PointerLocation'); **

基本上我想在用户点击图像时捕获索引信息。

^这张图片是图片的结果(结果) h =

图 (2) 具有属性:

  Number: 2
    Name: ''
   Color: [0.9400 0.9400 0.9400]
Position: [680 558 560 420]
   Units: 'pixels'

显示所有属性

Error using get
There is no Index property on the Root class.

Error in two_human_new (line 23)
    loc(end+1)=get(0, 'Index');

就像 Sardar_Usama 指出的那样,您收到一个错误,因为 get(0, 'PointerLocation') returns 一个包含两个元素的数组。

修改loc为元胞数组解决问题:

初始化:

loc = {};

存放位置:

loc{end + 1} = get(0, 'PointerLocation');

其他选项是将 loc 拆分为 locxlocy

locx = [];
locy = [];

存储:

pos = get(0, 'PointerLocation');
locx(end + 1) = pos(1);
locy(end + 1) = pos(2);

检查以下代码是否是您的意思:
我添加了 while 循环,所以代码保持 运行 直到你关闭 windows.

result = imread('peppers.png');

h=figure;
image(result);
locx = [];
locy = [];

while (ishandle(h))
    pos = get(0, 'PointerLocation'); 
    locx(end + 1) = pos(1);
    locy(end + 1) = pos(2);
    pause(1)
end

%Display locx and locy for debugging.
locx
locy

我想 ginput 就是您要找的。
肯定有比我使用的 try 和 'catch` 块更好的方法,但我现在想不出。

result = imread('peppers.png');

h=figure;
image(result);
locx = [];
locy = [];

%set(gca, 'Units', 'pixels');

while (ishandle(h))
    %pos = get(0, 'PointerLocation'); 
    %pos = get(gca, 'CurrentPoint');
    try
        [x, y] = ginput(1);
        locx(end + 1) = x;
        locy(end + 1) = y;
        pause(0.01)        
    catch me
    end
end

locx
locy