After reading some SO posts, I found a way to use OpenCVin Python3 with multiprocessing. I recommend doing this on Linux because according to this post, the spawned processes share memory with their parents until the content changes. Here is a minimal example:
import cv2
import multiprocessing as mp
import numpy as np
import psutil
img = cv2.imread('test.tiff', cv2.IMREAD_ANYDEPTH)
num_processes = 4
kernel_size = 11
tile_size = img.shape[0]/num_processes
output = mp.Queue()
def mp_filter(x, output):
print(psutil.virtual_memory())
output.put(x, cv2.GaussianBlur(img[img.shape[0]/num_processes*x:img.shape[0]/num_processes*(x+1), :],
(kernel_size, kernel_size), kernel_size/5))
if __name__ == 'main':
processes = [mp.Processes(target=mp_slice, args=(x, output)) for x in range(num_processes)]
for p in processes:
p.start()
result = []
for ii in range(num_processes):
result.append(output.get(True))
for p in processes:
p.join()
Queue, - multiprocessing . (ctypes ctypes)
result = mp.Array(ctypes.c_uint16, img.shape[0]*img.shape[1], lock = False)
, , . mp.Array . . , . NumPy:
result_np = np.frombuffer(result, dtypye=ctypes.c_uint16)