How to continuously interact with rectangular ROI?
조회 수: 9 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
Image Analyst
2022년 4월 1일
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);
댓글 수: 2
Jung Hwan
2024년 3월 21일
Definitely the best way to interactively set ROIs
Also works with polygon ROIs
Thank you very much!
Image Analyst
2024년 3월 21일
@Jung Hwan Thanks. I'm attaching several demos for drawing things and dealing with ROIs that may help people.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!