Search code examples
python-3.ximageopencvimage-processingnoise

OpenCV Image Denoising gives: Error: -215:Assertation failed


Trying to denoise a really simple image, using the code below. When printing out the array of data I get the following structure, which is expected as the image is greyscale:

[[ 62  62  63 ...  29  16   6]
 [ 75  90 103 ...  21  16  12]
 [ 77 100 118 ...  29  29  30]
 ...
 [ 84  68  56 ...  47  50  53]
 [101  94  89 ...  40  44  48]

Here is the code and the associated error, at this point I'm a little stuck. Any suggestions?

import cv2
from matplotlib import pyplot as plt

img = cv2.imread(path,0)
dst = cv2.fastNlMeansDenoising(img,None,10,10,7,21)

plt.subplot(211),plt.imshow(dst)
plt.subplot(212),plt.imshow(img)
plt.show()

____________________________________________________________________
runfile(___, wdir='G:/James Alexander/Python Programs')
Traceback (most recent call last):

  File "<ipython-input-127-ce832752c183>", line 1, in <module>
    runfile('G:/James Alexander/Python Programs/Noiseremoval.py', wdir=___)

  File "___", line 704, in runfile
    execfile(filename, namespace)

  File "___", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "___", line 13, in <module>
    dst = cv2.fastNlMeansDenoising(img,None,10,10,7,21)

error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\photo\src\denoising.cpp:120: error: (-215:Assertion failed) hn == 1 || hn == cn in function 'cv::fastNlMeansDenoising'

Solution

  • Read the documentation on the Denoising function that you're using. There are two ways to call the function and you seem to be doing a combination of the two.

    dst = cv.fastNlMeansDenoising(src[, dst[, h[, templateWindowSize[, searchWindowSize]]]])

    or

    dst = cv.fastNlMeansDenoising(src, h[, dst[, templateWindowSize[, searchWindowSize[, normType]]]])

    You are calling it with (src, dst, h, templateWindowSize, searchWindowSize, normType) which either has too many parameters or is in the wrong order, depending on which method you want to use.