System designHow-to

Machine vision image overlay with transparency and masks

Build a machine vision image overlay using transparency or binary masks, with checks for alignment, channels and intensity range.

LumaSpec Vision10 min read Share on LinkedIn

On this page 9 sections
  1. 01Choose the operation before writing the overlay
  2. 02Preserve originals and validate the input arrays
  3. 03Apply alpha blending in a common numeric domain
  4. 04Use a binary mask for exact pixel selection
  5. 05Check coordinates and frame identity before judging alignment
  6. 06Verify the compositor with controlled test images
  7. 07Choose acquisition and processing hardware by measured needs
  8. 08Know when an overlay is the wrong fix
  9. 09Questions

A transparent overlay and a pixel-replacement mask solve different problems. Transparency helps you compare two images while keeping both visible. A binary mask selects which source supplies each output pixel. For inspection developers, choosing the wrong operation can hide defects, alter intensities or make a correctly detected feature appear misplaced. The workflow below separates display composition from inspection data, checks the input arrays, and verifies the result without changing the geometric calibration of already acquired images.

Choose the operation before writing the overlay

Use alpha blending when you want to compare a reference image with a current image. Use binary-mask compositing when selected pixels must come entirely from another image. Neither operation aligns images automatically.

Requirement Preferred operation What happens to pixel values Main limitation
Compare a reference with a current image Alpha blending Corresponding values are mixed Differences can become less visible
Replace a selected region Binary-mask compositing Each pixel comes from one source Mask boundaries must be correct
Show detected defects on an inspection preview Colored annotation on a display copy Display pixels change; source pixels remain untouched The preview is unsuitable for intensity measurement
Measure a defect or calculate a threshold Process the original image Measurement uses unmodified source values A display overlay is not the measurement input

For a defect preview, the workflow in live binary image preview for defect inspection explains how thresholded results can remain separate from the acquired image. Use its binary output as a selection mask, not as an unintended replacement for the original grayscale data.

Make the intended result explicit before implementation: visual comparison, hard replacement, or annotation. That decision determines the numeric operation and the verification tests.

Preserve originals and validate the input arrays

  1. Keep acquisition data outside the display pipeline. Store or retain the original frame, its identifier and the processing result. Create a separate output buffer for composition. A new variable name does not necessarily create an independent image buffer; confirm the ownership rules of your imaging library.

A camera buffer may be reused after acquisition resumes. If the compositor still needs its pixels, retain the buffer through the documented mechanism or copy it before release. The discussion of image buffer ownership and memory leaks is useful when display and inspection run asynchronously.

  1. Check dimensions and channel layouts explicitly. Direct pixelwise composition requires matching height and width. Also verify channel count, channel order, numeric type and usable intensity range.

For example, an image measuring 1280 × 1024 pixels does not directly match one measuring 1024 × 1280 pixels. Those arrays may represent a rotated view, but swapping dimensions alone does not establish correspondence.

A grayscale image has one value per pixel. An RGB image has three. To place grayscale content into an RGB display, replicate its values across the color channels or apply a documented display mapping. Do not depend on implicit broadcasting whose behavior might change between libraries.

  1. Check what the numbers represent. An unsigned 8-bit display image commonly uses values from 0 to 255. A floating-point display image might use 0 to 1, but floating-point storage does not guarantee that convention.

Higher-bit-depth camera data may occupy only part of a larger storage type. Decode packed data and establish its documented intensity range before conversion. Do not normalize using each frame's brightest pixel unless changing display contrast is intentional.

Apply alpha blending in a common numeric domain

  1. Convert compatible inputs to a common working representation, then blend. For two images without existing transparency, define alpha as the contribution of the foreground image. Use a scalar alpha for uniform transparency or a matching spatial array for local transparency.
C = (1 - alpha) × A + alpha × B 0 ≤ alpha ≤ 1

Here, A is the background, B is the foreground and C is the display result. Alpha equal to 0 selects the background; alpha equal to 1 selects the foreground. For multichannel images, apply the same spatial alpha value to every channel unless a different effect is deliberate.

Consider a hypothetical grayscale pixel with background value 40, foreground value 200 and alpha equal to 0.25:

