英文:
openCvSharp4 - apply graytone mask to image
问题
在我的代码中,我拍照,找到边缘,加粗它们,反转图像,应用模糊效果,最终得到一个白色图像,其中有黑色模糊的线条。模糊区域显然是灰色调。
我想将这个图像用作蒙版,应用到基础图像上,但出于某种原因,蒙版被视为二进制蒙版,要么是黑色,要么是白色。模糊效果产生的灰度值被忽略了。
为什么会发生这种情况?如何更改我的代码以便在蒙版中考虑灰度值?
'加载叠加图像
Dim overlayBmp As New Bitmap("dust/dust1.png")
'加载图像
Dim matImg As Mat = BitmapConverter.ToMat(fr_bm)
'将图像转换为灰度
Dim grayImg As New Mat()
Cv2.CvtColor(matImg, grayImg, ColorConversionCodes.BGR2GRAY)
'使用Canny边缘检测算法检测边缘
Dim edgesImg As New Mat()
Cv2.Canny(grayImg, edgesImg, 100, 200)
'对检测到的边缘图像应用膨胀
Dim dilatedImg As New Mat()
Dim element As Mat = Cv2.GetStructuringElement(MorphShapes.Ellipse, New Size(5, 5))
Cv2.Dilate(edgesImg, dilatedImg, element)
'反转边缘
Cv2.BitwiseNot(dilatedImg, dilatedImg)
'对图像应用高斯模糊
Dim blurMask As New Mat()
Cv2.GaussianBlur(dilatedImg, blurMask, New Size(21, 21), 0)
'从原始图像创建4通道Mat
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)
'从叠加图像创建4通道Mat
Dim overlayMat As Mat = New Mat(overlayBmp.Height, overlayBmp.Width, MatType.CV_8UC4, 4)
Cv2.CvtColor(BitmapConverter.ToMat(overlayBmp), overlayMat, ColorConversionCodes.BGR2BGRA)
'创建合成图像
Dim compositeMat1 As New Mat()
Cv2.BitwiseAnd(overlayMat, overlayMat, compositeMat1, blurMask)
英文:
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)
答案1
得分: 1
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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论