Issue with imdistline() function in MATLAB?

8 ビュー (過去 30 日間)
Eric Nunes
Eric Nunes 2014 年 10 月 22 日
回答済み: Roche de Guzman 2021 年 1 月 30 日
I am trying to measure the distance two points on an image and then use that distance as an input for some other function. Here is my code:"
if true
figure, imshow(I);
h = imdistline(gca);
api = iptgetapi(h);
width = api.getDistance();
end
When I run it, I am able to drag and get the distance I want on the image, but the distance I get from using the getDistance is some default value it started with , when it opened and it does not change , even if I change it in the image. How can the variable width have the distance i measured before I press enter or any eye and close the image.
Eric
  1 件のコメント
Brett Barnes
Brett Barnes 2016 年 4 月 22 日
Are you right clicking the line to open the context menu? There's an option there to "Export to Workspace"

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2014 年 10 月 23 日
I ran your code on R21014b and it works just fine - exactly how you'd expect. I think you'll have to call tech support.

Roche de Guzman
Roche de Guzman 2021 年 1 月 30 日
global h; h = imdistline; % activates the fx for drawing and measuring line
global xc yc; xc = 0.3; yc = 0.1; % x and y conversion factors
setLabelTextFormatter(h,'measure'); % message
addNewPositionCallback(h,@ShowDistance); % new position callback
%% Custom fx
function ShowDistance(pos)
global h xc yc; % sets line handles and x and y conversion factors to global
X = pos(:,1)*xc; Y = pos(:,2)*yc; % converts x and y positions from px to target unit
d = norm(diff([X Y])); % distance in target unit
assignin('base','d',d); % stores in Workspace
setLabelTextFormatter(h,[num2str(d,'%.1f') ' target unit']); % updates measurement
end

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by