C = (1 - 0.25) × 40 + 0.25 × 200 C = 0.75 × 40 + 0.25 × 200 C = 30 + 50 = 80

The result is neither source value. That is why a blended preview must not silently become the input to a calibrated intensity measurement.

Perform the arithmetic in a representation that avoids integer overflow and premature rounding. Convert back to the display format only after blending. The following is conceptual pseudocode, not a library-specific API:

A_work = floating_point(A)
B_work = floating_point(B)
C_work = (1 - alpha) * A_work + alpha * B_work
C_display = cast_to_display(round(clip(C_work, low, high)))

The limits low and high belong to the selected display representation. If the output remains floating point, integer rounding is unnecessary. Both inputs must already use compatible intensity scales before this calculation.

For a visual comparison, a midrange alpha makes both images visible. It does not prove that exposures, color responses or illumination match. Arithmetic on gamma-encoded display values is also not equivalent to blending linear light measurements.

Use a binary mask for exact pixel selection

  1. Convert the selection mask to Boolean values, then copy selected pixels. A true mask element selects the foreground; a false element leaves the background unchanged.

For multichannel images, the mask usually contains one Boolean value per spatial pixel. Copy all channels at each selected position. This avoids replacing only one component of a color pixel.

C = independent_copy(A)
for each pixel position (row, column):
    if M[row, column] is true:
        C[row, column, all_channels] = B[row, column, all_channels]

A mask stored with values 0 and 255 must not be used directly as an alpha array expecting values between 0 and 1. Convert it to a Boolean selection, or normalize it deliberately if fractional blending is intended.

For example, suppose the background pixel is 40 and the foreground pixel is 200. A false mask returns 40; a true mask returns 200. Unlike the earlier alpha example, no intermediate value is created.

Decide separately whether intermediate mask values represent meaningful transparency. An antialiased edge can be appropriate for a display annotation, but it is no longer a strictly binary selection. If a binary mask must be resized, nearest-neighbor sampling preserves discrete labels, although the geometry still requires validation.

For a colored defect highlight, construct a foreground containing the chosen display color and use the defect mask to select it. Use fractional alpha inside that mask only when the underlying texture must remain visible.

Check coordinates and frame identity before judging alignment

  1. Confirm that corresponding array positions refer to the same scene location. Matching array sizes is necessary, but it is not sufficient. Two equally sized images can have different crops, orientations, acquisition times or distortion corrections.

Check these properties together:

  • Sensor region of interest and its offset within the full image.
  • Image rotation, mirroring and any display-only transformation.
  • Pixel-coordinate convention, including whether indexing begins at zero or one.
  • Whether each source is raw, rectified, cropped or resized.
  • Frame identifiers and the relationship between acquisition and processing results.

A crop introduces a coordinate offset even when no resizing occurs. With zero-based coordinates, suppose a crop begins at full-image position (80, 40). A feature at crop position (25, 10) belongs at:

u_full = u_crop + x_offset = 25 + 80 = 105 v_full = v_crop + y_offset = 10 + 40 = 50

Apply that mapping to annotation coordinates. Do not shift the underlying acquisition image merely to compensate for a coordinate-indexing mistake. The guide to barcode detection coordinate indexing covers this distinction for detection results.

Use a fixed edge or fiducial to inspect alignment at full pixel scale. A pixel step alignment cursor helps distinguish a genuine displacement from display interpolation.

If orientation differs, make the transformation explicit and apply the corresponding mapping to masks and annotations. The workflow for camera image rotation without losing acquisition speed addresses where that operation belongs in the processing pipeline.

Keep the original calibration associated with its original image geometry. A transformed display needs a corresponding coordinate mapping; it does not justify silently editing calibration parameters.

Verify the compositor with controlled test images

  1. Test arithmetic, selection and timing independently. Start with synthetic arrays whose expected outputs are easy to calculate. Real production images contain too many unrelated variables for an initial correctness test.

Use this short acceptance checklist:

  • Alpha endpoints: alpha equal to 0 reproduces the background, and alpha equal to 1 reproduces the foreground, after documented format conversions.
  • Known intermediate value: the worked example above produces 80 in an integer display representation.
  • Empty mask: no pixels change from the background.
  • Full mask: every pixel comes from the foreground.
  • Single selected pixel: exactly that spatial position changes, across all intended channels.
  • Color order: a known red annotation appears red rather than blue.
  • Source integrity: the original frame remains unchanged after composition.
  • Frame association: the displayed annotation carries the identifier of the image that produced it.

