我只需要使用rbbox以像素(相对于图像而不是图形)获得ROI,但是我很难从rbbox的标准化图形坐标转换为图像坐标。
我已经尝试将它乘以:图像大小,图形大小,屏幕大小,(图像大小)/(图形大小)。还尝试使用轴位置。
标准化意味着从0到1,所以1应该是图像大小,或者图形大小,所以我试过的应该有效!我想也许这个数字的边界也很重要......谷歌这次没有帮助。
应该有一个方法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
答案 0 :(得分:1)
我想我解开了这个难题。
我创建了以下示例,将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);
答案 1 :(得分:1)
@Rotem解决方案不起作用。
我希望获得投资回报率,所以我找到的解决方案是使用roipoly 找到鼠标位置的另一种方法是ginput
ROImask = roipoly; % Multi sides ROI
% or
[x,y] = ginput(1); % mouse position in image coordinates