imgproc: fix segfault in connectedComponents on zero-size input - #30225
`cv::connectedComponents` and `cv::connectedComponentsWithStats` crash with SIGSEGV when passed an image with a zero-length dimension (e.g., shape `(0, 400)`), instead of raising a proper error. All other OpenCV functions handle empty input gracefully.
The root cause is that the labeling algorithms dereference pointers based on image dimensions without first checking for empty input, causing a null dereference when `rows == 0` or `cols == 0`.
The fix adds `CV_Assert(!img.empty())` at the entry point of both public API functions, so callers receive a `cv::Exception` / `cv2.error` consistently with the rest of the OpenCV API (`findContours`, `cvtColor`, `inRange`, etc.).
A regression test `Imgproc_ConnectedComponents.empty_input_29854` is included, covering all three zero-dimension shapes (`{0,400}`, `{400,0}`, `{0,0}`) for both `connectedComponents` and `connectedComponentsWithStats`.
Reproducer:
```python
import numpy as np, cv2
mask = np.zeros((0, 400), dtype=np.uint8)
cv2.connectedComponentsWithStats(mask) # was: SIGSEGV; now: cv2.error
```
Fixes#29854
Take into account overflow for connected components #27582
### Pull Request Readiness Checklist
Fix#27568
The problem was caused by a label type overflow (`debug_example.npy` contains `92103` labels, that doesn't fit in the `CV_16U` (`unsigned short`) type). If pass `CV_32S` instead of `CV_16U` as `ltype` - everything will be calculated successfully
Added overflow detection to throw exception with a clear error message instead of strange segfault/assertion error
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
Usage of imread(): magic number 0, unchecked result
* docs: rewrite 0/1 to IMREAD_GRAYSCALE/IMREAD_COLOR in imread()
* samples, apps: rewrite 0/1 to IMREAD_GRAYSCALE/IMREAD_COLOR in imread()
* tests: rewrite 0/1 to IMREAD_GRAYSCALE/IMREAD_COLOR in imread()
* doc/py_tutorials: check imread() result
- removed tr1 usage (dropped in C++17)
- moved includes of vector/map/iostream/limits into ts.hpp
- require opencv_test + anonymous namespace (added compile check)
- fixed norm() usage (must be from cvtest::norm for checks) and other conflict functions
- added missing license headers
* Add Grana's connected components algorithm for 8-way connectivity. That algorithm is faster than Wu's one (currently implemented in opencv). For more details see https://github.com/prittt/YACCLAB.
* New functions signature and distance transform compatibility
* Add tests to imgproc/test/test_connectedcomponents.cpp
* Change of test_connectedcomponents.cpp for c++98 support