matlab 获取rbbox(ROI) 以像素为单位的图中图像内部

Matlab get rbbox (ROI) in pixels of the image inside the figure

我只需要使用 rbbox 获得以像素为单位的 ROI(相对于图像,而不是图形),但我很难从 rbbox 的标准化图形坐标转换为图像坐标。

我已经尝试将它乘以:图像大小、图形大小、屏幕大小、(图像大小)/(图形大小)。也尝试过使用轴位置。

归一化意味着从0到1,所以1应该是图像大小,或者图形大小,所以我试过应该有效!我想也许图形的边界也很重要...... Google 这次没有帮助。

应该有一个方法 pixelStuff = FromNormalizedToImagePixels(normalizedStuff) !!

我错过了什么??

代码示例:

close all; clc;

figH = figure();
set(figH,'Units','normalized');
if isvalid(figH)
    % load some image
    imgData = imread('ImL_9.png');

    imshow(imgData,'Colormap', hot(256),'DisplayRange',...
        [min(imgData(:)) max(imgData(:))],'InitialMagnification','fit');
    grid on; axis on; xlabel('x'); ylabel('y');
    axis equal; axis manual;
    % Get image size (pixels)
    [isy,isx] = size(imgData);
    % Set axis to fit image
    ax = get(figH,'CurrentAxes');
    set(ax,'xlim',[0 isx]); set(ax,'ylim',[0 isy]);

    % Get mouse event to set ROI
    k = waitforbuttonpress;
    imgROIn = rbbox;
    annotation('rectangle',imgROIn,'Color','red');

    % Get screen size
    screenSize = get(0,'screensize');

    % Get figure position
    pos = get(figH, 'Position');

    % Conversion 1. roi size px = roi size norm * (image size px / figure size norm)
    cx = isx / pos(3);
    cy = isy / pos(4);
    conv = [cx cy cx cy];
    % Converts from normalized figure coordinate to image pixels coordinate
    imgROIpx = imgROIn.*conv;

    % Show result. imgROIpx does not match what was expected, like
    % selecting the entire image the result should be: 0 0 isx isy
    imgROIpx
end

我想我解开了这个谜题。

我创建了以下示例,将 rbbox 标准化坐标转换为以像素为单位的图像坐标:

close all

%Load and display an image for testing
I = imread('pout.tif');
imshow(I);
set(gcf, 'Units', 'normalized')
k = waitforbuttonpress;
rect_pos = rbbox;

%Get screen size.
screenSize = get(0, 'ScreenSize');

%Screen size in pixels (width, height).
screenSizePixels = screenSize(3:4);

%Get figure size (normalized to [0, 1] out of screenSize).
figPositionNormalized = get(gcf, 'Position');

%Get axes size (normalized to [0, 1] out of figure size).
axesPositionNormalized = get(gca, 'Position');

%Convert figure size to pixels.
figPositionPixels = figPositionNormalized.*[screenSizePixels, screenSizePixels];
figSizePixels = figPositionPixels(3:4);

%Convert axes position to pixels.
axesPositionPixels = axesPositionNormalized.*[figSizePixels, figSizePixels];

axesSizePixels = axesPositionPixels(3:4);

%Subtract axes upper left corner from rect_pos.
rect_pos(1:2) = rect_pos(1:2) - axesPositionNormalized(1:2);

%Convert rect_pos to pixels
rectPosPixels = rect_pos.*[figSizePixels, figSizePixels];

%Reverse up/down (to get coordinates in image).
rectPosPixels(2) = axesSizePixels(2) - rectPosPixels(2);

rectPosPixels = round(rectPosPixels);

%Mark pixel with white rectange, and black dot.
I(rectPosPixels(2)-1:rectPosPixels(2)+1, rectPosPixels(1)-1:rectPosPixels(1)+1) = 255;
I(rectPosPixels(2), rectPosPixels(1)) = 0;

%Show marked image.
imshow(I);

我用鼠标指向中心像素:

如果调整图像大小,@Rotem 解决方案将不起作用。

我一直在寻找投资回报率,所以我找到的解决方案是使用 roipoly 另一种找到鼠标位置的方法是 ginput

ROImask = roipoly; % Multi sides ROI
% or
[x,y] = ginput(1); % mouse position in image coordinates