簡體   English   中英

C#下划線作為VB.NET的參數

[英]C# underscore as argument to VB.NET

我有以下要轉換為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)

C#中的下划線參數是一個廢棄值 據我所知,VB沒有與此等效的功能。 由於這是一個out參數,因此您需要聲明一個局部虛擬變量並將其傳遞給:

Dim contours As Point()()
Dim unused As HierarchyIndex()
Cv2.FindContours(bin, contours, unused, RetrievalModes.External, ContourApproximationModes.ApproxNone) 

另請注意,您當地的contours聲明缺少類型,因此不完整。 編譯器應將此視為無效(如果您使用Option Strict編譯,則應該這樣做)。

編譯器不知道您要使用什么重載,因為您沒有給contours指定類型。 指定什么是Type contoursDim contours as Point()Dim contours as Mat

如果您對參數值不感興趣,則在csharp中使用下划線_ 由於VB沒有out機制,因此必須為下划線參數Dim hierarchy As OutputArray Dim hierarchy() As HierarchyIndexDim hierarchy As OutputArray指定一個臨時變量。

使用VB.net時,請確保指定參數類型-如有可能,請啟用嚴格類型檢查。 這迫使您編寫更加類型安全的更干凈的代碼。 這將為您節省很多麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM