Using openCV in a task type¶
openCV can be used in a task type if Core.Python.opencv
is installed.
For example Example.CV.Gauss
uses the openCV function cv2.GaussianBlur
:
import omni
import xfworkerutil
import cv2
worker = xfworkerutil.XFWorkerJob()
inputs = worker.job['inputs']
sigma = inputs['gauss_sigma']
support = inputs['gauss_support']
if sigma is None:
# cv2.GaussianBlur automatically selects sigma from kernel size if sigma is zero.
sigma = 0
kernel_size = (support * 2 + 1, support * 2 + 1)
# Download input files (if not local)
image = worker.get_omni_image(inputs['image'])
# Use openCV for image processing
if image.dimensions == 2 or image.dimensions == 3 and image.axes == 'CXY':
blurred_pixels = cv2.GaussianBlur(image.pixels, kernel_size, sigma)
else:
worker.error(f"Image of {image.dimensions} dimensions not supported.")
# Save the segment image
blurred_omni_path = worker.create_related_path(image.metadata_path, '_blurred.omni')
blurred_raw_path = worker.create_related_path(image.metadata_path, '_blurred.raw')
blurred_image = omni.Image.from_numpy_array(blurred_pixels, blurred_omni_path, {
'pixels_filename': blurred_raw_path.name,
'dimensions': image.dimensions,
'size': image.size,
'pixel_size_mm': image.pixel_size_mm,
'pixel_type': image.pixel_type,
'axes': image.axes,
})
if image.pixel_min_value is not None:
blurred_image.metadata['pixel_min_value'] = image.pixel_min_value
if image.pixel_max_value is not None:
blurred_image.metadata['pixel_max_value'] = image.pixel_max_value
blurred_image.pixels_path = blurred_raw_path
blurred_image.save_all()
# Upload output files
image_output = worker.upload_omni_output(blurred_image)
# Save outputs
outputs = {
"image": image_output,
}
worker.finish(outputs)