System designGuide

Binary image classification for defect detection: where to start

How to start flagging defects with binary image classification: image counts, capture set-up, pixel size, precision and recall, and the parts to order first.

LumaSpec Vision12 min read Share on LinkedIn

On this page 10 sections
  1. 01Should your first defect project be a binary classifier?
  2. 02How many labeled images do you need to start?
  3. 03What does the capture set-up need to look like?
  4. 04How many pixels does your smallest defect need?
  5. 05How do you measure pass/fail performance honestly?
  6. 06Which model do you train first, and on what hardware?
  7. 07When is binary classification the wrong tool?
  8. 08What do you check before ordering parts?
  9. 09How does the pass/fail decision reach your line?
  10. 10Questions

A first deep-learning vision project usually dies one of two ways: it starts with a network too big for the data, or it never leaves the whiteboard. This guide commits you to the smallest project that still ships — binary image classification, pass or fail, on images of one part family. You will size the dataset, freeze the capture conditions, check the pixel math, train a small CNN, read precision and recall without fooling yourself, and order camera hardware that fits the line. The same pipeline extends later to multi-class sorting, localization and anomaly detection, so nothing here is throwaway work.

Should your first defect project be a binary classifier?

Yes. Binary classification — defective or OK, one image in, one bit out — is the smallest complete machine-learning project: it walks you through data collection, labeling, training, honest metrics and deployment, and a first working model needs hundreds of images, not tens of thousands.

Generative models, GANs and reinforcement learning are research programs; a pass/fail classifier is a deliverable. Ground truth is trivial — the part either ships or it does not — so you score every model output against reality the same day. That fast feedback loop is what a newcomer to machine learning needs, and it is what a production line needs.

Binary classification is also the base of the tower. Capture, label, split, train, measure — that pipeline reappears in anomaly detection, multi-class sorting and defect localization. Start here and nothing is wasted; start with a diffusion model and the first month produces pictures instead of decisions.

For the project around the model — station layout, budget, scope discipline — the guide to planning a first vision inspection station covers the station side.

How many labeled images do you need to start?

Plan 100 to 300 labeled images per class as the starting target. Around 100 usually trains a first model that clearly beats guessing; by 300 you cover part rotation, finish variation and borderline defects. Split each class 70/20/10 into training, validation and test sets.

Do the split inside each class: 200 defective and 200 OK images become 280 training, 80 validation and 40 test images, because 200 × 0.7 = 140, 200 × 0.2 = 40 and 200 × 0.1 = 20 per class. Guard the boundaries — the moment a test image also appears in training, every metric you report is fiction.

Keep the classes roughly balanced for training even if production runs 95% good parts: oversample the defects so the model sees both classes equally, then measure on a test mix that matches production. Capture across more than one shift, operator or material lot where you can — the per-condition generalization check documents how models fail quietly on conditions the training set never contained.

The deeper arithmetic, including when fewer than 100 is enough, lives in how many samples deep-learning defect inspection really needs.

What does the capture set-up need to look like?

Fix every condition you can before the first capture session — same light, same exposure, same part presentation — because a CNN happily learns a dimmer afternoon as a defect signature and fails the first time the sun hits the line.

Build a shroud or booth so room lighting never reaches the part. Pick one lighting geometry for the defect family — a ring light for flat surface marks, a dark-field bar for raised edges and recesses — and never change it between classes or sessions. Trigger from a presence sensor so every part lands in the same frame position, and save images at full sensor resolution; downscaling discards exactly the pixels the next section is about. Drifting room light is the classic silent killer, and the ambient-light control guide for pick-and-place vision shows how far a window alone can move a measurement.

Capture both classes in one session, with the same settings, on the same day. A model trained on Monday's rejects and Tuesday's good parts has learned the days, not the defects.

If the deployed station will be a standalone sensor, capture with the device you will deploy: smart cameras and vision sensors run the inspection on the device, so training images come from the same optics that judge production. If you will train on a PC and swap lenses while the field of view settles, start from the area scan cameras and pick a C-mount model.

Three fixed-mount options bracket the range, from an entry vision sensor to a smart camera with its own integrated light.

Smart cameras & vision sensorsHikrobot MV-SCB007EC-12S-WBN SCB vision sensor, 1.6 MP, Color, 12 mm lens, Manual focusMV-SCB007EC-12S-WBN
Interface
Fast Ethernet
Resolution
Up to 3 MP
$1,989.00 · quotedView and quote
Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
$1,629.00View part
Smart cameras & vision sensors1.6 MP smart camera, 60 fps, 16 mm lens, white light (polarized, transparent light) (LS-SMC-0063)LS-SMC-0063
Interface
GigE
Resolution
Up to 3 MP
$1,110.00View part

