I just need to get the ROI in pixels (relative to image, not figure) using rbbox, but I'm having a hard time converting from normalized figure coordinates of the rbbox to image coordinates.
I already tried to multiply it by: image size, figure size, screen size, (image size) / (figure size). Also tried to use the axis position.
Normalized means from 0 to 1, so 1 should be the image size, or the figure size, so what I tried should have worked! I guess maybe the borders of the figure also count... Google is not helping this time.
There should be a method pixelStuff = FromNormalizedToImagePixels(normalizedStuff) !!
- Figure size in pixels = windows size, useless, it includes the borders.
- I need the ROI in "image pixels" (image inside the figure).
- I can calculate the ROI if I could get the image region inside the figure (without the borders).
What I'm missing??
Code sample:
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