Examples 01-10

Example 01: Calculate Area

Calculate the area for a rim found be the ‘Rim’ tool. Set the result in the ‘Rimarea’ tool.

Observe that function and tag names are case-dependent.

radius = GetValue('Rim.Radius')
area = radius * radius * 3.14
SetValue('Rimarea.Value',area)

Example 02: Calculate mean value

Calculates the gap middle value from two LineEdgeFinder tools and sets the value in the Gap ExternalScalar tool.

gap1 = GetFloatValue('Gap 1.Width');
gap2 = GetFloatValue('Gap 5.Width');
SetFloatValue('Gap.Value',(gap2+gap1)/2);

Example 03: Manipulate the results from two LineFinders

'''
script from an application
establishing a set of results based on results from two lines in a LineFinder tool
'''

from math import sqrt,pow;

NumberLines = GetValue('Check top filter.Number lines');

x1 = GetValue('Check top filter.Line[1].p.x');
y1 = GetValue('Check top filter.Line[1].p.y');
x2 = GetValue('Check top filter.Line[2].p.x');
y2 = GetValue('Check top filter.Line[2].p.y');

angle = GetValue('Check top filter.Angle[2]');
Width= sqrt(pow((x1-x2),2)+pow((y1-y2),2))

if NumberLines == 2:

  SetValue('Topfilter Width.Value',Width)
  SetValue('Topfilter present.Value',1)
  SetValue('Topfilter angle.Value',angle)

else:

  SetValue('Topfilter Width.Value',-1)
  SetValue('Topfilter tilstede.Value',0)
  SetValue('Topfilter angle.Value',0)

Example 04: Dynamic Threshold

Dynamic Threshold setting for a blob-tool

#Find the ROI intensity
i=GetValue('Sidelight.Intensity')
print 'Intensity=', i

#set new max-Threshold
#new value must be converted to an integer string since
#this is the SPB format

if i>50:
  t=int(i)-20
  SetConfigValue('Sideprofile.Threshold.Max',t)
else:
  SetConfigValue('Sideprofile.Threshold.Max',30)

print 'Threshold =', GetValue('Sideprofile.Threshold.Max')

Example 05: Auto Exposure

Implement a simple auto exposure calculation

# retrieve intensity level
level = GetValue('Level.Value')
# read intensity level
light1 = GetValue('LightMeter.Intensity')

count = count +1

increaseExposure = ( count <20 ) and (light1 < level)
SetValue('CalculateExposure.Value',increaseExposure)

if (increaseExposure ) :
  print 'count -',count
  # set exposure on ieee-1394 camera
  ExecuteCmd('SetImageProp','imageno=1;Exposure='+str(count*25))  # camera support Exposure property
else:
  count = 0
  # reset exposure on ieee-1394 camera
  ExecuteCmd('SetImageProp','imageno=1;Exposure=0')

Example 06: DrawLine

# Constructs a line from origo to a point
# The line is drawn
# Uses ScorpionGeometry.py

from ScorpionGeometry import *

line = Lin(Vec(0,0),center)
ok = DrawLine('ArcialRef',line.p.x,line.p.y,line.v.x,line.v.y,'green',4,7)

# a marker is drawn at the end-point
ok = DrawMarker("ArcialRef",center.x,center.y,'blue',2,22)

Example 07: Overlays

#---------------------------------------------
# Add an overlay to the image Valve
# Uses Arr.pyd
#---------------------------------------------

def InitOverlays():
  #create a new overlay where we can visualize our stuff
  mgr=GetOverlayMgr('Valve')
  ovl=mgr.addOverlay('Custom')
  ovl.vertexStyle=0      #sets the default vertextyle to ovsNone, no visual marks
  ovl.edgeColor='Red'    #sets  the default color to red
  ovl.penStyle=0         #sets the default penstyle to psSolid

def DrawROI():
  import arr

  #get the image and find the size of the image
  img=GetImageMatr('Valve')
  rows,cols=img.dim()

  #define a ROI covering nearly all image
  offs=20
  roi=arr.xyfVec(4,'ROI')
  roi[0]=offs,offs
  roi[1]=offs,cols-offs
  roi[2]=rows-offs,cols-offs
  roi[3]=rows-offs,offs

  ovl=GetOverlay('Valve','Custom')
  ovl.clear()
  ovl.add(roi).closed=1

Example 08-A: Python Methods

#---------------------------------------------
# Traditional procedural approach
#---------------------------------------------
a=100
b=200
c=300

def spam(v):
  a=v

def eggs(v):
  b=v

def ham(v):
  c=v

spam(10)
eggs(20)
ham(30)

print a,b,c

# yields 10,20,30

Example 08-B: Python Objects

#---------------------------------------------
# object oriented approach
#---------------------------------------------
class abc:
  def __init__(self,a=100,b=200,c=300):
    self.a=a
    self.b=b
    self.c=c

  def spam(self,v):
    self.a=v

  def eggs(self,v):
    self.b=v

  def ham(self,v):
    self.c=v

myAbc=abc()

print myAbc.a, myAbc.b, myAbc.c

# yields 100,200,300

myAbc.spam(10)
myAbc.eggs(20)
myAbc.ham(30)

print myAbc.a, myAbc.b, myAbc.c

# yields 10,20,30

anotherAbc=abc(10,20,30)
anotherAbc.eggs(40)

print anotherAbc.a, anotherAbc.b, anotherAbc.c

# yields 10,40,30

Example 09: Scorpion Timing

This example is provided to show how to check the time consumption in Scorpion

# these values are defined in the Central Start script of Scorpion
global time,min,max,mean

# init all values if time is zero
if time == 0:
  # read a timer with milliseconds resolution
  t0 = GetValue('System.MSecSinceMidnight')
  time = t0
  min = 10000
  max = 0
  mean = 0
else:
  # read a timer with milliseconds resolution
  t0 = GetValue('System.MSecSinceMidnight')

  # calculates the times since this script was run in seconds
  cyclus = (t0-time)/1000.0

  # calculates min and max values
  if cyclus > max:
  max = cyclus
  if cyclus < min:
  min = cyclus
  mean = mean*0.9 + cyclus*0.1   # caculates a running mean value
# print cyclus and running mean to console windows
print ' cyclus - ',t0 - time, ' mean - ',mean
# remember counter
time = t0

Example 10: Imaging Averaging

This example is provided to show how to do image averaging using python and the arr module.

#------------------------------------------------------
# An example that is part of a PythonScript tool
#------------------------------------------------------
import arr
global image  # contains the averaged image data
global counter # counter is set to zero in the Central Start Script

# increment image counter
counter += 1

# read 'Intensity' image data
img = GetImageMatr('Intensity')

if counter == 1:
  image=img # initial value
else:
  # --------------------------------------------------
  # an exponential image average is calculated
  #
  #      image = image * 10/11 + img * 1/11
  #
  #  --------------------------------------------------
  image=arr.toUint8(arr.toInt(image)*(10,11)+arr.toInt(img)*(1,11)) # note special fraction multiplication syntax

if counter > 2:
  # the Mean Image is set
  SetImageMatr('Mean',image)
  # ------------------------------------------------------------------------------------------
  # image substraction
  #    convert images to float - the rangeToUnit8 function
  #    scales and handles image overflow to an 8 bit image with values from 0 to 255
  #--------------------------------------------------------------------------------------------
  image1 = arr.toFloat(image)-arr.toFloat(img)
  img = image1.rangeToUint8(-128,128)
  # set Subtract image
  SetImageMatr('Subtract',img)