The MV-SCB007EC-12S-WBN is the entry point, a 1.6 MP color vision sensor with a 12 mm manual-focus lens. The MV-SC5060XM-08M-WBN steps up to a 6 MP mono sensor with an 8 mm lens on GigE, which buys margin for smaller defects at the same field of view. For a training bench, the MV-CU020-90GC pairs a 2.5 MP global-shutter color sensor with a C-mount and GigE — the flexible start while the optics are still moving.

Area scan camerasHikrobot MV-CU020-90GC 2.5 MP Area Scan Camera, GigE, GMAX4002, ColorMV-CU020-90GC
Sensor format
1/1.7"
Lens mount
C-mount
Interface
GigE
Resolution
Up to 3 MP
$309.00View partBuild a system with it

How many pixels does your smallest defect need?

Size the camera so the smallest rejectable defect spans at least 3 pixels, and 6 to 10 when the mark is low-contrast; pixels on the part, not megapixels, decide this, so divide the field of view by the sensor's pixel count and test the defect against that number.

mm per pixel = field of view width / pixels across the sensor pixels per defect = defect size / mm per pixel

Worked example. The smallest rejectable scratch is 0.25 mm and the part needs a 48 mm-wide field of view. A 2448-pixel-wide sensor, typical of the 5 MP class, gives 48 / 2448 = 0.0196 mm per pixel, about 20 µm. The scratch spans 0.25 / 0.0196 ≈ 13 pixels, comfortably above the floor. Turned around, this sensor could stretch to 0.25 / 3 × 2448 ≈ 204 mm of field width before the scratch drops below 3 pixels — the field-of-view ceiling for that defect.

Put your own field of view and defect size into the resolution calculator below.

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

The same arithmetic for cracks, worked through per defect for both area scan and line scan, is in the pixels-per-defect camera sizing walkthrough.

When the numbers will not close — the defect needs 6 pixels and the field of view cannot shrink — the fix is more pixels. Stepping from a 2.5 MP class camera to a 20 MP one multiplies the pixel count by 8, so the ground sample distance improves by √8 ≈ 2.8 and a 13-pixel scratch reads at about 37 pixels across the same field of view. The MV-CS200-10UM is that class of camera: 20 MP, mono, USB 3.0, a 1-inch IMX183 sensor.

Area scan camerasHikrobot MV-CS200-10UM 20 MP Area Scan Camera, USB3.0, IMX183, 2nd Gen., MonoMV-CS200-10UM
Sensor format
1"
Lens mount
C-mount
Interface
USB3
Resolution
16-25 MP
$759.00View partBuild a system with it

How do you measure pass/fail performance honestly?

Report precision, recall and F1 on the defective class from a test set the model has never seen; accuracy alone lies when one class dominates, and a line running 95% good parts scores a do-nothing model at 95% accuracy while it catches zero defects.

precision = TP / (TP + FP) recall = TP / (TP + FN) F1 = 2 × precision × recall / (precision + recall)

Worked example. The test set holds 500 images: 100 defective, 400 OK. The model rejects 90, and 80 of those are true defects, so TP = 80, FP = 10, FN = 20, TN = 390.

  • Precision = 80 / (80 + 10) = 89% — one rejected part in nine was actually good.
  • Recall = 80 / (80 + 20) = 80% — one real defect in five escaped.
  • F1 = 2 × 0.89 × 0.80 / (0.89 + 0.80) = 84%.
  • Accuracy = (80 + 390) / 500 = 94%, the least informative of the four.

Precision prices the false rejects — good parts scrapped or reworked, lines stopped without cause. Recall prices the escapes. The model's output score lets you slide the decision threshold between them; in most plants an escaped critical defect costs an order of magnitude more than a false reject, so tune for high recall first and tighten until false rejects become the louder pain.

Which model do you train first, and on what hardware?

Start with a small CNN or a fine-tuned standard backbone such as ResNet18; on 100 to 300 images per class it trains in minutes and beats anything built from scratch, a mid-range desktop GPU is enough at this scale, and a CPU-only box works if you accept training runs that take a full day instead of minutes.

Fine-tune rather than build from scratch: take a backbone pretrained on natural images, replace the final layer with your two outputs, and train at a small learning rate. Public defect-classification projects do exactly this with ResNet18 and report accuracies above 99% on casting X-ray sets; read those numbers as proof the approach works, not as a target your line will reproduce.

Watch the gap between training and validation scores. A model that fits the training set perfectly but lands near 85% on validation has memorized, not learned — answer with more images or augmentation, not more layers. At this dataset size, crop consistency and honest splits matter far more than the architecture.