A checkerboard or alternating-stripe test also exposes accidental resizing and interpolation. Inspect both the numerical output and the displayed result; a display window may smooth pixels even when the array itself is correct.

Then exercise the live acquisition path. Delay processing deliberately during a controlled software test and check that old results are not painted over newer frames. Camera preview overlays for debugging motion tracking explains useful frame and coordinate annotations for this stage.

Choose acquisition and processing hardware by measured needs

Overlay mathematics rarely determines camera selection by itself. Choose area scan cameras around the inspection's field of view, required detail, exposure and acquisition timing. Use the camera finder to compare those requirements before considering display convenience.

The LS-CMR-0166 is a camera candidate to evaluate against your acquisition requirements.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

Its suitability still depends on the application. A product listing alone does not establish compatibility with your acquisition library, host operating system or processing runtime. Verify the documented software path and the required image format before ordering.

For a dedicated processing installation, review vision controllers against camera interfaces, application deployment requirements and measured processing load. Port counts alone do not establish compositing performance or runtime compatibility.

The MV-VC3201X-128G66 is a vision-controller candidate for that evaluation.

Vision controllersHikrobot MV-VC3201X-128G66 VC3000X vision controller, G7400, 8GB+128G SSD, No PoE, PCIe expansion supported, Incl. VM6200Pro licenseMV-VC3201X-128G66
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
Light outputs
4 built-in channels
Memory
8 GB
$2,319.00View partBuild a system with it

Benchmark the complete path: acquisition, any required copy, format conversion, composition and display. Measure inspection latency separately from preview refresh time. If the preview falls behind, discarding obsolete display updates can be preferable to delaying inspection results.

Know when an overlay is the wrong fix

Transparency cannot repair a registration error, stale frame pairing or inconsistent exposure. Hard replacement cannot make differently calibrated views geometrically interchangeable. Stop at the failing input check rather than hiding it with a visually pleasing blend.

Images from different viewpoints may require registration, rectification or a scene model before comparison. Images of moving parts need appropriate timing. Those are separate tasks from combining already aligned arrays.

Likewise, a threshold mask should not be treated as evidence that a defect measurement is correct. Validate the segmentation and measurement on the original data, then use the overlay to explain the result.

Before ordering or deploying, confirm:

  • The intended operation is blending, replacement or annotation.
  • Image dimensions, channels, ranges and coordinate mappings are documented.
  • Source buffers remain unchanged and valid for their required lifetime.
  • The application retains correct image-to-result associations under load.
  • Camera, processing hardware and runtime compatibility have been checked together.

Start with the camera configuration step when acquisition hardware is still undecided. For an existing installation, send representative image formats, frame timing and processing requirements through application support before selecting additional hardware.

Questions

Should I use transparency or a binary mask for an image overlay?

Use transparency when both source images should remain visible for comparison. Use a binary mask when each output pixel must come entirely from one source. Neither operation establishes alignment, and neither should modify the original image used for inspection measurements.

Can I overlay a grayscale image on a color image?

Yes, after an explicit conversion to compatible channel layouts and intensity scales. You can replicate grayscale values across the color channels or apply a defined display mapping. Keep that converted display image separate from the grayscale source used for inspection.

Why is a mask displaced when both images have the same dimensions?

Equal array dimensions do not guarantee matching coordinates. Check crop offsets, rotations, mirroring, indexing conventions and acquisition timing. Also verify that the mask belongs to the displayed frame rather than an earlier acquisition whose processing finished late.

Can the blended image be used for defect measurement?

Keep quantitative processing on the original image unless the compositing operation is deliberately part of a validated measurement method. Blending changes intensities, while hard replacement substitutes source data. A preview that looks useful can therefore produce misleading thresholds or brightness measurements.

Does creating an overlay require recalibrating the camera?

Display composition does not inherently require new calibration when the source images and their geometry remain unchanged. If you crop, resize, rotate or rectify a display, maintain the appropriate coordinate mapping for annotations. Separately acquired views may require registration before any meaningful overlay.