XScript Manual · Chapter 61

VISION — vision images and tools

VISION["name"] refers to one image object or one vision tool registered in the Vision Tool Editor.

  • Image object — the container that holds the picture taken by a camera. It saves and loads image files and converts pixel values to mm with the calibration result.
  • Vision tool — a tool that finds marks (Find · Match), measures dimensions (gauges) or reads codes (Barcode · DataMatrix · QRCode · Ocr) on its linked image.

Typically you grab one picture with CAMERA → run a vision tool → convert the result (pixels) to mm with the image object and use it to correct motor positions.

Access

Images and vision tools are created in the studio's Vision Tool Editor. Its left tree has four groups: CAMERA · IMAGE · VISION SYSTEM TOOL · VISION JOBFILE TOOL.

  • Image — set the IMAGE NAME, IMAGE DRIVER (XVisionCore or a project driver that provides image classes) and the class (XVisionCore provides mono XvImageGray8 · color XvImageColor24).
  • Vision tool — set the TOOL NAME, TOOL DRIVER, VISION TOOL TYPE and the linked image (the image the tool processes). Only the types the selected driver provides are listed (from Find · Match · Blob · LineGauge · CircleGauge · RectGauge · PointGauge · CrossGauge · DiamondGauge · Barcode · DataMatrix · QRCode · Ocr · Calibration · Algorithm · Roi · the Overlay types). XVisionCore provides only the Overlay types.
  • System tools and jobfile tools — vision system tools are kept once for the whole project, while vision jobfile tools are saved in the current JobFile folder. Tool names must be unique across both groups.

VISION["name"] looks the name up among the images first, then among the vision tools. Do not give an image and a tool the same name.

Basic example

This example finds a mark, moves the X axis by 5 mm, finds it again and computes the camera scale (mm/pixel).

xscript
double CALIB_MOVE_DIST = 5.0;   // distance to move the mark (mm)
 
FUNCTION MeasureScaleX()
{
   // Camera scale (mm/pixel) — find the mark, move the X axis by 5 mm and find it again
   double posX = MOTOR["M_X"].CommandPosition;
 
   if( CAMERA["AlignCam"].OneShot() == false )
   {
      return false;
   }
   VISION["FindCalibAlign"].MinScore = 0.5;
   if( VISION["FindCalibAlign"].FindPattern() == false )
   {
      return false;
   }
   double x1 = VISION["FindCalibAlign"].ResultX;   // pixel
   double y1 = VISION["FindCalibAlign"].ResultY;
 
   if( MOTOR["M_X"].MoveAbs(/*position*/posX + CALIB_MOVE_DIST, /*wait*/true) == false )
   {
      LogError("X move failed : dist={0}", CALIB_MOVE_DIST);
      return false;
   }
   Sleep(300);
 
   if( CAMERA["AlignCam"].OneShot() == false )
   {
      return false;
   }
   if( VISION["FindCalibAlign"].FindPattern() == false )
   {
      return false;
   }
   double x2 = VISION["FindCalibAlign"].ResultX;
   double y2 = VISION["FindCalibAlign"].ResultY;
 
   double dist   = MATH.CalcDistance(x1, y1, x2, y2);   // distance moved (pixel)
   double scaleX = CALIB_MOVE_DIST / dist;              // mm / pixel
   Log("Camera Scale X = {0}", scaleX);
   return true;
}
 
FUNCTION OffsetToMm()
{
   // How far the found position is from the taught center, in mm — the image object converts
   double dx = VISION["FindCalibAlign"].ResultOffsetX;
   double dy = VISION["FindCalibAlign"].ResultOffsetY;
   VISION["AlignImage"].ConvertPixelToMm(/*ref*/dx, /*ref*/dy);
   Log("Offset = {0:F3}, {1:F3} mm", dx, dy);
   return true;
}

Common to every vision object (image · tool)

SignatureDescription
string NameVision object name
string DriverNameDriver name of the vision object
string ImageNameName of the linked image object (set · get)
bool VisibleWhether it is displayed (set · get)
bool SelectedWhether this object is selected on the display (set · get)
bool IsVisibleToolWhether it is a kind of object shown on screen
double ImageWidth · double ImageHeightWidth · height of the linked image
string BasePath · string FileNameBase path · file name of the vision tool information file (set · get)
string PREFIXLog prefix string (XvImage:name for an image object)
bool Contains(string keyword)Whether this object's name contains keyword
string ToString()Information string of the vision object

The functions below update object information; QMachineStudio uses them when it creates, saves and loads tools.