On hardware, the CPU-versus-GPU setup guide for deep-learning vision carries the full decision; the short version is that a few hundred images train on any modern machine, and a GPU compresses a day of experimentation into about an hour.

When is binary classification the wrong tool?

Reach for another tool when the answer is not one bit: if you must locate the defect, sort several defect types, or judge parts with no defect samples at all, a two-class classifier is the wrong shape — match the tool to the decision your process needs.

  • The check is really a threshold — hole present, region too dark, seal too short. Write the rule first and reach for learning only when the rule cannot hold tolerance; rule-based versus AI smart-camera inspection sets out that split, and a live binary image preview shows a thresholded check running directly.
  • The line needs the defect's position, for marking, rework or robot guidance. Classification answers what, never where; you need localization or segmentation, and the feasibility checklist before teaching vision to locate a part tells you whether the part can be found reliably at all.
  • Defective parts are too rare to collect. Train an anomaly detector on good images only and threshold its heatmap into pass/fail; the ONNX anomaly-detection workflow runs that path from map to decision.
  • Several defect types need different dispositions — rework versus scrap versus release. Add classes incrementally once the binary loop works end to end; do not open at five.

What do you check before ordering parts?

Confirm three numbers before anything goes in the cart: the field of view your largest part needs, the pixels your smallest defect needs, and the cycle time the station must hold; every other camera specification follows from those three decisions.

  • Field of view: the part plus about 10% positioning margin, measured at the working distance your fixture actually allows.
  • Pixels on defect: at least 3 across the smallest rejectable defect, 6 to 10 for low-contrast marks; the worked example above is the arithmetic.
  • Shutter: global if parts move or vibrate under the camera; rolling is fine on a static fixture.
  • Sensor color: mono unless the defect is itself a color change, such as a stain or a scorch mark.
  • Interface: GigE for runs beyond a few meters or shared cable trays; USB 3.0 on the bench.
  • Trigger input: a presence sensor keeps framing identical across 600 images and 600,000 parts alike.

Feed those numbers into the camera configurator or narrow the catalog with the cameras finder; both filter by resolution, interface and shutter, so the shortlist arrives already scoped.

How does the pass/fail decision reach your line?

Ship the decision, not the images: one pass/fail bit to the PLC over Ethernet or digital I/O, a running reject counter, and the last rejected image on the operator screen; a smart camera inspects on the device with no PC whenever the check fits a single sensor.

Keep the interface contract boring: the vision system reports results, and the PLC decides what the machine does. The results-not-images pattern for vision-to-PLC integration covers the handshake and its failure modes; boring is the goal, because boring survives a plant. Heavier stations — several cameras, geometry checks, traceability — move up to a PC or one of the vision controllers, but the contract does not change.

Then keep the loop closed. Log every rejected image with the operator's confirmation, review the counters weekly, and retrain when recall drifts. Deployed classifiers rarely die in code; they die in drifted lighting and drifted part presentation, and both announce themselves as false rejects first — which is why the reject split matters more than the pass total.

When the metrics and the hardware line up, capture the first 100 images per class in a single session and you will hold a trained model with honest numbers inside a month. Free expert support comes with every order, the contact page reaches the team for application questions, and the systems guides hub collects the deployment-side articles this one leans on.

Questions

Is 99% accuracy a good result for a defect classifier?

Not on its own. With 95 good parts in every 100, a model that answers good every time scores 95% accuracy while catching nothing. Ask instead for the confusion matrix behind the number, and track the false-reject rate on known-good production — it is the earliest signal that a deployed model is drifting.

Mono or color camera for surface defect detection?

Mono when the defect appears as a brightness change — scratches, dents, cracks, missing material. Color when the defect is itself a color change: stains, scorch marks, misprints. A mono sensor of the same family usually resolves a little more detail, because no color filter sits in front of its pixels to split the light.

What if I have almost no defective samples to photograph?

Pull rejects from the QA hold area or the scrap bin first — even 30 to 50 borderline parts teach the boundary better than synthetic defects. If defective parts genuinely never reach you, switch to an anomaly detector trained on good parts only; it needs no defect images, and its score threshold becomes the pass/fail call.

Can a smart camera train its own model?

No — training stays a PC job, where the GPU and your labeled dataset live. Smart cameras and vision sensors are built to run a trained inspection on the device: you capture and train on the PC, then move the finished decision onto the camera, which answers pass/fail at the line with no PC attached.

How long does labeling 600 images actually take?

Budget half a day. At roughly five to ten seconds per crop in a simple labeling tool, 600 images is one to two hours of clicking; ambiguous defects and a second review pass of the test set roughly double that. This is exactly why 100 to 300 images per class is the right first target — the whole set stays labelable inside a day.