# How do I know the length of line?

4 views (last 30 days)
Kantachai Chamnarnkit on 5 Feb 2023
Edited: Image Analyst on 5 Feb 2023
I want to draw a line on the picture and know its length

Image Analyst on 5 Feb 2023
See examples in the help for imdistline
help imdistline
IMDISTLINE Draggable Distance tool. H = IMDISTLINE creates a draggable distance tool on the current axes. The function returns H, a handle to an imdistline object. H = IMDISTLINE(HPARENT) creates a draggable distance tool on the object specified by HPARENT. HPARENT specifies the HG parent of the imdistline graphics, which is typically an axes but can also be any other object that can be the parent of an hggroup. H = IMDISTLINE(...,X,Y) creates a draggable distance tool with endpoints located at the locations specified by the vectors X and Y. X and Y specify the initial endpoint positions of the draggable distance tool in the form X = [X1 X2], Y =[Y1 Y2]. The draggable distance tool has a context menu associated with it that allows you to: Export endpoint and distance data to the workspace Toggle the distance label on/off Set the line color Specify horizontal and vertical drag constraints Delete the distance tool object Remarks ------- If you use IMDISTLINE with an axis that contains an image object, and do not specify a position constraint function, users can drag the line outside the extent of the image and lose the line. When used with an axis created by the PLOT function, the axis limits automatically expand to accommodate the movement of the line. To understand how IMDISTLINE calculates the angle returned by getAngleToHorizontal, draw an imaginary horizontal vector from the bottom endpoint of the distance line, extending to the right. The value returned by getAngleToHorizontal is the angle from this horizontal vector to the distance line, which can range from 0 to 180 degrees. Example 1 --------- % Insert a distance tool into an image. Use makeConstrainToRectFcn to specify % a position constraint function that prevents distance tool from being dragged % outside the extent of the image. Explore the context menu options of the % distance tool by right clicking on the distance tool. figure, imshow('pout.tif'); h = imdistline(gca); fcn = makeConstrainToRectFcn('imline',get(gca,'XLim'),get(gca,'YLim')); setPositionConstraintFcn(h,fcn); Example 2 --------- % Use distance tool with XData and YData of associated image in non-pixel % units. start_row = 1; end_row = 1350; meters_per_pixel = 1; start_col = 731; end_col = 2955; I = imread('concordorthophoto.png'); I1 = I(start_row:meters_per_pixel:end_row, start_col:meters_per_pixel:end_col); figure; imshow(I1); title('1 meter per pixel'); % Specify initial position of distance tool on image. h1 = imdistline(gca,[4 2218],[40 781]); setLabelTextFormatter(h1,'%02.0f meters'); % Repeat process but work with a 2 meter per pixel sampled image. Verify % that the same distance is obtained. meters_per_pixel = 2; I = imread('concordorthophoto.png'); I1 = I(start_row:meters_per_pixel:end_row, start_col:meters_per_pixel:end_col); figure; hImg = imshow(I1); title('2 meters per pixel'); % Convert XData and YData to meters using conversion factor. XDataInMeters = get(hImg,'XData')*meters_per_pixel; YDataInMeters = get(hImg,'YData')*meters_per_pixel; % Set XData and YData of image to reflect desired units. set(hImg,'XData',XDataInMeters,'YData',YDataInMeters); set(gca,'XLim',XDataInMeters,'YLim',YDataInMeters); % Specify initial position of distance tool on image. h2 = imdistline(gca,[4 2218],[40 781]); setLabelTextFormatter(h2,'%02.0f meters'); See also makeConstrainToRectFcn. Documentation for imdistline doc imdistline
##### 2 CommentsShowHide 1 older comment
Image Analyst on 5 Feb 2023
Edited: Image Analyst on 5 Feb 2023
To get the pixel dimensions, see Steve's blog:
to get real world distances you need to spatially calibrate to some object of known length in the image. See my attached demo and incorporate it into your code.
To automatically get the caliper dimensions, you can use bwferet
help bwferet
BWFERET Measure Feret diameters and angles of image regions. OUT = BWFERET(I) measures the maximum Feret Properties of each component (object) in the image. I can be a binary image, connected component or labeled matrix. OUT = BWFERET(BW, PROPERTIES) measures a set of Feret properties of each connected component (object) in the binary image BW, which is a logical array. OUT = BWFERET(CC, PROPERTIES) measures a set of Feret properties of each connected component (object) in CC, which is a struct returned by BWCONNCOMP. OUT = BWFERET(L, PROPERTIES) measures a set of Feret properties of each labeled component (object) in the label matrix L. Positive integer elements of L correspond to different regions. For example, the set of elements of L equal to 1 corresponds to region 1; the set of elements of L equal to 2 corresponds to region 2; and so on. [OUT, L] = BWFERET(...) measures a set of Feret properties of each component (object) in the binary image, connected component or labeled matrix. This also returns the corresponding label matrix L such that the first value in the table OUT corresponds to the labeled region 1, the second value with the labeled region 2 and so on. PROPERTIES can be an array of strings, a single character vector, a cell array of character vectors, or 'all'. If PROPERTIES is set to 'all' it returns all the Feret properties mentioned below. If no argument is provided, all maximum Feret properties are given as outputs. The set of valid measurement strings or character vectors includes 'MaxFeretProperties', 'MinFeretProperties' and 'all'. 'MaxFeretProperties' - Outputs all the properties related to maximum Feret Diameter. These properties are: MaxDiameter - Maximum Feret diameter length. MaxAngle - Angle of maximum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MaxCoordinates - Endpoint coordinates of maximum Feret diameter. 'MinFeretProperties' - Outputs all the properties related to minimum Feret Diameter. These properties are: MinDiameter - Minimum Feret diameter length. MinAngle - Angle of minimum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MinCoordinates - Endpoint coordinates of minimum Feret diameter. Class Support ------------- If the first input is BW, BW must be a logical array and it should be 2D. If the first input is CC, CC must be a structure returned by BWCONNCOMP. If the first input is L, L must be real, nonsparse and 2D. L can have any numeric class. The output OUT is returned as a table. Example 1 --------- % Calculate the minimum Feret diameter for objects in image I = imread('toyobjects.png'); bw = imbinarize(I, 'adaptive'); % Retain the two biggest objects in the image bw = bwareafilt(bw, 2); bw = imfill(bw, 'holes'); % Calculate the Feret properties of the objects in the image along with % the label matrix [out, L] = bwferet(bw, 'MinFeretProperties'); Example 2 --------- % Plot the maximum Feret diameter for objects in the image % Read an image I = imread('toyobjects.png'); % Binarize the image B = imbinarize(I, 'adaptive'); % Fill in the holes in the binary image B = imfill(B, 'holes'); % Show the image h = imshow(B) ax = h.Parent; % Convert to connected component struct using bwconncomp C = bwconncomp(B); % Calculate Feret Properties F = bwferet(C, 'MaxFeretProperties'); hold on % Display maximum Feret Diameters with their values for each object imdistline(ax, F.MaxCoordinates{1}(:,1), F.MaxCoordinates{1}(:,2)); imdistline(ax, F.MaxCoordinates{2}(:,1), F.MaxCoordinates{2}(:,2)); imdistline(ax, F.MaxCoordinates{3}(:,1), F.MaxCoordinates{3}(:,2)); imdistline(ax, F.MaxCoordinates{4}(:,1), F.MaxCoordinates{4}(:,2)); See also BWCONNCOMP, BWLABEL, BWLABELN, LABELMATRIX, REGIONPROPS. Documentation for bwferet doc bwferet

### Categories

Find more on Region and Image Properties in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!