OpenCV background subtraction via Python script

Hi, i am currently trying to use the background subtractor MOG from OpenCV in my KNIME Workflow.
I have two images which i am subtracting. The code is working but now i am stuck on the implementation into KNIME. I tried using the PythonSourceNode but i don’t know how i can output the image for further use.
Here’s my code:

import cv2

backgroundSubtractor = cv2.bgsegm.createBackgroundSubtractorMOG()

# apply the algorithm for background images using learning rate > 0
for i in range(1, 16):
    bgImageFile = f"/data/bg{i}.jpg"
    bg = cv2.imread(bgImageFile)
    backgroundSubtractor.apply(bg, learningRate=0.5)

# apply the algorithm for detection image using learning rate 0
stillFrame = cv2.imread("/data/Test.jpg")
fgmask = backgroundSubtractor.apply(stillFrame, learningRate=0)

#output mask???


Hi @AlexBtt,

There’s an extension called KNIME Image Processing - Python Extensions that changes the way how images are handled in a Python Script (1⇒1) node. Once you have this extension installed, you’d use KNIME Image Processing to load the two images and pass them to a Python Script node via the input table. The images are subsequently available as ndarrays in Python for further processing (see for an example). You can then do your processing with OpenCV. But you have to make sure to wrap your output ndarray back into a KNIPImage and append it as a column:

# Convert output to KNIPImage
outputKnip = KNIPImage(output)
# Add to columns



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.