I wrote the Matlab GUI script below in App Designer in order to select an image ROI:
function markROIButtonPushed(app, event)
function allevents(src,evt)
evname = evt.EventName;
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
pos = roi.Position;
app.col1 = floor(pos(1)); % Column 1
app.col2 = ceil(pos(1) + pos(3)); % Column 2
app.row1 = floor(pos(2)); % Row 1
app.row2 = ceil(pos(2) + pos(4)); % Row 2
app.x1Crop = app.col1;
app.y1Crop = app.row1;
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
pos = roi.Position;
app.col1 = floor(pos(1)); % Column 1
app.col2 = ceil(pos(1) + pos(3)); % Column 2
app.row1 = floor(pos(2)); % Row 1
app.row2 = ceil(pos(2) + pos(4)); % Row 2
app.x1Crop = app.col1;
app.y1Crop = app.row1;
end
end
imshow(app.I,'Parent', app.imageAxes);
roi = drawrectangle(app.imageAxes);
[app.Iy1,app.Ix1,app.Iz1] = size(app.I);
addlistener(roi,'MovingROI',@allevents);
addlistener(roi,'ROIMoved',@allevents);
pos = roi.Position;
app.col1 = floor(pos(1)); % Column 1
app.col2 = ceil(pos(1) + pos(3)); % Column 2
app.row1 = floor(pos(2)); % Row 1
app.row2 = ceil(pos(2) + pos(4)); % Row 2
app.x1Crop = app.col1;
app.y1Crop = app.row1;
if app.col1 < 1
app.col1 = 1;
end
if app.row1 < 1
app.row1 = 1;
end
if app.col2 > app.Ix1
app.col2 = app.Ix1;
end
if app.row2 > app.Iy1
app.row2 = app.Iy1;
end
end
% Button pushed function: selectROIButton
function selectROIButtonPushed(app, event)
app.Icrop = app.I(app.row1 : app.row2, app.col1 : app.col2, :)
disp(app.col1)
disp(app.row1)
app.RoiOn = true;
app.ROISwitch.Enable = 'off';
end
But when I wish to start to mark the ROI from the image coordinates (1,1) for the upper-left corner of the image I get this error “Index in position 2 is invalid. Array indices must be positive integers or logical values.”
The same issue will appear in the lower-left corner of the image (error: "Index in position 1 exceeds array bounds. Index must not exceed...")?
How can I solve it?