我有以下要转换为VB.NET的C#代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System;
using OpenCvSharp;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string sPath = "c:\\users\\myuser\\desktop\\lenna.png";
using (var src = new Mat(sPath, ImreadModes.Color))
using (var srcGray = new Mat(sPath, ImreadModes.GrayScale))
using (var hsv = new Mat())
using (var dst = new Mat())
{
Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV);
Cv2.CvtColor(srcGray, dst, ColorConversionCodes.GRAY2BGR);
var hsvChannels = Cv2.Split(hsv);
var v = hsvChannels[2];
for (int i = 0; i < 8; i++)
{
using (var bin = new Mat())
{
Cv2.Threshold(v, bin, i * 32, 255, ThresholdTypes.Tozero);
Cv2.Threshold(bin, bin, (i + 1) * 32, 255, ThresholdTypes.BinaryInv);
Cv2.FindContours(bin, out var contours, out _, RetrievalModes.External,
ContourApproximationModes.ApproxNone);
Cv2.DrawContours(dst, contours, -1, Scalar.Red, 1);
}
}
Window.ShowImages(dst);
foreach (var m in hsvChannels)
m.Dispose();
}
}
}
}
我设法将其转换,但是“ _”让我头疼。
如果我将其替换为“ Nothing”,则编译器会告诉我“不是最具体”。
如果我这样声明“轮廓”(更具体而言),则编译器告诉我“ Nothing”是无效的参数:
Dim contours As OpenCvSharp.Mat()
这是我的VB.NET尝试:
Imports OpenCvSharp
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim sPath As String = "c:\users\myuser\desktop\lenna.png"
Using src = New Mat(sPath, ImreadModes.Color)
Using srcGray = New Mat(sPath, ImreadModes.GrayScale)
Using hsv = New OpenCvSharp.Mat()
Using dst = New Mat()
Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV)
Cv2.CvtColor(srcGray, dst, ColorConversionCodes.GRAY2BGR)
Dim hsvChannels = Cv2.Split(hsv)
Dim v = hsvChannels(2)
For i As Integer = 0 To 7
Using bin = New Mat()
Cv2.Threshold(v, bin, i * 32, 255, ThresholdTypes.Tozero)
Cv2.Threshold(bin, bin, (i + 1) * 32, 255, ThresholdTypes.BinaryInv)
Dim contours
Cv2.FindContours(bin, contours, Nothing, RetrievalModes.External, ContourApproximationModes.ApproxNone) // Compiler error occurs here
Cv2.DrawContours(dst, contours, -1, Scalar.Red, 1)
End Using
Next i
Window.ShowImages(dst)
For Each m In hsvChannels
m.Dispose()
Next m
End Using
End Using
End Using
End Using
End Sub
End Class
我不确定编译器要我做什么。有人知道吗 使用下划线或双下划线(如某些在线转换器建议的那样)将无效。
这些是FindContours的声明:
Public Shared Sub FindContours(image As InputOutputArray, ByRef contours() As Mat, hierarchy As OutputArray, mode As RetrievalModes, method As ContourApproximationModes, Optional offset As Point? = Nothing)
Public Shared Sub FindContours(image As InputOutputArray, ByRef contours As Point()(), ByRef hierarchy() As HierarchyIndex, mode As RetrievalModes, method As ContourApproximationModes, Optional offset As Point? = Nothing)
答案 0 :(得分:6)
C#中的下划线参数是discard。据我所知,VB没有与此等效的功能。由于这是一个out
参数,因此您需要声明一个本地虚拟变量并传递该变量:
Dim contours As Point()()
Dim unused As HierarchyIndex()
Cv2.FindContours(bin, contours, unused, RetrievalModes.External, ContourApproximationModes.ApproxNone)
还要注意,您的contours
本地声明缺少类型,因此不完整。编译器应将此视为无效(如果您使用Option Strict
进行编译,则应该这样做)。
答案 1 :(得分:1)
编译器不知道您要使用什么重载,因为您没有给contours
指定类型。指定类型contours
是Dim contours as Point()
还是Dim contours as Mat
。
当您对参数值不感兴趣时,在csharp中使用下划线_
。由于VB没有out机制,因此必须为下划线参数Dim hierarchy() As HierarchyIndex
或Dim hierarchy As OutputArray
指定一个临时变量。
使用VB.net时,请确保指定参数类型-并尽可能启用严格的类型检查。这迫使您编写更加类型安全的更干净的代码。这将为您节省很多头痛。