LabVIEW Machine Vision and Image
Processing Course
Day Two
National Instruments Confidential
Lesson 5
Creating and Processing Binary Images
TOPICS
Collecting image information with histograms
Performing a threshold
Primary binary morphology functions in IMAQ Vision
Particle measurements
National Instruments Confidential
Using Histograms and Thresholds
• Histograms
– Provide a quantitative distribution of pixels in an image
– Indicate the number of pixels at each gray level
– Limit subjectivity
• Thresholds
– Convert all pixel values in an image to 0 or 1,
according to the value of the original pixel
– Help extract significant structures in an image
National Instruments Confidential
Histograms vs. Thresholds
Original Threshold
Histogram
National Instruments Confidential
What Is a Binary Image?
• An image segmented into two regions during the
thresholding process
– Particle region (pixels equal to 1)
– Background region (pixels equal to 0)
National Instruments Confidential
Histograms vs. Histographs
• Histogram • Histograph
National Instruments Confidential
Setting Up Your Threshold Filter
• You can set an upper and lower limit for pixel values
in your image
• Pixels inside the bounds of the limit are set to a value
of 1, and pixels outside the bounds of the limit are
set to a value of 0.
• Two types of threshold filters:
– Binarizing threshold filter: If a pixel falls within the bounds, the
filter converts the pixel value to 1; all other pixels are converted
to 0, creating a binary image.
– Bandpass threshold filter: If a pixel falls within the bounds, the
pixel retains its current value; all other pixels are converted to 0
National Instruments Confidential
Exercise 5.1
Histogram Grab VI
Time to complete: 10 min.
To create a histogram from a live image acquisition.
OBJECTIVE
National Instruments Confidential
Exercise 5.2
Histogram and Threshold Grab VI
Time to complete: 15 min.
To apply a threshold to a live image acquisition.
OBJECTIVE
National Instruments Confidential
Exercise 5.3
Threshold in IMAQ Vision Assistant
Time to complete: 10 min.
To use histograms and thresholds in IMAQ Vision
OBJECTIVE
Assistant
National Instruments Confidential
Morphology
• Morphological functions remove unwanted
information caused by the thresholding process:
– Noise particles
– Particles touching the border of an image
– Particles touching each other
– Particles with uneven borders
• Binary morphological functions extract and alter
the structure of particles in a binary image
National Instruments Confidential
Primary Morphology Functions
• The primary morphology functions are erosions
and dilations
• A single IMAQ Vision VI (IMAQ Morphology)
performs all primary morphology operations:
– Erosions
– Dilations
– Openings
– Closings
National Instruments Confidential
Advanced Morphology Functions
• Advanced morphological functions are combinations of
multiple VIs, each of which is responsible for a single
operation
• These functions execute the following tasks:
– Separate particles
– Remove small or large particles
– Keep or remove particles identified by morphological parameters
– Fill holes
– Remove particles from an image border
– Create an outline of particles
National Instruments Confidential
Particles and Connectivity
• Particle in a binary image
– Typically, a group of connected white pixels in a black
background
• Connectivity
– Defines whether 2 adjacent pixels are “connected”
– Connectivity-4
– Connectivity-8
National Instruments Confidential
How Connectivity Affects a Particle
• How many particles with
connectivity-4?
• How many particles with
connectivity-8?
National Instruments Confidential
Filtering
• Alters the structure of particles
• Typically used on binary images
• Cleans up noisy images
National Instruments Confidential
Erosion
• Decreases the size of objects in an image
– Removes a layer of pixels along the boundary of the particle
– Eliminates pixels isolated in the background and removes
narrow peninsulas
• Use Erode to:
– Separate particles for counting
– Eliminate one-pixel particles that constitute noise
National Instruments Confidential
Dilation
• Increases the size of objects in an image
– Adds a layer of pixels around the boundary of an object
(including the inside boundary for objects with holes)
– Eliminates tiny holes in objects
– Removes gaps or bays of insufficient width
• Use Dilate to:
– Fill in small holes in a particle
– Calculate the area by counting pixels in a neighborhood
National Instruments Confidential
Dilating and Eroding Particles
Dilate
Erode
National Instruments Confidential
Erode vs. Dilate
Erode
Dilate
National Instruments Confidential
Open
• An erosion followed by a dilation
• Removes small particles and smoothes
boundaries
• Does not significantly alter the size or shape
of particles
– Erosion and dilation are dual transformations
– Borders removed by the erosion process are replaced
by the dilation process
National Instruments Confidential
Close
• A dilation followed by an erosion
• Fills tiny holes and smoothes boundaries
• Does not significantly alter the size or shape
of particles
– Erosion and dilation are morphological complements
– Borders expanded by the dilation process are reduced
by the erosion process
National Instruments Confidential
Making Particle Measurements
• Use these VIs to make particle measurements on a
binary image:
– IMAQ Particle Analysis Report – Returns the number of
particles in an image and a report containing general
measurements, such as area, number of holes, center of
mass, dimensions, etc
– IMAQ Particle Analysis – Returns the number of particles
and a user selectable report containing the most commonly
used measurements
National Instruments Confidential
Exercise 5.4
Morphology in Vision Assistant
Time to complete: 20 min.
To apply a threshold and various morphology techniques
OBJECTIVE
in Vision Assistant.
National Instruments Confidential
Exercise 5.5
Particle Analysis in Vision Assistant
Time to complete: 10 min.
To apply particle analysis techniques in Vision Assistant
OBJECTIVE
and use Vision Assistant’s Create LabVIEW VI feature.
National Instruments Confidential
Lesson 6
Spatial Calibration
TOPICS
Reasons for spatial calibration
The calibration process
Common calibration misconceptions
National Instruments Confidential
Spatial Calibration
• Many machine vision applications require reliable real-
world measurements based on an image
• These measurements are based on the relationship
between the image and the real world
• Accounts for possible errors by constructing mappings for
conversion
• Corrects perspective or nonlinear distortion errors for
image display and shape measurements
• Translates pixels to real world units so you can make
accurate measurements
National Instruments Confidential
Calibration
• Accounts for errors due to:
lens distortion
perspective
known orientation offset
• Transforms image coordinates to real-world
coordinates and back again
National Instruments Confidential
Calibrating Your Image Setup
• Follow these steps to calibrate your imaging setup:
1. Create a calibration template appropriate for your field of view.
2. Acquire an image of the calibration template using your current imaging setup.
3. Input the acquired image, the distances between the dots on the template, and the
location and orientation of the coordinate system to the IMAQ Learn Calibration
Template VI. This VI produces a calibrated image.
4. Acquire an image of the object of interest without the calibration template.
5. Apply the calibration information to the acquired image by copying it from the
calibrated image.
6. Use IMAQ Vision VIs to apply the calibration information to the pixel
measurements.
National Instruments Confidential
Calibration Grid
• Image a calibration template with known real-
world distances between the dots
• Learn the calibration (mapping information)
original template image of the template
National Instruments Confidential
IMAQ Vision Calibration Functions
• Use these IMAQ Vision VIs to calibrate your image:
– IMAQ Set Simple Calibration
– IMAQ Learn Calibration Template
– IMAQ Convert Real World To Pixel
– IMAQ Convert Pixel To Real World
– IMAQ Correct Calibrated Image
– IMAQ Set Calibration Info
National Instruments Confidential
Common Calibration Misconceptions
• Calibration “fixes” any measurement to arbitrary
accuracy
• Calibrated images always need to be corrected
• Calibration can compensate for poor lighting or
unstable conditions
National Instruments Confidential
Exercise 6.1
Calibration
Time to complete: 10 min.
To use IMAQ Vision calibration tools to solve a
OBJECTIVE
perspective or lens distortion problem.
National Instruments Confidential
Lesson 7
Using Machine Vision Techniques
TOPICS
Edge detection
Regions of interest
Pattern Matching
National Instruments Confidential
Programming with IMAQ Vision’s Machine
Vision VIs
Function
Palette
National Instruments Confidential
Machine Vision Sub-palettes
IMAQ Vision’s machine vision VIs are divided into the
following sub-palettes:
– Select Region of Interest
– Coordinate System
– Count and Measure Objects, Measure Intensities,
– Measure Distances
– Locate Edges
– Searching and Matching
– Caliper
– Analytic Geometry
– Instrument Readers
National Instruments Confidential
Edge Detection
• Finds locations of significant intensity
changes within an image
• Works on line profiles
• Detects boundaries of objects quickly
line profile
edge points
National Instruments Confidential
What Is an Edge?
• Contrast—A change in pixel value
• Width—The mean pixel value near an edge
• Steepness—The slope of an edge
Contrast
Edge located
Pixels
Width Steepness Width
National Instruments Confidential
Sub-Pixel Accuracy
• Returns edge detection or pattern matching
information in fractions of a pixel
• Uses interpolation and fitting techniques to estimate
sub-pixel accuracy
• Returns with accuracy of one-fourth of a pixel under
optimal imaging conditions
National Instruments Confidential
Edge Detectors
Low-level VIs based on the high-level IMAQ Simple
Edge VI
Rake, Spoke, Concentric Rake edge detectors for
curved search areas
Configurable search directions, sub-sampling ratios,
and display settings
National Instruments Confidential
Edge Detector VIs
Find Horizontal Edge Find Circular Edge
Find Vertical Edge Find Concentric Edge
National Instruments Confidential
Common Machine Vision Applications
Gauge Count
Detect Locate
HOT
National Instruments Confidential
Clamping
• Searches for the first and last edges
along a rake
• Locates the closest edge from each
side
• Computes the two lines passing
through these points and
perpendicular to the search area
• Computes the distance between the
first and last edges
National Instruments Confidential
Clamping VIs
Clamp Horizontal Max Clamp Vertical Max
Clamp Horizontal Min Clamp Vertical Min
National Instruments Confidential
Gauging
• Make critical dimensional measurements such as lengths,
distance, and diameter to determine if your part is
manufactured correctly
• Use visual inline gauging inspections to verify assembly
and packaging routines
• Use offline gauging to judge product quality according to a
sample of the product extracted from the production line
National Instruments Confidential
Exercise 7.1
Edge Detection and Gauging
Time to complete: 15 min.
To use Vision Assistant to perform edge detection and
OBJECTIVE
use the Caliper function for gauging jumper switches
without any programming.
National Instruments Confidential
Exercise 7.2
Machine Vision Tools Exercise
Time to complete: 15 min.
To measure the circularity, the distance between the
OBJECTIVE
centers of two holes, and the distance between two
vertical edges.
National Instruments Confidential
Regions of Interest
• An area of an image in which you want to focus your
image analysis
• Define your ROI interactively or using one of the following
methods:
– Construct an ROI in an image window
– Construct an ROI with an image window
– Extract an ROI associated with an image window
– Erase the current ROI from an image window
– Transform an ROI into an image mask
– Transform an image mask into an ROI
National Instruments Confidential
Exercise 7.3
Region of Interest Exercise
Time to complete: 15 min.
To program a machine vision application that can snap
OBJECTIVE
an image and perform a useful measurement.
National Instruments Confidential
How Pattern Matching Works
• Locates regions of a
grayscale image that match
a predefined template
– Calculates a score for each
matching region
– Score indicates quality of match
• Locates key components in
a part
– Measures distance or angle
between components in
reference to a predefined
tolerance range
National Instruments Confidential
Creating Good Template Images
• Symmetry • Positional Information
• Feature Details • Background Information
National Instruments Confidential
Two Parts of Pattern Matching
• Learning
– Time-intensive depending on type of match
– Available offline
• Matching
– The VI returns position, rotation, and match score
information
– Match patterns inside an ROI
National Instruments Confidential
How Robust is Pattern Matching?
• Resists changes in
– Pattern orientation
– Ambient lighting conditions
– Blur and noise conditions
• Searches for
– Patterns that may be partially occluded
– Multiple patterns
– Multiple instances of the same pattern
National Instruments Confidential
Improving Match Speed
IMAQ Vision’s pattern matching tool contains two
parameters that you can adjust to influence your
match speed during pattern matching
• Specify a minimum contrast
– If you are searching for a template with high contrast, eliminate
low contrast areas of your image to increase match speed
• Specify a smaller rotation angle range
– If you know that your template is limited to a certain range of
rotation, limit your search to that range
National Instruments Confidential
Pattern Matching Applications
• You can use pattern matching in these general
applications:
– Alignment – Determines the position and orientation of a
known object by locating features. The features are used as
points of reference on the object.
– Inspection – Detects simple flaws, such as missing parts on
illegible printing
– Gauging – Measures lengths, diameters, angles, and other
critical dimensions to determine manufacturing quality
National Instruments Confidential
Exercise 7.4
Pattern Matching in Vision Assistant
Time to complete: 15 min.
To apply pattern matching techniques in Vision
OBJECTIVE
Assistant.
National Instruments Confidential
Exercise 7.5
Find Pattern Application in LabVIEW
Time to complete: 15 min.
To build Find Pattern Application in LabVIEW.
OBJECTIVE
National Instruments Confidential
Lesson 8
Using Color Tools
TOPICS
Basics of color image representation
Color Matching
Color Location
Color Pattern Matching
National Instruments Confidential
About Color
• Color is the wavelength of light we receive in our
eye when we look at an object
• White light is a combination of all colors at once
– You can represent a white light by selecting a few reference
colors and weighting them appropriately
– The most common way to represent white light is with the
RGB primaries (red, green, and blue)
• Color perception depends on hue, saturation,
luminance, and intensity
National Instruments Confidential
Color Matching
• Use color matching for
applications such as:
– Color identification
– Color inspection
– Color object location
– Other applications that
require color comparison
to make decisions
National Instruments Confidential
How Color Matching Works
• Learning color distribution
– IMAQ Vision learns a color distribution by generating a color spectrum
– You provide an image or regions in an image that contain the color
information you want to use as reference
– IMAQ Vision then generates a color spectrum
• Comparing color distributions
– The color spectrum from the object under test is compared to the
reference spectrum
– A match score ranging from 0 to 1000 is computed based on the
similarity between the spectrums
– A score of 0 indicates no similarity between the spectrums, and a
score of 1000 indicates a perfect match
National Instruments Confidential
Color Pattern Matching
• Use color pattern matching if:
– The object you want to locate contains color information very
different from that of the background
– You must find the precise location of the object
– The object you want to locate has grayscale properties that
are difficult to characterize or distinguish from the
background
– The object is similar to others in the image except for color
information
National Instruments Confidential
Color Location
• Use color location to quickly locate known color regions in
an image
• Use color location in applications with the following
characteristics:
– Requires the location and number of regions in an image with their
specific color information
– Relies on the cumulative color information in the region, instead of
how the colors are arranged
– Does not require the orientation of the region
– Does not require the location with sub-pixel accuracy
National Instruments Confidential
How Color Location Works
• Create a template that represents the colors you
are searching
• Your machine vision application searches for the
template in each acquired image, and calculates a
score for each match.
• The score indicates how closely the color
information in the template matches the color
information in the found regions.
National Instruments Confidential
What To Expect From Your
Color Pattern Matching Tool
The color pattern matching tools in IMAQ Vision
can locate reference patterns with accurate results
under these conditions:
– Pattern orientation and multiple instances
– Ambient lighting conditions
– Blur and noise conditions
National Instruments Confidential
Color Tools VIs
• IMAQ Setup Learn Color Pattern
– Sets the parameters that are used during the color learning stage
• IMAQ Learn Color Pattern
– Creates a description of the color template image you are going to
search for during the color pattern matching stage
• IMAQ Setup Match Color Pattern
– Sets the parameters that are used during the color matching stage
• IMAQ Match Color Pattern
– Searches for a color pattern, or color template image, in the image
under inspection
National Instruments Confidential
Exercise 8.1
Color Pattern Matching Exercise
Time to complete: 20 min.
To use color pattern matching to search for a defined
OBJECTIVE
pattern in an image.
National Instruments Confidential
Lesson 9
Coordinate Systems
TOPICS
Reasons for using a coordinate system
How to set up a coordinate system
National Instruments Confidential
Coordinate Systems
• Allow you to define search
Origin of local
areas that can shift and
coordinate system
rotate with the object you are
inspecting
• Defined by a reference point
(origin) and a reference angle
within the image or by the
lines that make up its axes 2 points are
• Based on a characteristic ambiguous
feature of the object under
inspection that is used as a
reference for the
measurements
National Instruments Confidential
Defining A Coordinate System
1. Define a reference coordinate system.
– Locate an easy-to-find reference feature
– Define a search area that encompasses the reference feature you
want to base your coordinate system on
– IMAQ Vision builds a coordinate system based on these
references
2. Set up measurement areas within the reference image in
which you want to make your measurements.
National Instruments Confidential
Defining A Coordinate System
3. Acquire an image of the object you are inspecting.
4. Update the coordinate system.
– IMAQ Vision computes the difference between the reference
coordinate system and the new coordinate system
– Based on this difference, IMAQ Vision moves the new
measurement areas with respect to the new coordinate system
5. Make measurements within the updated measurement
area.
National Instruments Confidential
Coordinate System Functions
• IMAQ Find CoordSys (Rect)
Computes a coordinate system based on the position of an object in a
search area of an image
• IMAQ Find CoordSys (2 Rects)
Computes a coordinate system based on the position of an object in
two search areas of an image
• IMAQ Find CoordSys (Pattern)
Searches for a template image in a rectangular search area of an
image
National Instruments Confidential
Exercise 9.1
Coordinate System Exercise
Time to complete: 20 min.
To define a coordinate system and use it to inspect parts
OBJECTIVE
that may change orientation during inspection.
National Instruments Confidential
Where do you go from here?
• Example programs
• Other NI courses
• Web resources at ni.com
– NI Developer Zone
– Application Notes
– Knowledge Base
• Alliance members
• NI Week 2004
National Instruments Confidential
Technical Support Options
• Web: ni.com/support
– KnowledgeBase
– Application Notes
– Product Manuals
– Much more…
• E-mail: ni.com/ask
• Phone (800) 433-3488
National Instruments Confidential
Please complete the course evaluation.
Thank you for attending.
National Instruments Confidential