需要帮助将c#中的opencv转换为vb.net

时间:2018-05-03 14:38:17

标签: c# vb.net opencv porting

所以我遇到了似乎正在崩溃的问题,试图弄清楚这些代码并将其移植到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 个答案:

答案 0 :(得分:0)

这应该让你开始:

从这里开始:

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