SignatureDescription
void SetBaseInfo(string name, string driverName, string sImageName)Sets the basic information of the vision object (name · driver name · linked image name)
void UpdateCommonToolInfo()Updates the names and display state of the linked objects
void SetToolType()Updates the type information of the vision object
void SetToolInfo()Pushes the information registered on the object to the linked display
void UpdateToolInfo()Copies the configured information into the object's internal information

Image objects

Members that exist only on VISION["image"] obtained with an image name.

SignatureDescription
bool EmptyWhether the image is empty (false once grabbed or loaded)
int Width · int HeightImage width · height
bool AvailableWhether the image object can be used
string CalibToolNameName of the linked calibration tool (set · get — changing it re-links to that tool)
void UpdateCalibToolLink()Updates the calibration tool link
double MmPerPixelX · double MmPerPixelYSize of one pixel in X · Y (mm/pixel, set · get)
double MmPerPixelAverage of the X · Y values
double ConvertPixelToMm(double pixel)pixel → mm (average scale)
double ConvertMmToPixel(double mm)mm → pixel (average scale)
double ConvertPixelToMmX(double pixel) · ConvertPixelToMmY(double pixel)pixel → mm with the X · Y scale
double ConvertMmToPixelX(double mm) · ConvertMmToPixelY(double mm)mm → pixel with the X · Y scale
void ConvertPixelToMm(ref double pixelX, ref double pixelY)Converts both values to mm with the X · Y scale, in place
void ConvertMmToPixel(ref double mmX, ref double mmY)Converts both values to pixels with the X · Y scale, in place
bool SaveImage(string fileName)Saves the current image to a file
bool LoadImage(string fileName)Reads an image file into this image
bool BackupToBuffer()Backs up the current image into the internal buffer
bool RestoreFromBuffer()Restores the image backed up in the buffer as the current image

Main members of the find tool (Find)

Each kind of vision tool has its own functions (e.g. FindPattern() on a find tool, FindBarcode() on a barcode tool). The find-tool members used in the example above are:

SignatureDescription
bool FindPattern()Finds the taught mark (model) in the linked image
double MinScoreMinimum score accepted as found (set · get)
double ResultX · double ResultYFound position (pixel)
double ResultScoreScore of the found result
double ResultOffsetX · double ResultOffsetYDistance of the found position from the taught center (pixel)

Tool-specific additions (4.0)

The functions below exist only on the named tools (MaxFindCount · GetResultCount · GetResultDecodeStr are common to the barcode tools, GetResultIsGS1 is Euresys only). Used on another tool they are a 'method not found' error at run time (not checked at build time).

ToolFunctions · propertiesDescription
Euresys barcode (DataMatrix)MaxFindCount (2 or more) · int GetResultCount() · string GetResultDecodeStr(int resultIndex = 0)With MaxFindCount set to 2 or more, up to that many DataMatrix codes are read from one image. Read the results by index
Euresys barcode (DataMatrix)bool GetResultIsGS1(int resultIndex = 0)Whether that result is a GS1 DataMatrix (always false for QR · 1D barcode results)
QVision gauges (Line · Point · Rect · Cross · Diamond · Circle)void ClearResult()Clears the measurement result. Called automatically when code changes the gauge position, size or angle
QVision cross gaugedouble ResultThicknessV · double ResultThicknessHMeasured width of the vertical bar (left↔right) / thickness of the horizontal bar (top↔bottom)
xscript
FUNCTION ReadAllCodes()
{
   VISION["DmReader"].MaxFindCount = 4;   // up to 4 codes in one image
   if( VISION["DmReader"].FindBarcode() == false )
   {
      return false;
   }
 
   int n = VISION["DmReader"].GetResultCount();
   int i = 0;
   for( i, 0, n - 1 )
   {
      string code = VISION["DmReader"].GetResultDecodeStr(i);
      bool gs1 = VISION["DmReader"].GetResultIsGS1(i);
      Log($"[{i}] {code} GS1={gs1}");
   }
   return true;
}

Note The class names of the vision overlay shapes are now XvOverlayLabel · XvOverlayLine · XvOverlayRect · XvOverlayCircle · XvOverlayCross. Older projects are converted automatically when opened.

Cautions

  • The scale comes from the calibration tool. When no calibration tool (CalibToolName) is linked to the image, MmPerPixelX · MmPerPixelY are 1 and the conversion functions return the value unchanged. Setting MmPerPixelX · MmPerPixelY without a link stores nothing.
  • The conversion functions are on the image object. To convert a tool result (pixels) to mm, get VISION["image"] with the tool's linked image name and call them there.
  • Use only the functions of the tool's kind. Calling a function the tool does not have passes the build and fails at run time.
  • Vision object names are not checked at build time. Make sure they match the names in the Vision Tool Editor.
Subscribe