Introduction

Dimensional and deformation measurement of mechanical components underpins manufacturing inspection, experimental mechanics, and product validation. Traditional contact instruments such as Vernier calipers, micrometers, dial gauges, and CMM touch probes are reliable for rigid components but have three practical limitations. First, the instrument contacts the specimen, applying a probing forsce that can deform flexible or miniaturized components. Second, each sensor measures one point at a time, making it impossible to capture simultaneously correlated multi-point displacements. Third, high-accuracy CMMs cost significantly more than mobile optical alternatives, restricting access in academic and small-industry settings.

Non-contact optical measurement using cameras and image processing avoids all three limitations. A calibrated camera captures all visible surface points simultaneously in each frame without applying any mechanical load. With appropriate sub-pixel algorithms, dimensional accuracy in the sub-millimetre range is achievable with commercially available mobile phone cameras and free software [1], [2]. Despite these advantages, commercial vision measurement and DIC software (HALCON, Vic-2D, GOM Aramis) carry per-seat license costs that limit their use in teaching laboratories and small engineering workshops.

Two freely available open-source platforms remove this barrier. Python OpenCV [3] is a cross-platform computer vision library providing calibration, homography, optical flow, and sub-pixel edge detection; all required for a complete 2D planar measurement pipeline — at zero cost. MATLAB NCORR [4] is an open-source Digital Image Correlation package that computes full-field displacement and strain maps from speckle-pattern images with accuracy comparable to commercial DIC platforms.

This paper complements the authors' companion review published in Micromachines [5], which examined 2D vision measurement specifically for a Single-Input Dual-Output Compliant Displacement Amplification Mechanism (SIDO-CDAM) [6]. The present paper restricts its scope to general planar mechanical components such as, flat rectangular parts, cylindrical fasteners, threaded screws, gears, and standard beam specimens; which represent the far more common measurement targets in undergraduate laboratories, workshop inspection, and industrial quality control. The paper makes five contributions: (i) a focused review of Python OpenCV for 2D dimensional and displacement measurement; (ii) a review of MATLAB NCORR for full-field DIC strain measurement; (iii) experimental validation of the OpenCV pipeline on two component types using a mobile phone camera, with quantitative accuracy data; (iv) documentation of the thread pitch measurement limitation at standard mobile phone magnification and a proposed Hough Transform solution; and (v) proposal of an integrated two-tool measurement workflow for general mechanical components.

This paper is organized as follows. Section 2 reviews Python OpenCV. Section 3 covers MATLAB NCORR. Section 4 presents experimental validation. Section 5 benchmarks both tools against alternatives. Section 6 discusses applications, limitations, and research gaps. Section 7 concludes.

Python OpenCV for 2D Dimensional and Displacement Measurement

Overview

OpenCV (Open Source Computer Vision Library) [3] is a BSD-licensed, cross-platform library providing over 2,500 optimized algorithms for image processing and computer vision. Freely installable via the Python package manager (pip install opencv-python), it provides camera calibration [7], homography-based world-plane reconstruction, Lucas-Kanade pyramidal optical flow tracking [8], and sub-pixel edge localization: all required for a complete 2D measurement pipeline at no software cost. It is used in peer-reviewed academic measurement studies [1], [2], [9] and industrial inspection applications [10], [11]

Camera Calibration

Camera calibration estimates the intrinsic parameters — focal length, principal point, and lens distortion coefficients — and extrinsic parameters (rotation and translation between camera and world frames). Zhang's checkerboard method [7], implemented in OpenCV as cv2.calibrateCamera(), recovers these from 15–25 images of a printed checkerboard captured from different orientations. Distortion correction is important for measurements spanning the full image field: uncorrected radial distortion can introduce positional errors at the image periphery that reduce dimensional accuracy. A reprojection error below 0.5 pixels is recommended for workshop-level inspection accuracy.

For practical laboratory use where the camera-to-specimen distance changes between sessions a scale-factor calibration provides a simpler starting point. A scale-factor calibration is computing a pixel-to-millimetre conversion factor from one reference object of known size. This approach was used in the experimental validation reported in Section 4. For higher accuracy, ArUco fiducial markers [3] of precisely known size placed alongside the specimen in each image allow automatic per-image scale-factor computation without a separate calibration session. Arellano-González et al. [9] compared homogeneous and non-homogeneous DLT calibration methods and confirmed reliable trajectory tracking for planar mechanisms with low-cost hardware.

Homography-Based Planar Reconstruction

A homography matrix H (3×3, 8 degrees of freedom) maps image-plane pixel coordinates to world-plane physical coordinates:

s·[u,v,1]ᵀ=H·[X,Y,1]ᵀ

(1)

