opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

SegFault in connectedComponents with connectivity = 4

Open raphaelreme opened this issue 3 years ago • 0 comments

Hello !

I was experiencing with cv2.connectedComponents in python and with the connectivity argument (Wanted to use 4 instead of 8), and I faced a SegFault error when using this function without giving the output labels array as parameter (and letting opencv allocating it for me)

I'm not sure whether it is a bug of opencv or opencv python. So I just opened the issue here.

Steps to reproduce

Example that yields a SegFault

import cv2
import numpy as np

mask = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 1]], dtype=np.uint8)

n, labels = cv2.connectedComponents(mask, connectivity=4)

# SegFault

A solution without SegFault and the good result

import cv2
import numpy as np

mask = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 1]], dtype=np.uint8)
labels = np.zeros(mask.shape, dtype=np.int32)

n, labels = cv2.connectedComponents(mask, labels, connectivity=4)

# No SegFault
# labels is
# 0 1 1 0
# 0 1 1 0
# 0 0 0 2
# 0 0 2 2

This behavior does not seem to happen with connectivity=8

I'm on Ubuntu 20.4 64bits (x86_64) Using Python 3.8.13 and OpenCv-python 4.5.5.64

raphaelreme avatar Aug 08 '22 09:08 raphaelreme