2

I want to use OpenCV to perform a grayscale morphological dilation. This seems very easy but I did not manage to do it. Therefore, I am wondering if it is possible to do it with OpenCV?

To check the results I created a MWE comparing OpenCV and SciPy. Scipy seems to give the expected results while OpenCV do not. Unfortunately, from other constrains I have to use OpenCV and not Scipy and do a grayscale morphological dilation. From the MWE it is seems to be possible to do a binary morphological dilation.

MWE:

import cv2
import scipy
import scipy.ndimage
import numpy as np

print('start test')
image=np.array( [[0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0],
  [0, 0, 25, 0, 0],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0]] )
Kernel=np.array([[0, 1, 0],
       [1, 1, 1],
       [0, 1, 5]])
print('input')
print(image)

image=image.astype('uint8')
Kernel=Kernel.astype('uint8')
output=cv2.dilate(image, Kernel)
print('OpenCV')
print(output)

Output2=scipy.ndimage.grey_dilation(image, structure=Kernel)
print('Scipy')
print(Output2)

print('end test')

Results:

start test
input
[[ 0  0  0  0  0]
 [ 0  0  0  0  0]
 [ 0  0 25  0  0]
 [ 0  0  0  0  0]
 [ 0  0  0  0  0]]
OpenCV
[[ 0  0  0  0  0]
 [ 0 25 25  0  0]
 [ 0 25 25 25  0]
 [ 0  0 25  0  0]
 [ 0  0  0  0  0]]
Scipy
[[ 5  5  5  5  5]
 [ 5 25 26 25  5]
 [ 5 26 26 26  5]
 [ 5 25 26 30  5]
 [ 5  5  5  5  5]]
end test

So it there a simple way (or an option) to do a grayscale morphological dilation with OpenCV, and obtain the same result than SciPy ?

R. N
  • 707
  • 11
  • 31
  • OpenCV result seems correct to me. I don't get the Scipy result, since a max_filter shouldn't create new values (where are 5, 26, 30 in the original image?). Probably there is some interpolation going on. Also check the typo in your kernel (5 instead of 0/1). – Miki Apr 09 '20 at 14:15
  • @Miki Yes, my kernel is using a 5 to emphasize the differences. About Scipy and max_filter, I don't know what you are talking about, but according to the definition of morphological dilation given by https://en.wikipedia.org/wiki/Dilation_(morphology)#Grayscale_dilation the values of SciPy are corrects. For example, the value 30 is image[2,2]+Kernel[1,1]=25+5 for x=[3,3] and y=[2,2] (note that Kernel indices going from [-1,-1] to [1,1]). – R. N Apr 09 '20 at 15:12
  • @ThomasSablik Since OpenCV is also a C++ library, you can have the same question for a C++ code. – R. N Apr 09 '20 at 15:13
  • from scipy doc: _" For the simple case of a full and flat structuring element, it can be viewed as a maximum filter over a sliding window"_ which is what OpenCV does. – Miki Apr 09 '20 at 15:24
  • since you're using a non-flat structuring element, you're NOT doing the same operation as OpenCV. OpenCV dilation works for grayscale images but only with flat structuring element, i.e. OpenCV treats evert non zero value as 1 – Miki Apr 09 '20 at 15:29
  • @ThomasSablik I am not interested in all the programming language for image processing but in the specific OpenCV library which is in C++. But I understand your point. – R. N Apr 09 '20 at 15:29
  • @Miki Yes, I agree. This is why my question is how to do a grayscale morphological dilation (implicitly with a non flat structuring element). Maybe the answer is: this is not possible. – R. N Apr 09 '20 at 15:32
  • 1
    Ok, now that we agreed that the issue is not the grayscale image, but the non-flat structuring element... Yes, OpenCV can't do that. https://answers.opencv.org/question/59646/non-flat-structuring-elements-for-morphological-operations/ But it's fairly easy to implement ;) – Miki Apr 09 '20 at 15:36
  • @Miki Ok, thanks ! That was the kind of information I was looking for ! I simply wonder why is not implemented in OpenCV, but that is another discussion. – R. N Apr 09 '20 at 15:50

1 Answers1

-1

OpenCV used flat structure element:

[[1, 1, 1],
 [0, 1, 1]]

Scipy used non-flat structuring element:

[[1, 1, 1],
 [0, 1, 5]]
Alex Alex
  • 1,893
  • 1
  • 6
  • 12
  • I am sorry, but I do not understand how it is answering to the question: how obtain a grayscale morphological dilation with OpenCV ? As I mentioned in my question, I observed that OpenCV is able to do such dilation for a flat binary structuring element. This is why I am asking about a grayscale one! – R. N Apr 09 '20 at 15:18