In HALCON 12, the new operator distance_contours_xld was introduced for calculating the unsigned distance between two contours. This is a useful tool to, for example, find defects in cutting profiles.
What you may not know is that the XLD distance transform was also introduced, which in many cases allows to speed up the distance measurement.
Let’s begin with a template contour representing an ideal cutting profile:
Row := [250,300,350,300,250,300,350,300,250,300,
350,300,250]
Col := [100,150,200,250,300,350,400,450,500,550,
600,650,700]
gen_contour_nurbs_xld (TemplateXLD, Row, Col, ‘auto’,
gen_tuple_const(|Col|,10), 3, 1, 5)
and a test contour representing a real cutting profile:
S :=20
Rnd := rand(|Row|)*S-S/2
gen_contour_nurbs_xld (TestXLD, Row+Rnd, Col, ‘auto’, gen_tuple_const(|Col|,10), 3, 1, 5)
We want to find those parts where the distance between TemplateXLD and TestXLD is larger than 5 pixels. To find these defects, we can simply use:
distance_contours_xld (TestXLD, TemplateXLD, ContourOut, ‘point_to_segment’)
to measure the distance between the contours. Then, we segment the defects via:
segment_contour_attrib_xld (ContourOut, Defects, ‘distance’, ‘and’, 5, 99999)
Now, what happens if we produce many such parts and want to verify the correctness of each profile? We could simply use the same approach and compare each new contour with our template using distance_contours_xld. However, it would be more efficient to use the XLD distance transform.
To use the distance transform, we first call:
create_distance_transform_xld (TemplateXLD, ‘point_to_segment’, 30, DistanceTransformID)
This creation of the distance transform is computationally expensive. However, this step can be performed offline. Using the XLD distance transform to measure the distance between the two contours simply requires a call of:
apply_distance_transform_xld (TestXLD, ContourOut, DistanceTransformID)
Afterwards, we can again extract the defects via segment_contour_attrib_xld. As the XLD distance transform is a handle-based approach, please do not forget to clear the memory once the handle is no longer needed:
clear_distance_transform_xld (DistanceTransformID)
In some cases using apply_distance_transform is more than 20x faster than using distance_contours_xld. In our standard example apply_distance_transform_xld.hdev the speedup is highest when using Mode := ‘point_to_segment’ (line 29), which yields more accurate measurements than Mode := ‘point_to_point’.
This way you can get accurate and fast measurements.