Unlike a fixed pixel-to-millimetre scale factor, homography compensates for perspective distortion and magnification non-uniformity across the image. Wu et al. [1] demonstrated that SVD decomposition of H recovers translational and rotational motion components with higher accuracy and repeatability than scale-factor methods on a robotic stage. For mobile phone cameras, which typically have some barrel distortion and perspective variation across the field; homography-based back-projection is more robust than a fixed scale factor, particularly for components spanning a significant fraction of the image field.

Camera Motion Compensation

Any movement of the mobile phone camera between image captures — vibration, accidental nudging, thermal drift — appears as false displacement in the measurement. Jiao et al. [12] proposed placing fixed reference markers on the stationary surface adjacent to the specimen; the apparent motion of these markers per frame represents unintentional camera motion, which is subtracted from all tracked feature positions. This compensation requires no additional hardware and is particularly relevant for mobile phone measurements where the camera is handheld or loosely mounted.

Adaptive Gaussian Thresholding for Metallic Components

Converting the greyscale image to a binary (object/background) image for contour extraction is a critical preprocessing step. Global thresholding (Otsu's method) works well for uniform-coloured specimens but fails for metallic components — machine screws, bolts, machined aluminium parts — where semi-specular surfaces produce locally varying brightness under standard room lighting. Bright specular reflections and dark thread valleys can both be incorrectly classified, producing a fragmented or incorrect silhouette.

Adaptive Gaussian Thresholding (cv2.adaptiveThreshold() with ADAPTIVE_THRESH_GAUSSIAN_C) computes a different threshold for each local pixel neighbourhood (typically block size 11, constant C = 2) based on the Gaussian-weighted average of surrounding pixels. This local adaptation handles large-scale brightness non-uniformity reliably, producing clean closed silhouettes of metallic components under standard room lighting — conditions typical of a mobile phone measurement setup. A morphological closing operation (3×3 kernel, two iterations) applied after thresholding fills residual gaps in the binary outline caused by thread root shadowing.

Geometry Fitting for Dimensional Extraction

After contour detection (cv2.findContours()), two bounding constructs extract dimensions from the largest contour. The axis-aligned bounding rectangle (AABR, cv2.boundingRect()) defines the region for thread analysis. The minimum area rotated rectangle (MARR, cv2.minAreaRect()) fits the smallest enclosing rectangle at any angle, providing orientation-independent length and diameter: the larger dimension equals total length and the smaller equals outer diameter, regardless of component orientation in the image. Thread length is estimated from the AABR of the threaded zone; thread pitch is estimated via horizontal edge projection over the threaded region.

Sub-Pixel Edge Detection

Sub-pixel edge detection improves dimensional accuracy beyond the native pixel resolution. Xie et al. [11] proposed a two-stage hybrid: Roberts operator for coarse pixel-level edge detection followed by improved Zernike moment fitting for sub-pixel refinement, with Otsu's method for automatic threshold selection. This combination achieves better accuracy and lower computational cost than pure Zernike methods and is particularly suited to straight or mildly curved edges typical of machined components. Cheng et al. [12] demonstrated 3 µm accuracy on shaft parts using Canny detection with Hessian-matrix sub-pixel localisation (Canny-Steger method). For mobile phone measurements at mm-level accuracy, standard sub-pixel refinement through cv2.cornerSubPix() is sufficient, achieving localization to within 0.1–0.2 pixels.

Accuracy at Mobile Phone Resolution

Table 1 summarizes expected dimensional accuracy for different optical configurations. At mobile phone magnification (PIXEL_TO_MM ≈ 0.05 mm/pixel for a 30 cm working distance), sub-pixel precision of 0.1–0.2 pixels translates to 0.005–0.010 mm absolute pixel-level resolution. However, additional contributions from scale-factor calibration error, perspective distortion, and lighting non-uniformity bring the practical measurement uncertainty to the sub-millimetre range — confirmed by the experimental results in Section 4.

Table 1. Expected dimensional accuracy as a function of optical configuration. Mobile phone row is derived from the experimental results in Section 4; other rows from reviewed literature [1], [2], [9], [11], [12], [13].

Optical Configuration

Pixel Pitch (mm/px)

Sub-pixel Precision (px)

Practical Accuracy

Primary Application

Standard macro lens, 4 MP, 80 × 80 mm FOV

~0.032

0.05–0.10

0.5–1.0 mm

Structural members, large flat parts

Telecentric 1×, 12 MP, 80 × 80 mm FOV

~0.020

0.05–0.10

0.1–0.5 mm

Precision flat parts, gear profiles

Mobile phone (12 MP, f/1.8), 25–30 cm WD

~0.050

0.10–0.20

0.1–0.8 mm

(<2% of dim.)

Workshop inspection, flat parts, fasteners

Telecentric 2×, 12 MP, 40 × 40 mm FOV

~0.010

0.05–0.10

0.05–0.2 mm

Fine features, small components

Microscopic objective 5×, 5 MP, 5 × 5 mm FOV

~0.001

0.05–0.10

0.001–0.01 mm

MEMS-scale features, micro-structures [13]

MATLAB NCORR for Full-Field DIC Displacement and Strain Measurement

DIC Principle

Digital Image Correlation (DIC) [14] is a non-contact, full-field technique that quantifies deformation by tracking a random speckle pattern applied to the specimen surface. A reference image (unloaded state) is compared with deformed images to recover the displacement field u(x, y) = [u, v] by minimizing the Zero-Normalized Cross-Correlation (ZNCC) criterion:

C=1-∑f·g∑f2·∑g2

(2)

where f and g are the normalized intensity distributions of the reference and deformed subsets. NCORR [4] refines an initial integer-pixel displacement estimate using an iterative Levenberg-Marquardt solver, achieving 0.01–0.05 pixel displacement accuracy. Its Reliable Filtering Algorithm (RFA) computes a quality metric per pixel and masks unreliable regions such as low-texture zones, occluded areas, crack edges which preventing poor correlation data from contaminating the displacement field [4]. NCORR is freely available at https://github.com/justinblaber/ncorr_2D_matlab, runs in MATLAB R2012b and later (including student edition), requires no toolboxes, and provides both a GUI and scriptable API.

Speckle Pattern Preparation

DIC requires the specimen surface within the region of interest (ROI) to carry a random, isotropic, high-contrast speckle pattern with speckle size of 3–7 pixels in the acquired image. Speckles smaller than 2–3 pixels cause correlation noise; speckles larger than 7–10 pixels reduce the spatial resolution of the strain field [14], [15]. Table 2 summarizes recommended preparation methods by component scale.

Table 2. Recommended speckle pattern preparation methods by component scale [14], [15]

Scale

Method

Application Procedure

Notes

> 50 mm

Aerosol spray

White primer + matte black spray at 30–50 cm

Accessible; adequate for tensile and bending tests

10–50 mm

Airbrush

White primer + black airbrush; 2–3 passes

Finer speckle; practice required

2–10 mm

Ink stamp / adhesive film

Randomized rubber stamp or inkjet-printed film

Low mass addition; check adhesive stiffness

< 2 mm

Lithographic

Photoresist or e-beam deposition [16]

Requires clean-room; reusable for repeated tests

NCORR Processing Workflow

The NCORR pipeline comprises five steps: (i) loading the reference and deformed image sequence; (ii) drawing the ROI over the specimen area of interest; (iii) setting correlation parameters — subset radius r (15–25 pixels), strain radius e (10–15 pixels), step size s (1–5 pixels); (iv) executing the correlation analysis with Levenberg-Marquardt sub-pixel refinement; and (v) computing strain fields by local polynomial fitting — more noise-robust than finite differencing. Fig. 1 illustrates the NCORR workflow on a representative mechanical component.

Fig. 1. MATLAB NCORR DIC workflow. (a) Experimental setup: fixed camera, speckle-coated specimen; (b) reference image at zero load; (c) deformed image under load; (d) NCORR GUI with ROI selection; (e) horizontal displacement field U(x,y); (f) axial strain field εxx revealing stress concentration. The same workflow applies to flat parts, fasteners, gears, and beams.

Strain and Stress Computation

From displacement fields u(x,y) and v(x,y), NCORR computes strain components εxx, εyy, γxy using the linearized small-strain approximation for elastic engineering applications. For isotropic linear elastic materials under plane stress:

σxx=E(εxx+νεyy)1-ν2,σyy=E(εyy+νεxx)1-ν2,τxy=Eγxy2(1+ν)

(3)

The Von Mises stress σVM=σxx2-σxxσyy+σyy2+3τxy2identifies the location and magnitude of maximum stress concentration, comparable to FEA predictions. For steel (E = 200 GPa, ν = 0.30) and aluminium (E = 70 GPa, ν = 0.33), stress fields are computed directly from NCORR-exported strain matrices using standard material constants.

Accuracy and Comparison with Commercial DIC

Table 3 compares NCORR with representative commercial DIC platforms and the emerging Python-based iCorrVision-2D.

Table 3. Comparison of NCORR with commercial DIC platforms [4], [14], [15], [16]. µε = microstrain.

Criterion

NCORR

Vic-2D

GOM Aramis

iCorrVision-2D [16]

Software cost

Free

Commercial ($)

Commercial ($$$)

Free

Platform

MATLAB (license)

Windows/Mac

Windows

Python (free)

GUI

Yes — full GUI + API

Yes (full)

Yes (full)

Yes (limited)

Displacement accuracy

0.01–0.05 pixel

0.01–0.02 pixel

0.01–0.02 pixel

0.02–0.08 pixel

Strain accuracy

50–200 µε

30–100 µε

30–100 µε

100–300 µε

Experimental Validation on Standard Mechanical Components

Experimental Setup

To validate the OpenCV pipeline, experiments were conducted on two standard mechanical component types using readily available laboratory equipment. A 12 MP mobile phone camera (f/1.8 aperture) was positioned approximately 25–30 cm above the specimen, perpendicular to a white A4 paper background, under diffuse room lighting. A scale-factor calibration of PIXEL_TO_MM = 0.05 mm/pixel was established from a reference object of known dimensions. Images were standardized to 700 × 1000 pixels before processing.

The processing pipeline applied to each specimen was: (1) grayscale conversion; (2) Gaussian blur (5 × 5 kernel, σ = 0); (3) adaptive Gaussian thresholding (block size 11, C = 2); (4) morphological closing (3 × 3 kernel, two iterations); (5) largest-contour selection (minimum area 3,000 px²); (6) MARR fitting for total length and outer diameter; (7) AABR-based thread zone extraction; (8) horizontal edge projection for thread pitch; (9) calibration conversion and annotated output display. Fig. 2 illustrates the complete pipeline.

Fig. 2. OpenCV measurement pipeline applied to a planar mechanical component. (a) Mobile phone image acquisition; (b) adaptive Gaussian thresholding result; (c) morphological closing; (d) contour detection and MARR fitting; (e) dimensional extraction and calibration; (f) annotated output image with measurements.

Phase I: Flat Rectangular Component

A scientific calculator was selected as a representative flat rectangular component with a well-defined outline and known nominal dimensions, verified by Vernier caliper measurement (length = 162.00 mm, width = 77.00 mm). Flat rectangular components represent the simplest and most common inspection target: injection-moulded housings, sheet metal brackets, PCB outlines, and flat plates. Table 4 reports the measurement results.

Table 4. Phase I experimental results — flat rectangular component (scientific calculator). Mobile phone camera at 30 cm; PIXEL_TO_MM = 0.05 mm/pixel; scale-factor calibration.

Parameter

Manual Vernier (mm)

Vision System (mm)

Abs. Error (mm)

% Error

Length

162.00

161.40

0.60

0.37

Width

77.00

76.20

0.80

1.04

Projected Area (mm²)

12,474

12,301

173

1.39

All parameters were within 1.4% of the Vernier reference, confirming that the adaptive thresholding and MARR pipeline functions correctly for flat rectangular components. Absolute errors of 0.60 mm (length) and 0.80 mm (width) are consistent with the expected sub-millimetre accuracy of a mobile phone measurement at 0.05 mm/pixel pixel pitch. Fig. 3 shows the annotated output.

Fig. 3. Phase I annotated output — flat rectangular component. OpenCV MARR-fitted bounding rectangle overlaid on image with dimension annotations: length 161.40 mm, width 76.20 mm, area 12,301 mm².

Phase II: Threaded Cylindrical Fastener

A stepped metallic machine screw — with a smooth cylindrical shank section and an external V-thread section — was selected as a representative threaded cylindrical fastener. This is a more demanding specimen than the flat component because: (i) the metallic surface is semi-specular under room lighting; (ii) the geometry combines multiple features (total length, outer diameter, thread length, thread pitch); and (iii) the thread profile produces an irregular silhouette. Adaptive Gaussian thresholding successfully produced a clean, closed silhouette where global Otsu thresholding failed. Table 5 reports the results.

Table 5. Phase II experimental results —metallic machine screw (threaded cylindrical fastener). Mobile phone camera at 25–30 cm; PIXEL_TO_MM = 0.05 mm/pixel. *Thread pitch error is discussed in Section 4.4.

Parameter

Manual Vernier (mm)

Vision System (mm)

Abs. Error (mm)

% Error

Total Length

33.85

34.10 (Vernier)

0.25

0.73

Outer Diameter

10.70

10.80 (Vernier)

0.10

0.93

Thread Length

16.95

16.50 (estimated)

0.45

2.73

Thread Pitch *

0.16

1.50 (M10 standard)

—

>89% — see 4.4

Projected Area (mm²)

232.89

—

—

—

Total length and outer diameter were both measured within 1% of Vernier references — absolute errors of 0.25 mm and 0.10 mm respectively. Thread length agreed within 2.73%. These results confirm that the adaptive thresholding and MARR pipeline extends reliably to metallic cylindrical components. Fig. 4 shows the annotated output.