The weightedMedianFilter () sometimes hangs with some particular input images.
The hang seems not depend on particular parameters (sigma, radius, weight type or mask image) as well as on joint image, but just on input image content.
It works fine with total majority of input images but fails with some particulars.
Attached is an example of 16-bit single-channel grayscale png image (uint16_t), which causes weightedMedianFilter() hangup . The image was converted to CV_32FC1 before passing to weightedMedianFilter().
If I change pixel values in this image in a some way (for example convert the image to 8 bit, make some brighness/contrast stretch, etc) then no hangups happened.

The outline of code example is below:
```.cpp
// C++ code example
Mat input_image = cv::imread("ss-2.003.png", IMREAD_UNCHANGED);
input_image.convertTo(input_image, CV_32F);
weightedMedianFilter(Mat::ones(input_image.size(), CV_8U),
input_image, output_image, raduis, sigma, WMF_OFF);
// No return: hang up inside of weightedMedianFilter()
```
Attached is complete code exampe to reproduce.
weightedMedianFilter-hangup.tar.gz
Just unpack, make and run:
$ ./weightedMedianFilter-hangup ss-2.003.png
Will see 100% cpu load with no return.
i could reproduce it, the problem is here:
https://github.com/opencv/opencv_contrib/blob/2aaa67a67abd6ca4437465a8c8467a22e1378732/modules/ximgproc/src/weighted_median_filter.cpp#L79-L100
with your example image, (r-l) reaches a final value of 0.000015 in iteration ~25 , and stops decreasing.
The example image I attached above is not unique problematic image, I can attach more examples if need.
@amyznikov Could you check this patch: #1613 ?
BTW, the most part of OpenCV algorithms expect 32F(64F) images within range 0..1.
So It is better to use something like this:
-input_image.convertTo(input_image, CV_32F);
+input_image.convertTo(input_image, CV_32F, 1.0f/65535.0f); // CV_16U only
Hi @alalek , sorry for delay with responce,
I tested the patch #1613 on 200 potentially problematic images I have, everything works fine now, no hangups happened.
Thank you a lot!
i think, this can be closed, now.
(unfortunately, since we submit pr's to 3.4, it can no more be closed automatically)