matlab是如何定义新图像的,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)的正确显示。

H65zF.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值