IMAQ Vision and Machine Vision Basics for LabWindows/CVI
Overview
The following sections include tips and tricks for working with IMAQ Vision functions in LabWindows/CVI.
Table of Contents
Viewing IMAQ Vision Structures
Many of the IMAQ Vision for LabWindows/CVI functions use structures. To view an IMAQ Vision structure, open a LabWindows/CVI project that contains IMAQ Vision functions, compile it, and then right-click on any IMAQ Vision structure. LabWindows/CVI opens the header file in which the structure is defined and places the cursor on the structure. You also can use function panels to find information about IMAQ Vision structures. Complete the following steps to use function panels to find information about structures in the imaqOverlayText function.
1. Open a .c file for a project that uses the imaqOverlayText function.
2. Highlight imaqOverlayText and press <Ctrl-Shift-P> to open the Overlay Text function panel.
3. Click the Options parameter.
4. Press <Ctrl-D> to open a Declare Variable dialog box.
5. Type a name in the Variable Name field.
6. Select Add declaration to current block in target file and click OK to declare the OverlayTextOptions variable from the function panel.
7. In the .c file, right-click OverlayTextOptions and click Go to Definition. The definition is similar to the following code snippet.
typedef struct OverlayTextOptions_struct {
const char* fontName; // The name of the font to use.
int fontSize; // The size of the font.
int bold; // Set this element to TRUE to bold the text.
int italic; // Set this element to TRUE to italicize the text.
int underline; // Set this element to TRUE to underline the text.
int strikeout; // Set this element to TRUE to strikeout the text.
TextAlignment horizontalTextAlignment; // Sets the alignment of the text.
VerticalTextAlignment verticalTextAlignment; // Sets the vertical alignment for the text.
RGBValue backgroundColor; // Sets the color for the text's background pixels.
double angle; // The counterclockwise angle (degrees) of the text relative to the x-axis.
} OverlayTextOptions;
Notice this structure has other structures inside it, such as TextAlignment, VerticalTextAlignment, and RGBValue. These are also defined in nivision.h. You can use the binoculars at the top of the LabWindows/CVI development environment to find the definitions for these structures.
Declaring IMAQ Vision Structure Variables
The following code snippet shows how to declare an OverlayTextOptions variable:
// 1 = TRUE, 0 = FALSE
OverlayTextOptions TextOptions={"Arial", 30, 1, 0, 1, 0, IMAQ_LEFT, IMAQ_TOP, {0, 255, 0, 0}, -45.0};
//R, G, B, resvd
This variable uses 30 pt Arial font that is bold, not italic, underlined, not strikethrough, aligned on the left, and top, with a green color for the background, and is positioned at a 45-degree angle.
Setting up a Text Overlay
The following code snippet shows how to add an overlay using the OverlayTextOptions variable you previously declared:
imaqOverlayText (MyImage, MakePoint(100,100), "Hey There", &(IMAQ_RGB_RED),
&TextOptions, NULL);
MakePoint creates a Point data type for the location of the font, and IMAQ_RGB_RED is the color for the font. IMAQ_RGB_RED is defined in NiVision.h in the RGBValues section. The imaqOverlayText function requires the color constant and the TextOptions parameter to be passed in by reference, which is denoted by the ampersand character (&). This code snippet produces a result similar to the following image.

