tmi*_*hty 1 c# vb.net arguments
我有以下要转换为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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我设法转换它,但"_"让我头疼.
如果我用"Nothing"替换它,编译器会告诉我"不是最具体的".
如果我像这样声明"轮廓"(更具体),编译器告诉我"Nothing"是一个无效的参数:
Dim contours As OpenCvSharp.Mat()
Run Code Online (Sandbox Code Playgroud)
这是我的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
Run Code Online (Sandbox Code Playgroud)
我不确定编译器想要什么.有人知道吗?使用下划线或双下划线(如某些在线转换器建议的)将无效.
这些是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)
Run Code Online (Sandbox Code Playgroud)
C#中的下划线参数是丢弃.据我所知,VB与此无关.由于这是一个out参数,您需要声明一个本地虚拟变量并传递:
Dim contours As Point()()
Dim unused As HierarchyIndex()
Cv2.FindContours(bin, contours, unused, RetrievalModes.External, ContourApproximationModes.ApproxNone)
Run Code Online (Sandbox Code Playgroud)
另请注意,您的本地声明contours缺少类型,因此不完整.编译器应该将其拒绝为无效(如果您正在编译Option Strict,则应该这样做).
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |