Create a Binary Image
Threshold your grayscale or color image to create a binary image. Creating a binary image separates the objects that you want to inspect from the background. The threshold operation sets the background pixels to 0 in the binary image, while setting the object pixels to a non-zero value. Object pixels have a value of 1 by default, but you can set the object pixels to have any value you choose.
You can use different techniques to threshold your image. If all the objects of interest in your grayscale image fall within a continuous range of intensities and you can specify this threshold range manually, use the IMAQ Threshold VI to threshold your image. If all the objects in your grayscale image are either brighter or darker than your background, you can use one of the automatic thresholding techniques in NI Vision. Complete the following steps to use one of the automatic thresholding techniques:
- Use the IMAQ AutoBThreshold 2 VI to select the thresholding technique that automatically determines the optimal threshold range.
- Connect the Threshold Data output to the IMAQ MultiThreshold VI, or use the Lookup Table output to apply a lookup table to the image using the IMAQ UserLookup VI.
If your grayscale image contains objects that have multiple discontinuous grayscale values, use the IMAQ MultiThreshold VI.
If your grayscale image contains objects whose grayscale values vary within the image due to effects such as light drift, use the IMAQ Local Threshold VI.
If you need to threshold a grayscale image that has nonuniform lighting conditions, such as those resulting from a strong illumination gradient or shadows, use the IMAQ Local Threshold VI. You need to define a pixel window that specifies which neighboring pixels are considered in the statistical calculation. The default window size is 32 × 32. However, the window size should be approximately the size of the smallest object you want to separate from the background. You also need to specify the local thresholding algorithm to use. The local thresholding algorithm options are the Niblack or background correction algorithm. Refer to Chapter 8, Image Segmentation, of the NI Vision Concepts Manual for more information about local thresholding.
Automatic thresholding techniques offer more flexibility than simple thresholds based on fixed ranges. Because automatic thresholding techniques determine the threshold level according to the image histogram, the operation is less affected by changes in the overall brightness and contrast of the image than a fixed threshold. Because these techniques are more resistant to changes in lighting, they are well suited for automated inspection tasks.
If you need to threshold a color image, use the IMAQ ColorThreshold VI. You must specify threshold ranges for each of the color planes—either Red, Green, and Blue or Hue, Saturation, and Luminance. The binary image resulting from a color threshold is an 8-bit binary image.