question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MedianBlur randomly fails with OpenCV error

See original GitHub issue

This issue is a bit mysterious from my point of view, as it’s kind of complicated to reproduce it a single try.

In [1]: from albumentations import MedianBlur
   ...: import numpy as np
   ...:
   ...: success, fail = 0, 0
   ...: for _ in range(100):
   ...:     img = np.random.rand(100, 100)
   ...:     try:
   ...:         img = MedianBlur()(image=img).get('image')
   ...:         success += 1
   ...:     except Exception:
   ...:         fail += 1
   ...: print(success, fail)
   ...:
OpenCV Error: Assertion failed (src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4)) in medianBlur, file /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/smooth.cpp, line 3538
OpenCV Error: Unsupported format or combination of formats () in medianBlur, file /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/smooth.cpp, line 3529
OpenCV Error: Unsupported format or combination of formats () in medianBlur, file 

// some errors removed from the log

48 52

I’ve reproduced this stochastic issue on two different computers, both using opencv-python==3.3.0.10.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
creafzcommented, Jul 23, 2018

I think there are 2 reasons why those errors appear:

  1. np.random.rand generates ndarrays with the float64 data type. This data type is not supported by cv2.medianBlur which is used internally in MedianBlur.
  2. If the input type is not np.uint8, the only allowed ksize values for cv2.medianBlur are 3 and 5. Link to the OpenCV docs

This code that uses float32 inputs and ksize values 3 and 5 work without errors for me:

from albumentations import MedianBlur
import numpy as np
success, fail = 0, 0
for _ in range(100):
    img = np.random.rand(100, 100)
    img = img.astype('float32') # Cast a float64 image to float32
    try:
        # Use blur_limit 5 instead of default 7. So this augmentation will
        # apply `cv2.medianBlur` using ksize=3 or ksize=5.
        img = MedianBlur(blur_limit=5)(image=img).get('image')
        success += 1
    except Exception:
        fail += 1
print(success, fail)

I am afraid as long as we are using OpenCV functions there is nothing we can do to support float64 inputs and ksize values larger than 5.

Maybe we should check the data type of input images in all our augmentations and raise an exception if the input data type is not supported, so users won’t get those cryptic OpenCV errors? @albu @ternaus what do you think?

2reactions
arsenyinfocommented, Jul 24, 2018

@creafz may be, it can be replaced with cv2.filter2D with a manually created filter?

Read more comments on GitHub >

github_iconTop Results From Across the Web

medianBlur Assertion error (-215) - Python - OpenCV Forum
Hi, I am trying to run some code to perform a median blur. When I run: differenceImage = cv2.medianBlur(differenceImage,17) I get an error: ......
Read more >
OpenCV Error: Assertion failed (ksize.width ... - Stack Overflow
As I understood from the trace, you are only allowed to use new Size(x,y) where x and y are odd.
Read more >
Python | Image blurring using OpenCV - GeeksforGeeks
Median Blur : The Median Filter is a non-linear digital filtering technique, often used to remove noise from an image or signal.
Read more >
OpenCV Smoothing Image Filters - Medium
medianBlur ). Like the blur filter Median Filter takes the median value all the values in the kernel and applies to the center...
Read more >
error: (-215:assertion failed) !_src.empty() in function 'cvtcolor'
Opencv (in python) gives error - !_src.empty() in function 'cv2::cvtColor ' ... BTW: Sometimes cv2 has problem to find haarcascades file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found