图像中的 MATLAB 自定义数据提示
MATLAB custom datatip in images
按照其他页面上的说明进行操作,例如
http://blogs.mathworks.com/videos/2011/10/19/tutorial-how-to-make-a-custom-data-tip-in-matlab/
http://it.mathworks.com/help/matlab/ref/datacursormode.html
http://it.mathworks.com/matlabcentral/answers/68079-how-to-add-additional-info-to-the-data-cursor
我已经为数据提示编写了一个自定义回调函数来显示 x-y 图上的点的索引以及它们的 x 和 y 坐标:
function output_txt = customCallback_DataTip(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
else % 2D plot: write index of current point
i = find(event_obj.Target.XData == pos(1), 1);
output_txt{end+1} = ['i: ',num2str(i)];
end
此代码从 MATLAB 建议的默认回调开始,并在绘图为 3D 时添加 z 坐标信息。由于我经常需要知道图形上某个点的数组索引,因此在 MATLAB 启动时会自动启用自定义回调函数。
现在,每当我绘制图像时(例如通过 imagesc
),我想要 "normal" 图像数据提示:
即上面有 Index/RGB 信息。如何修改回调函数以获得此行为?
编辑: 我想修改 我的自定义回调 以便它在我时自动显示类似于默认 MATLAB 默认数据提示的内容我正在图像上使用数据提示。
为此,您可以检查 event_obj.Target
的类型并做出相应的响应。
get(event_obj.Target, 'type')
所有图像(无论是 imagesc
、image
还是 imshow
)的 Type
都是 image
。
isImage = strcmpi(get(event_obj.Target, 'type'), 'image')
然后您可以提取图像数据。如果您有索引图像,您还可以获取颜色图以确定要进入数据提示的所有其他信息。
cdata = get(event_obj.Target, 'cdata');
cmap = colormap(ancestor(event_obj.Target, 'axes'));
综上所述,我会将您的自定义数据提示回调修改为类似这样的内容。
function output_txt = callback(obj, event_obj, clims)
% Get the cursor location
pos = get(event_obj, 'Position');
output_txt = {sprintf('[X,Y]: [%i, %i]', pos(1), pos(2))};
if strcmpi(get(event_obj.Target, 'type'), 'image')
% Get the image data
cdata = get(event_obj.Target, 'CData');
% Check to ensure pos is in range
if pos(1) < 1 || pos(1) > size(cdata, 2) || ...
pos(2) < 1 || pos(2) > size(cdata, 1)
rgb = {NaN, NaN, NaN};
newline = sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:});
output_txt = cat(1, output_txt, newline);
return
end
% If the image is RGB
if size(cdata, 3) == 3
rgb = num2cell(cdata(pos(2), pos(1), :));
% If this is an indexed image
else
index = cdata(pos(2), pos(1));
% Figure out the colormap
hax = ancestor(event_obj.Target, 'axes');
cmap = colormap(hax);
% If the CData is scaled, we need to scale to the colormap
if strcmpi(get(event_obj.Target, 'CDataMapping'), 'scaled')
value = (index - clims(1)) * size(cmap, 1) / diff(clims);
else
value = index;
end
% Determine RGB value from colormap
rgb = num2cell(ind2rgb(round(value), cmap));
if round(index) == index
newline = sprintf('Index: %d', index);
else
newline = sprintf('Index: %.4f', index);
end
% Generate datatip text
output_txt = cat(1, output_txt, newline);
end
output_txt = cat(1, output_txt, ...
sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:}));
% Otherwise we use your custom datatip for plots
else
index = find(event_obj.Target.XData == pos(1), 1);
pos = get(event_obj, 'Position');
output_txt = { sprintf('X: %0.4f', pos(1));
sprintf('Y: %0.4f', pos(2))};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = sprintf('Z: %0.4f', pos(3));
else % 2D plot: write index of current point
output_txt{end+1} = sprintf('i: %d', index);
end
end
end
如果您注意到了,我将一个附加变量 (clims
) 传递给回调函数。这是因为某些版本实际上不允许我从 在 数据提示 UpdateFcn
中查询坐标轴属性。所以这意味着您将不得不稍微更改 UpdateFcn
匿名函数。
h = datacursormode(fig);
set(h, 'Enable', 'on')
% Here I have to pass the `clims` because I can't fetch them inside
set(h, 'UpdateFcn', @(dt,e)callback(dt, e, caxis(ancestor(dt.Host, 'axes'))));
使用它,我能够显示绘图和图像(索引和 RGB)的正确显示。
按照其他页面上的说明进行操作,例如
http://blogs.mathworks.com/videos/2011/10/19/tutorial-how-to-make-a-custom-data-tip-in-matlab/ http://it.mathworks.com/help/matlab/ref/datacursormode.html http://it.mathworks.com/matlabcentral/answers/68079-how-to-add-additional-info-to-the-data-cursor
我已经为数据提示编写了一个自定义回调函数来显示 x-y 图上的点的索引以及它们的 x 和 y 坐标:
function output_txt = customCallback_DataTip(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
else % 2D plot: write index of current point
i = find(event_obj.Target.XData == pos(1), 1);
output_txt{end+1} = ['i: ',num2str(i)];
end
此代码从 MATLAB 建议的默认回调开始,并在绘图为 3D 时添加 z 坐标信息。由于我经常需要知道图形上某个点的数组索引,因此在 MATLAB 启动时会自动启用自定义回调函数。
现在,每当我绘制图像时(例如通过 imagesc
),我想要 "normal" 图像数据提示:
即上面有 Index/RGB 信息。如何修改回调函数以获得此行为?
编辑: 我想修改 我的自定义回调 以便它在我时自动显示类似于默认 MATLAB 默认数据提示的内容我正在图像上使用数据提示。
为此,您可以检查 event_obj.Target
的类型并做出相应的响应。
get(event_obj.Target, 'type')
所有图像(无论是 imagesc
、image
还是 imshow
)的 Type
都是 image
。
isImage = strcmpi(get(event_obj.Target, 'type'), 'image')
然后您可以提取图像数据。如果您有索引图像,您还可以获取颜色图以确定要进入数据提示的所有其他信息。
cdata = get(event_obj.Target, 'cdata');
cmap = colormap(ancestor(event_obj.Target, 'axes'));
综上所述,我会将您的自定义数据提示回调修改为类似这样的内容。
function output_txt = callback(obj, event_obj, clims)
% Get the cursor location
pos = get(event_obj, 'Position');
output_txt = {sprintf('[X,Y]: [%i, %i]', pos(1), pos(2))};
if strcmpi(get(event_obj.Target, 'type'), 'image')
% Get the image data
cdata = get(event_obj.Target, 'CData');
% Check to ensure pos is in range
if pos(1) < 1 || pos(1) > size(cdata, 2) || ...
pos(2) < 1 || pos(2) > size(cdata, 1)
rgb = {NaN, NaN, NaN};
newline = sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:});
output_txt = cat(1, output_txt, newline);
return
end
% If the image is RGB
if size(cdata, 3) == 3
rgb = num2cell(cdata(pos(2), pos(1), :));
% If this is an indexed image
else
index = cdata(pos(2), pos(1));
% Figure out the colormap
hax = ancestor(event_obj.Target, 'axes');
cmap = colormap(hax);
% If the CData is scaled, we need to scale to the colormap
if strcmpi(get(event_obj.Target, 'CDataMapping'), 'scaled')
value = (index - clims(1)) * size(cmap, 1) / diff(clims);
else
value = index;
end
% Determine RGB value from colormap
rgb = num2cell(ind2rgb(round(value), cmap));
if round(index) == index
newline = sprintf('Index: %d', index);
else
newline = sprintf('Index: %.4f', index);
end
% Generate datatip text
output_txt = cat(1, output_txt, newline);
end
output_txt = cat(1, output_txt, ...
sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:}));
% Otherwise we use your custom datatip for plots
else
index = find(event_obj.Target.XData == pos(1), 1);
pos = get(event_obj, 'Position');
output_txt = { sprintf('X: %0.4f', pos(1));
sprintf('Y: %0.4f', pos(2))};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = sprintf('Z: %0.4f', pos(3));
else % 2D plot: write index of current point
output_txt{end+1} = sprintf('i: %d', index);
end
end
end
如果您注意到了,我将一个附加变量 (clims
) 传递给回调函数。这是因为某些版本实际上不允许我从 在 数据提示 UpdateFcn
中查询坐标轴属性。所以这意味着您将不得不稍微更改 UpdateFcn
匿名函数。
h = datacursormode(fig);
set(h, 'Enable', 'on')
% Here I have to pass the `clims` because I can't fetch them inside
set(h, 'UpdateFcn', @(dt,e)callback(dt, e, caxis(ancestor(dt.Host, 'axes'))));
使用它,我能够显示绘图和图像(索引和 RGB)的正确显示。