主要内容

snap

R2026b

Snap measurement tool line or points to edge in image

Since R2026b

Description

snap(tool) snaps the measurement line of the measurement tool, tool, to linear or circular edges in the image using the "canny" edge detection method.

example

snap(tool,Name=Value) specifies options using additional name-value arguments. For example, snap(hTsquare,LineSnapMethod="parallel") specifies to snap the reference line to the linear edge that has the highest cosine similarity to the reference line.

Examples

collapse all

Read a test image of a metal plate into the workspace.

A = imread("solidplate.png");

Adjust the image contrast, and display the image using the imageshow function.

A = imadjust(A);
hIm = imageshow(A);

To interactively measure angles, configure the angle tool on the displayed image by using the uifitangle object. Position the angle tool at the vertex of the angle to measure, and then drag it to the two angle edges.

hAngle = uifitangle(hIm);

This GIF shows how to perform an angle measurement using the angle tool.

Animation showing a user performing an interactive measurement of an angular feature by interactively placing the angle tool.

Automatically Snap Angle Tool to Edges

To snap the position of the angle tool to the nearest plate vertex and edges after you have already interactively placed the tool and obtained a measurement, use the snap object function.

snap(hAngle)

This figure shows the updated, more precise angle measurement on the displayed image after snapping the angle tool. To snap the angle tool to edges while you are interactively measuring, see Automatically Align Interactive Angle Measurement Tool to Edges.

Image of the angle tool position and angle measurement after snapping the tool to the nearest vertex and metal plate edges.

Input Arguments

collapse all

Measurement tool, specified as one of these options.

To tune the snapping behavior, specify these name-value arguments depending on the measurement tool you select.

tool valuesnap Name-Value Arguments

uitsquare object

EdgeMap, SnapNeighborhood, LineSnapMethod, PointSnapMethod
uifitangle objectEdgeMap, SnapNeighborhood, LineSnapMethod, PointSnapMethod

uifitcircle object

EdgeMap, SnapNeighborhood, RansacIterations, RansacInlierDistance, AngleRange

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: snap(hTsquare,LineSnapMethod="parallel") specifies to snap the reference line to the linear edge that has the highest cosine similarity to the reference line.

Options for All Measurement Tools

expand all

Edge detection method for snapping, specified as a logical array or one of these options.

MethodDescription
"canny"

Finds edges by looking for local maxima of the gradient of the image. The edge function calculates the gradient using the derivative of a Gaussian filter. This method uses two thresholds to detect strong and weak edges, including weak edges in the output if they are connected to strong edges. By using two thresholds, the Canny method is less likely than the other methods to be fooled by noise, and more likely to detect true weak edges.

"sobel"

Finds edges at those points where the gradient of the image is maximum, using the Sobel approximation to the derivative.

"prewitt"

Finds edges at those points where the gradient of the image is maximum, using the Prewitt approximation to the derivative.

"roberts"Finds edges at those points where the gradient of the image is maximum, using the Roberts approximation to the derivative.
"log"Finds edges by looking for zero-crossings after filtering the image with a Laplacian of Gaussian (LoG) filter.
"zerocross"Finds edges by looking for zero-crossings after filtering the image.
"approxcanny"

Finds edges using an approximate version of the Canny edge detection algorithm that provides faster execution time at the expense of less precise detection.

Specify EdgeMap as a logical array to define a binary image that contains precomputed edge locations for snapping. Use this option to avoid repeated edge detection when making multiple measurements on the same image.

The default edge detection method depends on the measurement tool type.

Measurement ToolDefault Snapping Method

uitsquare object (point-to-line measurement), uifitangle object (angle measurement)

"canny"

uifitcircle object (circle measurement)

"sobel"

Snapping neighborhood, specified as a positive integer. Adjust this value for different image sizes. Units are in pixels.

The region defined by SnapNeighborhood and its default value depends on the measurement tool type.

Measurement ToolRegion DefinitionSnapNeighborhood value

uitsquare object (point-to-line measurement), uifitangle object (angle measurement)

For the reference line, SnapNeighborhood specifies the distance on either side of the line where the tool scans for edges to snap to. For measurement points, SnapNeighborhood specifies the radius around each point where the tool scans for edges to snap to.30

uifitcircle object (circle measurement)

SnapNeighborhood specifies a band-shaped search region for edge snapping, specified as a fraction of the circle radius. The algorithm searches for edges within an annular neighborhood extending from (1 – S)R to (1 + S) R around the circle’s circumference. S is the value of SnapNeighborhood, and R is the radius of the circle.0.4

Options for Point-to-line and Angle Tools

expand all

Snapping method for automatically aligning the reference line to an edge, specified as one of these options:

  • "closest" — Snap the reference line to the closest linear edge based on pixel distance from the midpoint of the reference line to the linear edge. Use this method for simple images with well-defined edges.

  • "strongest" — Snap the reference line to the linear edge with the highest Hough accumulator value. Use this method when multiple edges are close together but one is more prominent, or in noisy images where only strong features are of interest.

  • "parallel" — Snap the reference line to the linear edge that has the highest cosine similarity to the reference line. Use this method to snap to the edge that is most parallel to a specified reference line, regardless of its position.

  • "combined" — Snap the reference line to a linear edge based on the combined metrics of the "closest", "strongest", and "parallel" methods.

  • "none" — Do not snap the reference line.

To specify the LineSnapMethod argument, you must specify measurement tool, tool, as a uitsquare or uifitangle object.

Method for snapping a measurement point to a linear edge, specified as one of these options:

  • "closest" — Snap to the closest linear edge based on distance from the point to the linear edge

  • "strongest" — Snap to the linear edge with the highest Hough accumulator value.

  • "none" — Do not snap measurement point to the reference line.

The tool uses a Hough Transform to find linear edges in a circular region centered at the measurement point. The radius of the circular region is set by the SnapNeighborhood property value.

To specify the PointSnapMethod argument, you must specify the measurement tool, tool, as one of these values:

  • uitsquare object — The measurement point to snap is the first point in the reference line of the point-to-line measurement tool.

  • uifitangle object — The measurement point to snap is the start point of the angle tool.

Options for Circle Measurement Tool

expand all

Number of RANSAC iterations to run to detect the most accurately snapped circle, specified as a positive integer. The Random Sample Consensus (RANSAC) algorithm for circle estimation involves repeated random sampling steps to robustly estimate the parameters of a circle from a set of measurement points. Increase the number of iterations to increase the chance that at least one sample contains only inliers (points in the true circle), leading to a good model, at the expense of longer computation time.

To specify the RansacIterations argument, you must specify the measurement tool, tool, as a uifitcircle object.

Inlier distance threshold, specified as a positive numeric scalar. This value defines the maximum allowable distance, in pixels, between a measurement point and the estimated circle for that point to be considered an inlier. Increase the RansacInlierDistance value to count more noisy or slightly off-circle points as inliers, at the expense of a less accurate circle fit and measurement.

To specify the RansacInlierDistance argument, you must specify the measurement tool, tool, as a uifitcircle object.

Angle defining the circumference arc, specified as a 2-element numeric vector. The vector is in the form [startAngle, stopAngle], where startAngle and stopAngle represent the initial and final angle of the arc, respectively, measured counterclockwise from the positive x-axis. Both angles are unbounded. For example, [0, 360] and [-180, 180] define the same full circle. Specify the AngleRange to snap to an obstructed circle or circle arc.

To specify the AngleRange argument, you must specify the measurement tool, tool, as a uifitcircle object.

Version History

Introduced in R2026b