Image filters¶
When reading images from the image source using the tracetronic: Multimedia tool, you can optimize these images by using image filters. For example, you can create a grayscale version for comparison.
The following filters are already shipped with ecu.test:
Blur - This filter can be used to blur the image. This may improve the detection of objects in some cases.
Black / White Filter - You can use this filter to find elements that are brighter than a given threshold value. This filter will output a black/white version of the image.
Color Filter - This filter will highlight areas with a specific color. For example, you could improve text recognition results by adding one or more colors of your font. This filter will output a black/white version of the image.
Contrast & Brightness - With this filter you can manipulate contrast and brightness of an image.
HSV - Using this filter you can adjust hue, saturation and lightness value of your image. For example, a grayscale version of the image will be generated if the saturation is set to 0.
Invert - Inverts the image colors.
Scale - Resizes the image.
Rotate - Rotates the image.
User defined image filters¶
These filters can be selected in the Read image dialog, or you can access them using the Internal API.
The user defined image filters are python scripts that have to match certain criteria:
The script has to be located within the
UserPyModulesfolder of the workspaceOn the top level of the script, you must specify the variable MODULE_TYPE = ‘ImageFilter’
The file must have a class “Filter” that has to be derived from “ImageFilter”
The class must implement the methods GetDocumentation, ProcessImage and GetId.
There must not be two filters with the same ID
Created image filters are available after refreshing the user libraries. You can do this in ecu.test in the menu “Extras”.
Example filter¶
This is an example of an image filter that will blur the image.
import cv2
from tts.testModule.multimedia.dataElements.UserFilters import ImageFilter
MODULE_TYPE = 'ImageFilter'
class Filter(ImageFilter):
@classmethod
def GetDocumentation(cls):
doc = super().GetDocumentation()
doc.SetName('Blurring')
argument = doc.AppendArgument('Type')
argument.SetDescription('"AVERAGING", "GAUSS" oder "MEDIAN"')
argument.SetDefaultValue('"GAUSS"')
argument = doc.AppendArgument('Kernelsize')
argument.SetDescription('Size of the kernel matrix. Has to be an odd value.')
argument.SetDefaultValue('55')
return doc
def ProcessImage(self, imageData, parameters):
# b, g, r, a = cv2.split(imageData)
type = parameters['Type']
size = parameters['Kernelsize']
if type == "AVERAGING":
blur = cv2.blur(imageData, (size, size))
elif type == 'GAUSS':
blur = cv2.GaussianBlur(imageData, (size, size), 0)
elif type == 'MEDIAN':
blur = cv2.medianBlur(imageData, size)
else:
raise RuntimeError()
return blur
@classmethod
def GetId(cls):
# It is recommended to create a new UUID and paste it here.
# You can find several generators for UUIDs in the internet.
return "YOUR-GENERATED-UUID"
Explanation of the available methods¶
Class “Filter”
The user defined image filter has to define this class. It has to be derived from “ImageFilter”.
ProcessImage
This method has to be implemented. It receives two parameters:
An OpenCV numpy array (numpy.ndarray) that contains the read image with applied filters and mask (4 channels: blue, green, red, alpha)
A dictionary that provides access the parameters that have been specified in the FilterDocumentation.
This function must return an OpenCV numpy array (numpy.ndarray) containing 4 channels: blue, green, red, alpha
GetDocumentation
This method can be implemented if your filter uses parameters. Usage: Please refer to the example above. It has to return a FilterDocumentation object.
GetId
This method has to be overwritten to provide a unique ID. If you add the filter to a test case, this ID will be used for the reference.
By default, the ID of the filter is generated by its path within the UserPyModules directory. However, in case you move the file to another folder, existing tests will be broken. Therefore it is strongly recommended to provide a specific UUID here. You can use any UUID generator for this task.
Object “FilterDocumentation”
This object will be returned from the method “Filter.GetDocumentation” if you call super().GetDocumentation(). This object lets you define parameters for your filter.
SetName
Sets the display name of the filter.
AppendArgument (name)
Adds a parameter with name “name” that will be shown in ecu.test in the read image dialog. Returns the FilterArgument as an object.
Object “FilterArgument”
This object is returned when calling FilterDocumentation.AppendArgument(‘An argument’). It specifies a single parameter of a filter.
SetRequired (value)
Marks the argument as required. The parameter “value” can be True or False.
SetDescription (description)
Sets the description of the filter.
SetDefaultValue (defaultValue)
Sets the default value of the filter.