Scorpion Python Examples Documentation Release XI
Total Page:16
File Type:pdf, Size:1020Kb
SPE - Scorpion Python Examples Documentation Release XI Tordivel AS May 24, 2020 Contents 1 Python Primer 3 1.1 Variables and namespaces........................................3 1.2 A Script - An ordered collection of statements.............................6 1.3 Python and Scorpion...........................................8 2 Examples 01-10 9 2.1 Example 01: Calculate Area.......................................9 2.2 Example 02: Calculate mean value...................................9 2.3 Example 03: Manipulate the results from two LineFinders.......................9 2.4 Example 04: Dynamic Threshold.................................... 10 2.5 Example 05: Auto Exposure....................................... 10 2.6 Example 06: DrawLine......................................... 11 2.7 Example 07: Overlays.......................................... 11 2.8 Example 08-A: Python Methods..................................... 12 2.9 Example 08-B: Python Objects..................................... 12 2.10 Example 09: Scorpion Timing...................................... 13 2.11 Example 10: Imaging Averaging..................................... 14 3 Examples 11-20 15 3.1 Example 11: Resample Image...................................... 15 3.2 Example 12: Constant Image Contrast.................................. 16 3.3 Example 13-A: Serial Communication using PythonWin........................ 16 3.4 Example 13-B: Serial Communication in Scorpion........................... 17 3.5 Example 14: Python Results....................................... 17 3.6 Example 15: Making a result string................................... 18 3.7 Example 16: Automation by tool scripting............................... 18 3.8 Example 17: Image Manipulation with Python............................. 19 3.9 Example 18: Calculate Median Angle.................................. 20 3.10 Example 19: Iterating objects located by a blob............................. 21 3.11 Example 20: Resampling using non-linear calibration......................... 22 4 Examples 21-30 23 4.1 Example 21: Custom Scorpion Python extension............................ 23 4.2 Example 22: Accessing Scorpion Image Pixels............................. 24 4.3 Example 23: Implementing a tcp/ip Socket Server........................... 25 4.4 Example 24: Setting ExternalReference from calculated four points.................. 27 4.5 Example 25: Rotating a reference around in a circle.......................... 28 i 4.6 Example 26: Grabbing an image from an MOXA Video IP Server................... 29 4.7 Example 27: Toolbox Switch...................................... 29 4.8 Example 28: ColorMatcher Iteration................................... 29 4.9 Example 29: Audio notification..................................... 30 4.10 Example 30: Resampling using non-linear calibration......................... 30 5 Examples 31-40 33 5.1 Example 31: Client to tcp Socket Server................................. 33 5.2 Example 32: Read / Write External Data from / to file......................... 33 5.3 Example 33: Changing a tool’s ROI using ExecuteCmd........................ 34 5.4 Example 34: Histogram Equalization.................................. 35 5.5 Example 35: Robust Adam 6060 scripts................................. 36 5.6 Example 36: Bubble Sorting....................................... 38 5.7 Example 37: Element Statistics..................................... 39 5.8 Example 38: Saving Scorpion 3D Image................................ 40 5.9 Example 39 - Disabling Zoom in Image Windows........................... 40 5.10 Example 40 - Filtering timeseries.................................... 41 6 Examples 41-50 43 6.1 Example 41: Scorpion Watchdog keep system running......................... 43 6.2 Example 42: Binary Search....................................... 45 6.3 Example 43: Creating an ordered pointcloud.............................. 46 6.4 Example 44: UDP Socket Communication............................... 46 6.5 Example 45: Creating an empty pointcloud............................... 47 7 Using Arrlib from Python 49 7.1 Introduction............................................... 49 7.2 arrlibct versus pyArrlib.......................................... 50 7.3 How to get help on arrlibct........................................ 51 7.4 Data types in ArrLib and arrlibct..................................... 51 7.5 Fundamental types............................................ 52 7.6 Small vectors............................................... 52 7.7 Supported operations........................................... 53 7.8 Small matrices.............................................. 53 7.9 Supported operations........................................... 53 7.10 Poses................................................... 54 7.11 Supported operations........................................... 54 7.12 Ranges.................................................. 54 7.13 Parametric lines............................................. 55 7.14 Supported operations........................................... 55 7.15 Circles and spheres............................................ 55 7.16 Color pixels............................................... 55 7.17 Important note on garbage collection.................................. 56 7.18 Geometric recipes............................................ 56 7.19 Planar geometry............................................. 56 7.20 Working with homogeneous lines and points in the plane........................ 59 7.21 Fitting points to a straight line...................................... 60 7.22 Transformation of normal vectors and homogeneous lines........................ 61 7.23 3D space geometry............................................ 61 7.24 3D point transformations......................................... 61 7.25 Merging two point clouds in Scorpion.................................. 62 7.26 Intersecting lines in 3D.......................................... 62 7.27 Python Script Samples.......................................... 63 8 Using ScorpionOpenCV 67 ii 8.1 Introduction............................................... 67 8.2 Image filters............................................... 68 8.3 ImageFilter examples.......................................... 69 8.4 Code examples.............................................. 69 9 Release notes 71 10 Indices and tables 73 iii iv SPE - Scorpion Python Examples Documentation, Release XI The Scorpion Python Examples provides valuable insight in Scorpion Vision Python Scripting. Python is what has made Scorpion a powerful machine vision framework. Scorpion is extended by the important open-source libraries Numpy, Scipy and OpenCV. Most of these powerful API are exposed by a smooth python interface. Fig. 1: Tool script automate candles processing The Scorpion Python Modules provides the source code and documentation for all python modules with Scorpion Vision Software. Contents: Contents 1 SPE - Scorpion Python Examples Documentation, Release XI 2 Contents CHAPTER 1 Python Primer This section will provide a short introduction to namespaces in Scorpion and Python. These concepts are important for everybody who wants to exploit the power of Python in Scorpion. 1.1 Variables and namespaces All variables in Python, and every other programming language, are created and exist in a namespace. What is a namespace? Using PythonWin, an integral part for Python Extension for Windows, or Idle - yet another Python environment, one can play with namespaces. An assignment is a statement of type a=b where a is a variable and b another variable, constant, constructor or function. a=100 b=1.03 c=’spam’ e=[] f={} g=eggs() are assignments where: a is assigned an integer value, b a floating point value, c a string e an empty list f an empty dictionary g the value of function or a reference to an instance of a class When Python is initialized, a namespace is created. It is available for declaration of variables, functions and objects. Declarations: 3 SPE - Scorpion Python Examples Documentation, Release XI a=100 def foo(b): print ‘a=’,a,’b=’,b The statement: foo(200) Will yield the output: a=100b=200 Python searches for a local definition of a in foo(). If not found Python search in the global namespace. If a local variable is defined in foo() the result will be as follows: Declarations: a=100 def foo(b): a=300 print ‘a=’,a,’b=’,b The statement: foo(200) Will yield the output: a=300b=200 The statement: print ‘a=’,a Will yield the output: a=100 Functions and name spaces can be nested: a=100 def foo(b): a=300 def spam(b): a=400 print ‘a=’,a,’b=’,b spam(500) print ‘a=’,a,’b=’,b The statement: foo(200) Will yield the output 4 Chapter 1. Python Primer SPE - Scorpion Python Examples Documentation, Release XI a=400b=500 a=300 200 The statement: print ‘a=’,a Will yield the output: a=100 Lets modify this slightly: a=100 def foo(b): def spam(b): print ‘a=’,a,’b=’,b spam(500) print ‘a=’,a,’b=’,b The statement: foo(200) Will yield the output: a=100b=500 a=100b=200 The statement: print ‘a=’,a Will yield the output: a=100 Python finds the value of a by searching in the nested namespaces. To change a global variable we can instruct Python to do so. a=100 def foo(b): def spam(b): global a a=b print ‘a=’,a,’b=’,b spam(500) print ‘a=’,a,’b=’,b The statement: foo(200) Will yield the output: 1.1. Variables and namespaces 5 SPE - Scorpion Python Examples Documentation, Release XI a=500b=500 a=500b=200 The statement: print ‘a=’,a Will yield the output: a=500 1.2 A Script - An ordered collection