# Watershed returns an error

**URL:** <https://forum.opencv.org/t/watershed-returns-an-error/7181>\
**Category:** Python\
**Created:** [January 18, 2022, 1:34pm UTC](https://forum.opencv.org/t/watershed-returns-an-error/7181 "2022-01-18T13:34:13Z")\
**Posts on this page:** 4\
**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 18, 2022, 1:34pm UTC](https://forum.opencv.org/t/watershed-returns-an-error/7181/1 "2022-01-18T13:34:13Z")

</div>

Hey, everyone! I am trying to complete a watershed algorithm, but it returns a following error about cv.watershed function:

```auto
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\pythonProject\main.py", line 23, in <module>
    marks = cv.watershed(hsv_mask, marks)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\segmentation.cpp:161: error: (-215:Assertion failed) src.type() == CV_8UC3 && dst.type() == CV_32SC1 in function 'cv::watershed'

```

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)
val, 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)
sure_bg = sure_bg.astype(np.uint8)
ret, marks = cv.connectedComponents(sure_bg)
marks += 1
marks[unknown == 255] = 0
hsv_mask = np.asarray(hsv_mask)
marks = cv.watershed(hsv_mask, marks)

```

I wonder if anyone helps…

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 18, 2022, 1:40pm UTC](https://forum.opencv.org/t/watershed-returns-an-error/7181/2 "2022-01-18T13:40:19Z")

</div>

> [@mike](#):
>
> `(-215:Assertion failed) src.type() == CV_8UC3 && dst.type() == CV_32SC1`

check the types. what are the types?

---

<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 19, 2022, 9:55am UTC](https://forum.opencv.org/t/watershed-returns-an-error/7181/3 "2022-01-19T09:55:19Z")

</div>

both are numpy arrays

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 19, 2022, 9:59am UTC](https://forum.opencv.org/t/watershed-returns-an-error/7181/4 "2022-01-19T09:59:49Z")

</div>

of course, but what’s the `dtype` of each? and what’s their `.shape`?

I’m betting `hsv_mask` isn’t 3-channel, but that assertion says it needs to be.

please read the assertion that failed. it conveys meaning. it’s not just a string of expletives from an angry compiler.
