Files
opencv-MIRROR/modules/python/test/test_watershed.py
Varun Jaiswal 1bc76bc0fe Merge pull request #29740 from varun-jaiswal17:imgproc_cleanup
Imgproc test cleanup - #29740

co-authored by: @Prasadayus

  ### Re-enabled as-is (stale disable reasons)
  - `FillPolyFully.fillpoly_fully` (`test_drawing.cpp:1142`)
  - `Resize_Bitexact` (`test_resize_bitexact.cpp:188`, 4 instantiations): `INTER_NEAREST` and
  `INTER_NEAREST_EXACT` agree exactly at integer upscale factors; measured 0.0 diff on all 4.

### Add assertions

- **imgproc** — new `Imgproc_Watershed.regression`: `cv::watershed` had no working coverage at all.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-08-26 10:50:42 +03:00

46 lines
1.4 KiB
Python

#!/usr/bin/env python
'''
Watershed segmentation test
'''
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import cv2 as cv
from tests_common import NewOpenCVTests
class watershed_test(NewOpenCVTests):
def test_watershed(self):
img = self.get_sample('cv/inpaint/orig.png')
markers = self.get_sample('cv/watershed/wshed_exp.png', 0)
refSegments = self.get_sample('cv/watershed/wshed_segments.png')
if img is None or markers is None:
self.assertEqual(0, 1, 'Missing test data')
# cv.watershed() writes into markers in place, so the CV_32S array must be bound
# to a name: np.int32(markers) inline would hand over a temporary.
markers = np.int32(markers)
before = markers.copy()
colors = np.int32( list(np.ndindex(3, 3, 3)) ) * 122
cv.watershed(img, markers)
self.assertFalse(np.array_equal(before, markers),
'cv.watershed() did not modify the markers in place')
segments = colors[np.maximum(markers, 0)]
if refSegments is None:
refSegments = segments.copy()
cv.imwrite(self.extraTestDataPath + '/cv/watershed/wshed_segments.png', refSegments)
self.assertLess(cv.norm(segments - refSegments, cv.NORM_L1) / 255.0, 50)
if __name__ == '__main__':
NewOpenCVTests.bootstrap()