A robot vision team may turn up the iteration count of a cleanup filter and discover that a small detection disappears. With OpenCV's morphological opening, the iteration count applies to the underlying erosion and dilation stages. At two iterations, both erosions run before dilation begins. A loop that calls a one-iteration opening twice has a different sequence, because it lets the first dilation restore any surviving shape before the second opening starts.
That distinction matters when the small shape is a valid target. The same filter that clears isolated speckles can erase a short tab, a thin strip or a small fiducial. A regression fixture containing an expected small target can reveal whether a settings migration changed the kind of feature the pipeline accepts.
Test the two interpretations on one mask
The fixture uses a 23-by-32 single-channel mask, with foreground at 255 and background at zero. A 3-by-3 square and a 7-by-7 square are isolated from each other and from the image border. The footprint is a centered 3-by-3 square, and the test requests zero-valued padding explicitly. This isolates the effect of the iteration argument from cropping and padding.
import cv2 as cv
import numpy as np
mask = np.zeros((23, 32), dtype=np.uint8)
mask[5:8, 5:8] = 255
mask[11:18, 19:26] = 255
kernel = np.ones((3, 3), dtype=np.uint8)
options = dict(borderType=cv.BORDER_CONSTANT, borderValue=0)
def opening(image, iterations=1):
return cv.morphologyEx(image, cv.MORPH_OPEN, kernel,
iterations=iterations, **options)
once = opening(mask)
two_passes = opening(once)
two_iterations = opening(mask, iterations=2)
eroded = cv.erode(mask, kernel, iterations=2, **options)
expanded = cv.dilate(eroded, kernel, iterations=2, **options)
assert np.array_equal(once, mask)
assert np.array_equal(two_passes, once)
assert np.array_equal(two_iterations, expanded)
assert not two_iterations[5:8, 5:8].any()
assert (two_iterations[11:18, 19:26] == 255).all()
counts = [int(np.count_nonzero(x))
for x in (mask, once, two_passes, two_iterations)]
assert counts == [58, 58, 58, 49]
One erosion reduces the small square to one foreground pixel. A dilation after that step grows it back to its original 3-by-3 size. The large square also returns to its original dimensions. That is why a second complete opening leaves the mask from the first pass intact in this fixture. The input, first pass and second pass all have 58 foreground pixels.
With two iterations inside one call, the second erosion removes the single surviving pixel of the small square. The large square still has a 3-by-3 core after both erosions. The two dilations restore the large square and leave the location of the small square empty. The result contains its 49 foreground pixels. The fixture compares the combined call against the explicit two-stage code and checks the expected positions as well as the totals.
Use a footprint that fits the feature
Opening is sensitive to shape. A long narrow object can have many foreground pixels and still fail to fit the footprint. Area alone does not tell the reviewer whether the filter will preserve it. If a strip needs to survive, test its minimum width and expected orientation at the resolution used for inspection. Also include a nuisance mark the filter is supposed to remove. A test with only the smallest defect says little about whether the nearby legitimate part will survive.
Record the full filter configuration in the vision recipe. Include the footprint values and size, anchor, foreground polarity, border handling, iteration count and the size of the image entering the filter. Pixel settings can behave differently after a change in magnification or resampling. An opening applied before cropping can also behave differently near a crop boundary from the same call applied afterward. Add a border case when the actual inspection can show parts at the edge of the region of interest.
Keep the call order in the handoff
A recipe that says "open twice" leaves room for both readings. Write down the library call or the exact operation sequence. The OpenCV reference specifies how the iteration count groups erosions and dilations. The scikit-image opening documentation describes the operation in terms of a footprint, which is useful for checking what shape is being tested when a mask pipeline is moved between libraries.
For a migration, compare the complete output masks on the same representative inputs. A matched foreground count can hide relocated or changed boundaries. The two squares are a compact first check. Add representative features close to the acceptance threshold, verify the regions that matter, and inspect any pixel differences before adopting the new filter in an inspection recipe.
Sources
OpenCV image filtering reference, morphologyEx
https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html
scikit-image morphology reference, opening
https://scikit-image.org/docs/stable/api/skimage.morphology.html#skimage.morphology.opening