需要帮助将 c# 中的 opencv 转换为 vb.net
Posted
技术标签:
【中文标题】需要帮助将 c# 中的 opencv 转换为 vb.net【英文标题】:Need help converting opencv in c# to vb.net 【发布时间】:2018-05-03 14:38:17 【问题描述】:所以我似乎正在崩溃,试图找出这段代码并将其移植到 vb.net 等价物。坦率地说,我不想要字母的代码字母,我只想对 vb.net 中的图像做同样的处理。问题是我看过的所有地方,建议是使用包装器而不是直接使用 OpenCV。包装器是 emgucv.net
无论如何,我都会展示代码。
static void Decaptcha(string filePath)
// load the file
using (var src = new Mat(filePath))
using (var binaryMask = new Mat())
// lines color is different than text
var linesColor = Scalar.FromRgb(0x70, 0x70, 0x70);
// build a mask of lines
Cv2.InRange(src, linesColor, linesColor, binaryMask);
using (var masked = new Mat())
// build the corresponding image
// dilate lines a bit because aliasing may have filtered borders too much during masking
src.CopyTo(masked, binaryMask);
int linesDilate = 3;
using (var element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(linesDilate, linesDilate)))
Cv2.Dilate(masked, masked, element);
// convert mask to grayscale
Cv2.CvtColor(masked, masked, ColorConversionCodes.BGR2GRAY);
using (var dst = src.EmptyClone())
// repaint big lines
Cv2.Inpaint(src, masked, dst, 3, InpaintMethod.NS);
// destroy small lines
linesDilate = 2;
using (var element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(linesDilate, linesDilate)))
Cv2.Dilate(dst, dst, element);
Cv2.GaussianBlur(dst, dst, new Size(5, 5), 0);
using (var dst2 = dst.BilateralFilter(5, 75, 75))
// basically make it B&W
Cv2.CvtColor(dst2, dst2, ColorConversionCodes.BGR2GRAY);
Cv2.Threshold(dst2, dst2, 255, 255, ThresholdTypes.Otsu);
// save the file
dst2.SaveImage(Path.Combine(
Path.GetDirectoryName(filePath),
Path.GetFileNameWithoutExtension(filePath) + "_dst" + Path.GetExtension(filePath)));
【问题讨论】:
【参考方案1】:这应该让你开始:
从这里开始:
https://www.codeproject.com/Questions/405164/Use-OpenCV-in-web-application
Private Shared Sub Decaptcha(ByVal filePath As String)
Using src = New Mat(filePath)
Using binaryMask = New Mat()
Dim linesColor = Scalar.FromRgb(112, 112, 112)
Cv2.InRange(src, linesColor, linesColor, binaryMask)
Using masked = New Mat()
src.CopyTo(masked, binaryMask)
Dim linesDilate As Integer = 3
Using element = Cv2.GetStructuringElement(MorphShapes.Ellipse, New Size(linesDilate, linesDilate))
Cv2.Dilate(masked, masked, element)
End Using
Cv2.CvtColor(masked, masked, ColorConversionCodes.BGR2GRAY)
Using dst = src.EmptyClone()
Cv2.Inpaint(src, masked, dst, 3, InpaintMethod.NS)
linesDilate = 2
Using element = Cv2.GetStructuringElement(MorphShapes.Ellipse, New Size(linesDilate, linesDilate))
Cv2.Dilate(dst, dst, element)
End Using
Cv2.GaussianBlur(dst, dst, New Size(5, 5), 0)
Using dst2 = dst.BilateralFilter(5, 75, 75)
Cv2.CvtColor(dst2, dst2, ColorConversionCodes.BGR2GRAY)
Cv2.Threshold(dst2, dst2, 255, 255, ThresholdTypes.Otsu)
dst2.SaveImage(Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) & "_dst" + Path.GetExtension(filePath)))
End Using
End Using
End Using
End Using
End Using
End Sub
【讨论】:
除非我遗漏了某些东西,否则该代码中的任何内容似乎都不起作用。它只是充斥着对不存在的事物以及工作方式不同的事物等的引用等等。 “缺失”的东西是对 OpenCV DLL 的引用,我假设你有它们。以上是关于需要帮助将 c# 中的 opencv 转换为 vb.net的主要内容,如果未能解决你的问题,请参考以下文章