Skip to content

drawContours() function fails to correctly draw child contours #26264

@sunkyoo

Description

@sunkyoo

System Information

  • OpenCV version: 4.10.0
  • Operating System: Windows 11
  • Language: C++ & Python

Detailed description

bug_on_draw_contours

Since OpenCV version 4.10.0, the drawContours() function fails to correctly draw child contours. It seems to work fine when the thickness value is set to -1 to fill the interior. However, when the thickness is set to a positive value, it only draws a single contour and does not draw the child contours.

The attached image shows the results of running the same source code after installing OpenCV versions 4.9.0 and 4.10.0 separately on Anaconda. While version 4.9.0 correctly draws all child contours, version 4.10.0 does not.

This issue is reproducible in the C++ version of OpenCV as well, and the same result occurs when testing with a manually built version of OpenCV 5.0.0-pre (Visual Studio 2022).

Steps to reproduce

contours

import sys
import random
import numpy as np
import cv2

src = cv2.imread("contours.png", cv2.IMREAD_GRAYSCALE)

if src is None:
    print("Image load failed!")
    sys.exit()

h, w = src.shape[:2]

def generate_dst(w, h, contours, hier):
    dst = np.zeros((h, w, 3), np.uint8)

    idx = 0
    while idx >= 0:
        c = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        cv2.drawContours(dst, contours, idx, c, 2, hierarchy=hier)
        idx = hier[0, idx, 0]

    return dst

contours1, hier1 = cv2.findContours(src, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
contours2, hier2 = cv2.findContours(src, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

dst1 = generate_dst(w, h, contours1, hier1)
dst2 = generate_dst(w, h, contours2, hier2)

cv2.imshow("src", src)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.waitKey()
cv2.destroyAllWindows()

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions