图像过滤器¶
使用 tracetronic: Multimedia(tracetronic: 多媒体) 工具从图像源读取图像时,可以使用图像过滤器对这些图像进行优化。例如,可以生成灰度版本用于比较。
ecu.test 已提供以下过滤器:
Blur - 此过滤器可用于模糊图像。在某些情况下,这有助于改进对象检测。
Black / White Filter - 可使用此过滤器查找亮度高于给定阈值的元素。该过滤器将输出图像的黑白版本。
Color Filter - 此过滤器会突出显示具有特定颜色的区域。例如,可以通过添加一种或多种字体颜色来改进文本识别结果。该过滤器将输出图像的黑白版本。
Contrast & Brightness - 使用此过滤器可以调整图像的对比度和亮度。
HSV - 使用此过滤器可以调整图像的色相、饱和度和明度值。例如,当饱和度设为 0 时,将生成图像的灰度版本。
Invert - 反转图像颜色。
Scale - 调整图像尺寸。
Rotate - 旋转图像。
用户自定义图像过滤器¶
这些过滤器可以在 Read image dialog(读取图像对话框) 中选择,也可以通过 Internal API 访问。
用户自定义图像过滤器是必须满足以下条件的 Python 脚本:
脚本必须位于 Workspace(工作区)的
UserPyModules文件夹中在脚本的顶层,必须指定变量 MODULE_TYPE = ‘ImageFilter’
文件中必须包含类 “Filter”,并且该类必须派生自 “ImageFilter”
该类必须实现 GetDocumentation、ProcessImage 和 GetId 方法。
不得存在两个具有相同 ID 的过滤器
创建的图像过滤器在刷新用户库后即可使用。可在 ecu.test 的 “Extras” 菜单中执行此操作。
示例过滤器¶
以下示例图像过滤器会对图像进行模糊处理。
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"
可用方法说明¶
类 “Filter”
用户自定义图像过滤器必须定义此类。该类必须派生自 “ImageFilter”。
ProcessImage
必须实现此方法。该方法接收两个参数:
一个 OpenCV numpy 数组(numpy.ndarray),其中包含已应用过滤器和掩码的读取图像(4 个通道:blue、green、red、alpha)
一个字典,用于访问在 FilterDocumentation 中指定的参数。
此函数必须返回一个包含 4 个通道(blue、green、red、alpha)的 OpenCV numpy 数组(numpy.ndarray)
GetDocumentation
如果过滤器使用参数,则可以实现此方法。用法请参见上方示例。该方法必须返回一个 FilterDocumentation 对象。
GetId
必须重写此方法以提供唯一 ID。如果将该过滤器添加到 test case(测试用例)中,将使用此 ID 进行引用。
默认情况下,筛选器的 ID 根据其在 UserPyModules 目录中的路径生成。 但是,如果把该文件移到其他文件夹,现有测试就会失效。因此 强烈建议在此处提供特定的 UUID。可以使用任意 UUID 生成器完成 此操作。
对象 “FilterDocumentation”
如果调用 super().GetDocumentation(),该对象将由方法 “Filter.GetDocumentation” 返回。此对象可用于为过滤器定义参数。
SetName
设置过滤器的显示名称。
AppendArgument (name)
添加一个名为 “name” 的参数,该参数将显示在 ecu.test 的 read image dialog(读取图像对话框) 中。返回一个 FilterArgument 对象。
对象 “FilterArgument”
调用 FilterDocumentation.AppendArgument(‘An argument’) 时会返回此对象。它定义过滤器的单个参数。
SetRequired (value)
将该参数标记为必填。参数 “value” 可以为 True 或 False。
SetDescription (description)
设置过滤器的说明。
SetDefaultValue (defaultValue)
设置过滤器的默认值。