How to continuously interact with rectangular ROI?

9 visualizzazioni (ultimi 30 giorni)
Hello,
I would like to create a function which allows the user to enter an image and a rectangular region of interest (ROI) as an input and get as a result the average of the pixel values in that ROI. The intended behavior that I would like is that this should be automatically updated every time I move the rectangle on the image and, say, I click a button (or double click on the ROI).
In pseudo code, this is what I'd like:
  • Load and display an image, draw a rectangular ROI somewhere over the image at an initial position (not important where)
  • Interactively being able to move the ROI around the image and resize it
  • Double Click on the ROI and have the average of the pixels within the ROI pop up on the command window
  • Being able to re-size the ROI again and repeat the process without having to restart the script
I have tried looking into this and I suspect I need to set up "listeners", but I'm very confused on how to specifically do it for this case.
Also, I have noticed that the position of the rectangle that I get by accessing the "Position" field of the ROI is in double precision (and also it starts from 0.5 and 0.5 if I put the top left corner of the rectangle on the top left corner of the image, instead of 1 and 1), while I would like the ROI to be able to only be in integer values (since I have a discrete number of pixels), not a continuous array... Otherwise how can I pass the position to the averaging function, since I need to operate on the indices of the matrix?
Thank you so much!

Risposte (1)

Image Analyst
Image Analyst il 1 Apr 2022
You don't need a listener. Maybe you just want the user to draw a box and ask if they want it or not, like
rgbImage = imread('peppers.png');
hFig = figure;
imshow(rgbImage);
hFig.WindowState = 'maximized';
% Ask user to draw rectangle.
button = 'Redraw';
while contains(button, 'Redraw', 'IgnoreCase',true)
uiwait(helpdlg('Draw a box'));
% User draws a box. It exits as soon as they lift the mouse.
hBox = drawrectangle('Color', 'r');
roiPosition = hBox.Position;
% Delete the ROI object.
delete(hBox);
% and replace it with a rectangle in the graphical overlay.
hold on;
hRect = rectangle('Position', roiPosition, 'EdgeColor', 'r', 'LineWidth', 2);
% Ask user if the rectangle is acceptable.
message = sprintf('Is this good?');
button = questdlg(message, message, 'Accept', 'Redraw', 'Reject and Quit', 'Accept');
if contains(button, 'Quit','IgnoreCase',true)
delete(hRect);
roiPosition = [];
break;
elseif contains(button, 'Accept','IgnoreCase',true)
break;
end
end
% Close down figure.
close(hFig);

Prodotti


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by