All writing
Software & Data2 min read

Your first OpenCV part-inspection script

You don't need a Cognex system to catch a missing hole or a flipped part. A fixed camera and a bit of OpenCV handle a surprising amount of real inspection. Here's the first script.

A USB webcam on a desk

Machine vision has a reputation for being expensive and hard. The expensive-and-hard version is real — but a huge amount of everyday shop inspection is just "is the feature there or not," and that you can do with a $40 webcam and a few lines of OpenCV. The trick is to start with the boring, high-value check, not the hero project.

The simplest useful inspection: presence / absence

The pattern: fix the camera so the part always lands in the same place, define a region of interest (ROI) where the feature should be, threshold the image to black-and-white, and check whether the feature is there. Missing hole, absent clip, flipped part — all the same shape of problem.

import cv2

img = cv2.imread('part.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# black-and-white: features become white blobs on black
_, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)

# find the blobs and keep only meaningfully-sized ones
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)
holes = [c for c in contours if cv2.contourArea(c) > 200]

expected = 4
print('PASS' if len(holes) == expected else 'FAIL',
      f'({len(holes)} of {expected} holes found)')

That's a working go/no-go check. Count the holes, compare to what the drawing says, and flag the part. Everything more sophisticated is a refinement of this loop.

Lighting is 80% of the job

The number-one reason a vision project fails isn't the code — it's inconsistent lighting. A cheap ring light and a fixed camera position will do more for your reliability than any clever algorithm. Control the light before you tune a single threshold.

From presence/absence to measurement

Once you can find a feature, measuring it is a short step: calibrate how many millimeters a pixel represents (image a known gauge once), and a bounding box becomes a dimension. It won't replace your CMM or a real uncertainty budget — camera measurement carries its own error — but for a quick in-line "is this bore roughly right," it's plenty.

When rule-based isn't enough

Thresholds and contours are perfect for structural checks — presence, count, position. They fall apart on cosmetic defects: scratches, stains, subtle surface flaws where "good" and "bad" look almost identical. That's where you graduate to a trained model (modern tools learn a defect from under 200 sample images). But don't start there. Solve the presence/absence checks first — they're most of the value and none of the complexity.

The best first vision project is the dull one: the check a person does a thousand times a shift and gets bored enough to miss. Boredom is the defect source; the camera never blinks.

If you've got an inspection that's eating an operator's attention, it's often a half-day of OpenCV away from being automatic. Happy to talk through whether it's a fit.

Muerus Rodrigues

Applications Engineer

Get in touch

Keep reading

Home
Blog
Email
LinkedIn
Résumé