【问题标题】:How to get actual pixel indices from MATLAB function ginput?如何从 MATLAB 函数 ginput 中获取实际像素索引?
【发布时间】:2018-06-27 14:36:58
【问题描述】:

我正在使用 MATLAB 函数 ginput 来标记我的图像数据以进行进一步处理。这是我的代码:

file_name = "test.jpg";

% Read the image
img = imread(file_name);

% Get the image dimension
imgInfo = imfinfo(file_name);
width = imgInfo.Width;
height = imgInfo.Height;

% Using ginput function to label the image
figure(1);
imshow(img);
hold on;
[x, y] = ginput(4); % Manually label 4 points
scatter(x, y, 100, 'ro', 'filled'); % Plot the marked points on img
hold off;

我的问题:

我发现输出xy 不是整数,因此它们不代表像素索引。

有时,max(x) > widthmax(y) > height 这两个条件都满足。似乎表明我使用ginput 标记的 4 个点在图像之外(但实际上不是)。

我知道这个问题与图像坐标系设置有关,但我仍然不确定如何将从ginput 函数获得的xy 转换为实际的像素索引? 谢谢。

【问题讨论】:

    标签: image matlab


    【解决方案1】:

    下面的代码显示了一个 2x2 图像,放大了坐标轴以便我们看到它,然后打开坐标轴刻度和标签。这样做的目的是让您查看用于在坐标区对象中渲染图像的坐标系。

    imshow([255,0;0,255])
    set(gca,'position',[0.2,0.2,0.6,0.6])
    axis on
    

    ginput 返回的坐标与这些坐标相匹配。

    简而言之,您只需将ginput 返回的坐标四舍五入即可获取图像中的索引:

    [x, y] = ginput(4); % Manually label 4 points
    x = round(x);
    y = round(y);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      相关资源
      最近更新 更多