Determining Manual ROI from a graph (not image)

조회 수: 1(최근 30일)
Jessi Martin
Jessi Martin 2022년 11월 28일
편집: DGM 2022년 11월 29일
Hi Everyone,
I'm writing a matlab script to analyze some gait data I have. I currently have a graph that shows 5 distinct steps (below). I need to partition each step out to analyze separately, i.e. break it into 5 different variables.
I know in image processing there is a way to allow the user to input a region of interest to allow for further processing. My question is that is there a way to do this for non-images like graphs? If so, what would be the function to do this?
Jessi M.

채택된 답변

Image Analyst
Image Analyst 2022년 11월 28일
Sure, just use drawrectangle to drag out a box that defines the region you want to include.
% Demo to show how drawrectangle can be used to draw a rectangular box on the image, and crop out that rectangular region to a new image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
y = rand(1, 50);
hFig = figure;
plot(y, 'b-');
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');
% Get the coordinates in the form [xLeft, yTop, width, height].
roiPosition = hBox.Position;
% Delete the ROI object.
% 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); % Delete the box from the overlay.
roiPosition = [];
elseif contains(button, 'Redraw','IgnoreCase',true)
% OPTIONAL If you want to delete the prior one before drawing the next one.
elseif contains(button, 'Accept','IgnoreCase',true)
% If you want to delete the rectangle from the overlay, do this:
delete(hRect); % Delete the box from the overlay.
% Show roiPosition in command window
xLeft = roiPosition(1)
yTop = roiPosition(2)
xRight = xLeft + roiPosition(3)
yBottom = yTop + roiPosition(4)
% Draw lines
xline(xLeft, 'Color', 'r', 'LineWidth', 2);
xline(xRight, 'Color', 'r', 'LineWidth', 2);
yline(yTop, 'Color', 'r', 'LineWidth', 2);
yline(yBottom, 'Color', 'r', 'LineWidth', 2);
caption = sprintf('xLeft = %.2f, xRight = %.2f. yTop = %.2f, yBottom = %.2f', xLeft, xRight, yTop, yBottom);
title(caption, 'fontSize', fontSize)
If you want to turn the coordinates into indexes, you can use find() to find out which index is closest to the one you want. For example if you have x and y vectors
x1Index = find(x > xLeft, 1, 'first');
x2Index = find(x < xRight, 1, 'last');

추가 답변(1개)

DGM 2022년 11월 29일
편집: DGM 2022년 11월 29일





Community Treasure Hunt

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

Start Hunting!

Translated by