A robot vision check looks for a metal tab with a gap on its right. Eroding a binary mask with a vertical line template can detect the line where it appears in both a clear and an obstructed silhouette. To make the clearance part of the match, use a template that explicitly requires background on the right.
OpenCV's hit-or-miss operation works on a single-channel binary image. In its signed kernel, 1 requires foreground, -1 requires background, and 0 leaves a position unconstrained. The result marks the selected template anchor where all the requirements match. This example uses the default centered anchor and ignores anchors at the outer image border.
Test the same pixels with two templates
The synthetic mask contains two vertical runs of three foreground pixels. A neighboring foreground pixel touches the first run on the left, where neither template specifies a requirement. A neighboring pixel touches the second run on the right, where the strict template requires background.
import cv2 as cv
import numpy as np
mask = np.zeros((12, 12), dtype=np.uint8)
mask[2:5, 3] = 255
mask[3, 2] = 255
mask[7:10, 8] = 255
mask[8, 9] = 255
kernel = np.array([[0, 1, 0], [0, 1, -1], [0, 1, 0]], dtype=np.int32)
relaxed = kernel.copy()
relaxed[1, 2] = 0
def interior_hits(template):
result = cv.morphologyEx(mask, cv.MORPH_HITMISS, template)
rows, cols = np.nonzero(result[1:-1, 1:-1])
return list(zip((cols + 1).tolist(), (rows + 1).tolist()))
assert interior_hits(kernel) == [(3, 3)]
assert interior_hits(relaxed) == [(3, 3), (8, 8)]
With OpenCV 5.0.0 the strict template reports only (3, 3). Replacing its right-side -1 with 0 also returns (8, 8). The second run did not move, and its foreground pixels satisfy both templates. The relaxed template stopped checking the obstruction. The function above returns coordinates as X and Y, even though the NumPy mask uses row and column indexing.
Validate the template against the camera setup
Use a thresholding step that assigns 255 to the feature being inspected and 0 to its background. Verify the polarity on a known good frame before using the result. Test a clearance violation in the specific direction the template checks. If the tab can rotate, use explicit rotated templates or normalize its orientation before evaluating this pattern. The orientation of the camera and the indexing convention determine what "right" means in the image.
Keep the binary test mask, signed template, and expected anchor coordinates with the inspection settings. The example intentionally excludes anchors whose neighborhoods extend beyond the array. If those edge detections matter to the application, specify and test the border behavior explicitly. The result measures a local configuration in a segmented image. To interpret the gap in physical units, use a calibrated scale and account for the part's pose.
Sources
OpenCV Hit-or-Miss tutorial for the signed kernel convention
https://docs.opencv.org/4.13.0/db/d06/tutorial_hitOrMiss.html
OpenCV image filtering reference for morphologyEx and its anchor and border parameters
https://docs.opencv.org/4.13.0/d4/d86/group__imgproc__filter.html