Repository: opencv
Issue: watershed giving not stable results
Labels: category: imgproc

I keep getting not consistent results with watershed segmentation (providing the python test code below). I'm not sure if this is just weakness of the implemented algorithm or a bug. However when testing on the same data with scikit-image watershed I get correct results. My opencv version is 4.1.2

**dataset1** from opencv:
slice = [2,0,2,2,1,4,3,0,**0**]
```
image:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   4   0   0   0   0]
1 [  0   2   0   2   2   1   4   3   0   0   0]
2 [  0   2   0   2   2   1   4   3   0   0   0]
3 [  0   2   0   2   2   1   4   3   0   0   0]
4 [  0   0   0   0   0   0   4   0   0   0   0]

markers:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   0   0   0   0   0]
1 [  0   0   0   0   0   0   0   0   0   0   0]
2 [  0   0   2   0   0   0   0   0   0   4   0]
3 [  0   0   0   0   0   0   0   0   0   0   0]
4 [  0   0   0   0   0   0   0   0   0   0   0]

watershed:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]
1 [ -1   2   2   2   2   2  -1   4   4   4  -1]
2 [ -1   2   2   2   2   2  -1   4   4   4  -1]
3 [ -1   2   2   2   2   2  -1   4   4   4  -1]
4 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]

```
**dataset2** from opencv:
slice = [2,0,2,2,1,4,3,0,**2**]
```
image:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   4   0   0   0   0]
1 [  0   2   0   2   2   1   4   3   0   2   0]
2 [  0   2   0   2   2   1   4   3   0   2   0]
3 [  0   2   0   2   2   1   4   3   0   2   0]
4 [  0   0   0   0   0   0   4   0   0   0   0]

markers:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   0   0   0   0   0]
1 [  0   0   0   0   0   0   0   0   0   0   0]
2 [  0   0   2   0   0   0   0   0   0   4   0]
3 [  0   0   0   0   0   0   0   0   0   0   0]
4 [  0   0   0   0   0   0   0   0   0   0   0]

watershed:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]
1 [ -1   2   2   2   2   2   2  -1   4   4  -1]
2 [ -1   2   2   2   2   2   2  -1   4   4  -1]
3 [ -1   2   2   2   2   2   2  -1   4   4  -1]
4 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]

```


**dataset3** from opencv:
slice = [2,0,2,2,1,4,3,**2,1**]
```
image:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   4   0   0   0   0]
1 [  0   2   0   2   2   1   4   3   2   1   0]
2 [  0   2   0   2   2   1   4   3   2   1   0]
3 [  0   2   0   2   2   1   4   3   2   1   0]
4 [  0   0   0   0   0   0   4   0   0   0   0]

markers:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  0   0   0   0   0   0   0   0   0   0   0]
1 [  0   0   0   0   0   0   0   0   0   0   0]
2 [  0   0   2   0   0   0   0   0   0   4   0]
3 [  0   0   0   0   0   0   0   0   0   0   0]
4 [  0   0   0   0   0   0   0   0   0   0   0]

watershed:
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]
1 [ -1   2   2   2   2  -1   4   4   4   4  -1]
2 [ -1   2   2   2   2  -1   4   4   4   4  -1]
3 [ -1   2   2   2   2  -1   4   4   4   4  -1]
4 [ -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1]

```
On the other hand scikit-image watershed in all cases gives correct result:

watershed:
```
  [  c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 ]
0 [  2   2   2   2   2   2   2   4   4   4   4]
1 [  2   2   2   2   2   2   2   4   4   4   4]
2 [  2   2   2   2   2   2   2   4   4   4   4]
3 [  2   2   2   2   2   2   2   4   4   4   4]
4 [  2   2   2   2   2   2   2   4   4   4   4]
```


python code for testing:
```
import numpy as np
import cv2
# from skimage.morphology import watershed

print(cv2.__version__)

def printer(mat):
    row_labels = range(mat.shape[0])
    col_labels = range(mat.shape[1])
    print('  [ %s ]' % (' '.join(' c%s' % i for i in col_labels)))
    for (row_label, row) in zip(row_labels, mat):
        print('%s [%s]' % (row_label, ' '.join('%03s' % i for i in row)))

slice = [2,0,2,2,1,4,3,2,1]
gray = np.zeros((5,11), np.uint8)
gray[1,1:-1] = slice
gray[2,1:-1] = slice
gray[3,1:-1] = slice
gray[0,6] = 4
gray[-1,6] = 4
print('image:')
print(printer(gray))


# color = cv2.cvtColor(tab, cv2.COLOR_GRAY2BGR)
color = np.dstack((gray, gray, gray))

markers = np.zeros((5,11), np.int32)
markers[2,-2] = 4
markers[2, 2] = 2
print('markers:')
print(printer(markers))



# markers = watershed(gray, markers)
markers = cv2.watershed(color, markers)
print('watershed:')
print(printer(markers))
```