最新のリリースでは、このページがまだ翻訳されていません。 このページの最新版は英語でご覧になれます。
この例では、ポリライン ROI を使用して 3 点測定ツールを作成するために必要な手順を説明します。
イメージとポリラインを正しい角度で表示します。ここでは、ユーザーにポリラインを操作して関心のある角度を測定するよう促すプロンプトを表示します。
im = imread('gantrycrane.png'); % Display image in the axes imshow(im) % Get size of image m = size(im,1); n = size(im,2); % Get center point of image for initial positioning midy = ceil(m/2); midx = ceil(n/2); % Position first point vertically above the middle firstx = midx; firsty = midy - ceil(m/4); lastx = midx + ceil(n/4); lasty = midy; % Add empty context menu to replace default menu c = uicontextmenu; % Create a two-segment right-angle polyline centered in the image h = drawpolyline('Parent',gca,... 'Position',[firstx,firsty;midx,midy;lastx,lasty],... 'Label','Modify angle to begin...',... 'Color',[0.8,0.2,0.2],... 'UIContextMenu',c);
% Listen to event that fires as the ROI is being moved addlistener(h,'MovingROI',@(src,evt) updateAngle(src,evt)); % Intercept attempts to add or remove vertices addlistener(h,'AddingVertex',@(src,evt) storePositionInUserData(src,evt)); addlistener(h,'VertexAdded',@(src,evt) recallPositionInUserData(src,evt)); addlistener(h,'DeletingVertex',@(src,evt) storePositionInUserData(src,evt)); addlistener(h,'VertexDeleted',@(src,evt) recallPositionInUserData(src,evt));
ユーザーがポリラインの形状を変更すると、角度が再計算され、表示されるラベルを更新されます。
function updateAngle(src,evt) % Get the current position p = evt.CurrentPosition; % Find the angle v1 = [p(1,1)-p(2,1), p(1,2)-p(2,2)]; v2 = [p(3,1)-p(2,1), p(3,2)-p(2,2)]; theta = acos(dot(v1,v2)/(norm(v1)*norm(v2))); % Convert it to degrees angleDegrees = (theta * (180/pi)); % Update the Label to display the angle src.Label = sprintf('(%1.0f) degrees',angleDegrees); end
ポリラインではユーザーによる頂点の追加および削除が可能ですが、角度測定ツールでは頂点は常に 3 つしかないことを前提にしています。ここでは、ユーザーが頂点を追加または削除しようとするとそれを阻止するリスナーを追加します。'AddingVertex' および 'DeletingVertex' イベントのコールバックでは、対象となる頂点の追加または削除はまだ実行されません。UserData プロパティで現在のポリラインの位置をキャッシュし、'VertexAdded' および 'VertexDeleted' イベントのコールバックで復元します。これにより、ユーザーがポリラインの頂点を対話形式で追加または削除するのを防ぎます。
function storePositionInUserData(src,~) % Before a vertex is added/removed, store the Position in the UserData % property src.UserData = src.Position; end function recallPositionInUserData(src,~) % Restore the previous Position to prevent users from adding/removing % vertices src.Position = src.UserData; end
addlistener
| drawpolyline
| Polyline