# Connected Components returns an error

**URL:** <https://forum.opencv.org/t/connected-components-returns-an-error/7150>\
**Category:** Python\
**Created:** [January 16, 2022, 11:42am UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150 "2022-01-16T11:42:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mike/32/4250_2.png) [@mike](https://forum.opencv.org/u/mike)\
**Post date:** [January 16, 2022, 11:42am UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/1 "2022-01-16T11:42:45Z")

</div>

Hello, everyone!  
I am trying 2 create a watershed algorithm and encountered a following error:

```auto
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\pythonProject\main.py", line 15, in <module>
    ret, marks = cv.connectedComponents(sure_bg)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\connectedcomponents.cpp:5632: error: (-215:Assertion failed) iDepth == CV_8U || iDepth == CV_8S in function 'cv::connectedComponents_sub1'

```

Here is the code:

```auto
import cv2 as cv
import numpy as np
image = cv.imread(r'C:\\Users\User\Desktop\im.png')
low_cube = (120,50,50)
high_cube = (155,255,255)
hsv_im = cv.cvtColor(image, cv.COLOR_BGR2HSV)
hsv_mask = cv.inRange(hsv_im, low_cube, high_cube)
cnt, _ = cv.findContours(hsv_mask, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE)
c = max(cnt, key=cv.contourArea)
#cv.drawContours(image, c, -1, (0,0,255), thickness=3)
dist = cv.distanceTransform(hsv_mask, cv.DIST_L2, 5)
_, res = cv.threshold(dist, 0.4*dist.max(), 255, cv.THRESH_BINARY)
sure_bg = cv.dilate(res, np.ones((3,3)), iterations=3)
unknown = cv.subtract(sure_bg, res)
ret, marks = cv.connectedComponents(sure_bg)

```

I wonder if anyone helps…

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 16, 2022, 11:58am UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/2 "2022-01-16T11:58:01Z")

</div>

please check `sure_bg.dtype` (must be 0 or 1, but obviously isnt)

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mike/32/4250_2.png) [@mike](https://forum.opencv.org/u/mike)\
**Post date:** [January 16, 2022, 12:29pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/3 "2022-01-16T12:29:56Z")

</div>

what do you mean(0 or 1)? It’s float32

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 16, 2022, 1:06pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/4 "2022-01-16T13:06:46Z")

</div>

> [@mike](#):
>
> what do you mean(0 or 1)?

ah, right, the underlying c++ api uses integer types like that…  
what you need is `np.uint8` or `np.int8`

> [@mike](#):
>
> It’s float32

yea, probably from the distanceTransform already

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mike/32/4250_2.png) [@mike](https://forum.opencv.org/u/mike)\
**Post date:** [January 16, 2022, 1:35pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/5 "2022-01-16T13:35:15Z")

</div>

You mean, I need to change the dtype of the hsv\_msk variable?  
If yes, how to do this?..(

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 16, 2022, 1:39pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/6 "2022-01-16T13:39:29Z")

</div>

> [@mike](#):
>
> You mean, I need to change the dtype of the hsv\_msk variable?

no, not that one … 😉

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mike/32/4250_2.png) [@mike](https://forum.opencv.org/u/mike)\
**Post date:** [January 16, 2022, 1:56pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/7 "2022-01-16T13:56:26Z")

</div>

which exactly then? I didn’t got it

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 16, 2022, 1:59pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/8 "2022-01-16T13:59:17Z")

</div>

connectedComponents needs uint8 input

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mike/32/4250_2.png) [@mike](https://forum.opencv.org/u/mike)\
**Post date:** [January 16, 2022, 2:38pm UTC](https://forum.opencv.org/t/connected-components-returns-an-error/7150/9 "2022-01-16T14:38:59Z")

</div>

oh, yea, that works! Thanks a lot!
