openCvSharp 4-对图像应用灰度蒙版

0md85ypi  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(177)

在我的代码中,我拍了一张照片,找到边缘,使它们变厚,反转图像,应用模糊,最后得到一张白色的图像,有黑色的模糊线。模糊的区域显然是灰色色调。
我想使用这个作为遮罩应用到基础图像,但由于某种原因,遮罩被视为二进制遮罩,所以无论是黑色还是白色。模糊产生的灰色调被忽略。为什么会发生这种情况?如何更改我的代码,以便在遮罩中考虑灰色调?

'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)
mhd8tkvw

mhd8tkvw1#

掩码始终是二进制的。
你想要的是混合。
混合需要乘法(以及加法、减法)。
你必须自己使用OpenCV Mat对象来表达计算。OpenCV不包含一个在一个步骤中完成混合的API。

相关问题