vb.netmaskopencvgrayscalepython

openCvSharp4 - apply graytone mask to image


In my code, I take a picture, find the edges, make them thicker, invert the image, apply a blur and end up with a white image, with black blurred lines. The blurred zones are obviously gray tones.

I want to use this as a mask to apply to a base image, but for some reason the mask is taken as binary mask, so either black or white. The graytones produced by the blur are ignored. why is this happening? how can my code be changed so it takes graytones into account in a mask?

'Load overlay image
Dim overlayBmp As New Bitmap("dust/dust1.png")

'Load the Image
Dim matImg As Mat = BitmapConverter.ToMat(fr_bm)

'Convert Image to Grayscale
Dim grayImg As New Mat()
Cv2.CvtColor(matImg, grayImg, ColorConversionCodes.BGR2GRAY)

'Detect Edges using the Canny Edge Detection Algorithm
Dim edgesImg As New Mat()
Cv2.Canny(grayImg, edgesImg, 100, 200)

'Apply Dilation to the Edge Detected Image
Dim dilatedImg As New Mat()
Dim element As Mat = Cv2.GetStructuringElement(MorphShapes.Ellipse, New Size(5, 5))
Cv2.Dilate(edgesImg, dilatedImg, element)

'invert the edges
Cv2.BitwiseNot(dilatedImg, dilatedImg)

'Apply Gaussian Blur to the Image
Dim blurMask As New Mat()
Cv2.GaussianBlur(dilatedImg, blurMask, New Size(21, 21), 0)

'Create a 4-Channel Mat from the Original Image
Dim originalMat As Mat = New Mat(fr_bm.Height, fr_bm.Width, MatType.CV_8UC4, 4)
Cv2.CvtColor(BitmapConverter.ToMat(fr_bm), originalMat, ColorConversionCodes.BGR2BGRA)

'Create a 4-Channel Mat from the Overlay Image
Dim overlayMat As Mat = New Mat(overlayBmp.Height, overlayBmp.Width, MatType.CV_8UC4, 4)
Cv2.CvtColor(BitmapConverter.ToMat(overlayBmp), overlayMat, ColorConversionCodes.BGR2BGRA)

'Create a Composite Image
Dim compositeMat1 As New Mat()
Cv2.BitwiseAnd(overlayMat, overlayMat, compositeMat1, blurMask)

Solution

  • A mask is always binary.

    What you want is blending.

    Blending requires multiplication (and addition, subtraction).

    You will have to express the calculation yourself, using OpenCV Mat objects. OpenCV does not contain an API that does blending in one step.