Figure 1.
Note:
All IMAQ Vision functions that use special structures include an asterisk (*) in the function panel declaration. If you use the function panel to declare variables, the variables are declared as pointers and you must allocate space for the pointers. Also, in this case you cannot set the parameters as shown above. To avoid this situation, remove the asterisk (*) from the definition, and use an ampersand (&) to pass the address to the variable in the code.
The following code snippet combines the above lines of code into a program:
Main()
{
OverlayTextOptions TextOptions={"Arial", 30,1,0,1,0,IMAQ_LEFT, IMAQ_TOP,{0, 255, 0, 0},-45.0};
Image* MyImage;
int i;
MyImage = imaqCreateImage (IMAQ_IMAGE_U8, 3);
imaqReadFile (MyImage, "c:\overlay.bmp", NULL, NULL);
imaqOverlayText (MyImage, MakePoint(100,100), "Hey There", &(IMAQ_RGB_RED),
&TextOptions, NULL);
imaqDisplayImage (MyImage, 0, TRUE);
}
Where to find examples:
For LabWindows/CVI
C:\Program Files\National Instruments\MeasurementStudio\CVI\samples\Vision
For Measurement Studio for Visual C++
C:\Program Files\National Instruments\Vision\Examples\MSVC
Using Region of Interest (ROI) Contours
A single region of interest (ROI) can contain multiple regions, or contours as they are termed in C. Each contour of an ROI has a contour ID that you can access and modify individually. You can add differently shaped contours to an ROI.
The following code snippets create two contours in one ROI. The first code snippet shows how to create contours with a hard-coded constant that might come from IMAQ Vision Builder. The second code snippet shows how to design the program to allow end users to create contours on the ROI.
MyROI = imaqCreateROI (); //top, left, height, width
Contour = imaqAddOvalContour (MyROI, MakeRect(100, 100, 50, 50));
//This is used to display the ROI
imaqOverlayROI (MyImage, MyROI, IMAQ_POINT_AS_PIXEL, NULL, NULL);
//This is how you would have the user specify contour(s) in an ROI
imaqConstructROI (MyImage, MyROI, IMAQ_LINE_TOOL, NULL, NULL, NULL);
Where to find examples:
For LabWindows/CVI
C:\Program Files\National Instruments\MeasurementStudio\CVI\samples\Vision
For Measurement Studio for Visual C++
C:\Program Files\National Instruments\Vision\Examples\MSVC
Using Machine Vision Functions
Machine vision functions are included in a separate library. To include machine vision functions in a project, you must add the nimachinevision.c file to the project. To add this file, open nimachinevision.c in a new project and click Options»Create Object File. Select Generate object for current compatibility mode to create a nimachinevision.obj file. After you create the .obj file, you must set up the nimachinevision.fp file to use the .obj file instead of the .c file. If a project uses the .c file, which is the default, the .c file must be in the project. Otherwise, the linker cannot find the .c file. If the .fp file uses the .obj file, it can link correctly without the file being in the project.
Complete the following steps to determine which file the .fp file uses for linking:
1. In the LabWindows/CVI development environment, open nimachinevision.c in a new project and click Options»Create Object File.
2. Select Generate object for current compatibility mode to create an nimachinevision.obj file and click OK.
3. Click OK.
4. Click Options»Library Options and remove the nimachinevision.fp if it is listed. Restart LabWindows/CVI after making this change. If the .fp file is not listed, proceed to the next step without restarting the computer.
5.Click Instrument»Load and select nimachinevision.fp. This file is in the CVI\Toolslib\Vision directory where you installed LabWindows/CVI.
6. Click Load.
6. Click Instrument»Edit and click Show Info to determine if the nimachinevision.fp file is linked to the .c file.
7. Click Done to exit the Instrument Driver Information dialog box.
8. Click Detach Program to remove the .c file as the default, and then click OK.
9. Use Windows Explorer to change the name of the nimachinevision.c file and make sure the nimachinevision.obj file is in the same location as the .c and .fp files.
10. Click Instrument»Edit and click Reattach Program to associate the .fp with the .obj file.
11. Click Done.
11. Click Options»Library Options and select nimachinevision.fp to add the file back to the default libraries.
12. Restart LabWindows/CVI.
When you complete these steps, you no longer have to include the .c file every time you want to use machine vision functions.
Note:
The source code for the machine vision functions are included in the nimachinevision.c file. You can modify the functions, but to ensure that your modifications are not overwritten by future upgrades, you must move the modified file to another location or rename it.
Where to find examples:
For LabWindows/CVI
C:\Program Files\National Instruments\MeasurementStudio\CVI\samples\Vision
For Measurement Studio for Visual C++
C:\Program Files\National Instruments\Vision\Examples\MSVC
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
