Up | Example 01 - Calculate Area | Example 02 - Calculate mean value | Example 03 - Manipulate the results from two LineFinders | Example 04 - Dynamic threshold | Example 05 - Auto Exposure | Example 06 - DrawLine | Example 07 - Overlays | Example 08 - Python Objects | Example 09 - Timing | Example 10 - Image Averaging | Example 11 - Image Resampling | Example 12 - Constant Contrast | Example 13 - Serial Communication | Example 14 - Python results | Example 15 - Making a result string | Example 16 - Running tools from a script | Example 17 - Image Manipulation using Python Scripting | Example 18 - Calculating the median angle | Example 19 - Iterating objects located by a blob | Example 20 - Resampling using non-linear calibration | Example 21 - Custom Scorpion Python extension | Example 22 - Accessing Image Pixels | Example 23 - Implementing a tcp/ip Socket Server | Example 24 - Setting ExternalReference from calculated four points | Example 25 - Rotating a reference around in a circle | Example 26 - Grabbing an image from an MOXA Video IP Server | Example 27 - Toolbox Switch | Example 28 - Color Matcher Iteration | Example 29 - Audio Notification | Example 30 - Windows MessageBox | Example 31 -  Client to tcp Socket Server | Example 32 -  Read / Write External Data from / to file | Example 33 - Changing a tool's ROI | Example 34 - Histogram Equalization | Example 35 - Robust Adam 6060 scripts | Example 36 - Bubblesort | Example 37 - Element Statistics | Example 38 - Saving 3D Image | Example 39 - Disabling Zoom in Image Windows | Example 40 - Filtering timeseries | Example 41 - Scorpion Watchdog keep system running | Example 42 - Binary Search | Example 43 - Creating an ordered pointcloud | Example 44 - UDP Socket Communication

 

  

 
Example 21 - Custom Scorpion Python extension
As a programmer, you can extend the Python language using modules written in C, C++ and even the .NET language C#, with the right glue in place. Python scripts in Scorpion have access to all data being processed within Scorpion, and this example shows how to access and process the raw image data from Scorpion in a Python extension module. A grayscale image is represented by 8-bit pixels, stored rowwise. A colour picture would use four bytes per pixel, in the order <blue><green><red><pad>.

If you create a Python module called "myext" (source code below) that exports a Python function "average", you can do your own image processing as follows, in a Scorpion Python script tool:

import arr,myext

im = GetImageMatr('1')
r,c = im.dim()
format = im.elemtype()
p = arr.getArrDataPointer(im)
avg = myext.average(p,r,c,format)
# avg now contains your result...

For instructions on how to create a Python module, please consult the Python docs, or "Python Essential Reference" by David M. Beazley, where you will find detailed documentation on how to create a Python extension. Below you will find the source code for the "myext" module, written in C. The "image processing" is done in the routine average, which gets a pointer to the image data. The routine _average is the Python interface.

myext.h

/**************************************************************************
* myext.h

* Skeleton Python extension
**************************************************************************/

#include "Python.h"

#define byte unsigned char

myext.c

/**************************************************************************
* myext.c

* Skeleton Python extension
**************************************************************************/

#define DLL_PREFIX __declspec(dllexport)

#include "myext.h"

// c image processing routine...

static double average(byte *image,int rows,int cols)
{
  double sum = 0;
  int n = rows*cols;
  int i;
  for (i=0;i<n;i++) sum += *image++;
  return sum/n;
}

// python callable methods

static PyObject *_average(PyObject *self,PyObject *args)
{
  int rows;
  int cols;
  char *format;
  byte *image;
  if (!PyArg_ParseTuple(args,"iiis",&image,&rows,&cols,&format))
    return NULL;
  if (strcmp(format,"uint8")) {
    PyErr_SetString(PyExc_TypeError,"Non-supported image format. Must be 'uint8'");
    return NULL;
  }
  return Py_BuildValue("d",average(image,rows,cols));
}

// module methods table
static struct PyMethodDef myext_methods[] = {
  // DLL exports
  {"average",_average,METH_VARARGS},
  // sentinel
  {NULL, NULL}
};

DLL_PREFIX void initmyext()
{
  PyObject *m;
  m = Py_InitModule("myext", myext_methods);
}

 

More on extending Python with C - http://en.wikibooks.org/wiki/Python_Programming/Extending_with_C
 

Scorpion Vision Version XII : Build 646 - Date: 20170225
Scorpion Vision Software® is a registered trademark of Tordivel AS.
Copyright © 2000 - 2017 Tordivel AS.