Skip to content

SegFault in connectedComponents with connectivity = 4 #706

@raphaelreme

Description

@raphaelreme

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions