Reputation: 11399
I have the following C# code which I want to convert to VB.NET:
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();
}
}
}
}
I managed to convert it, but the "_" gives me a headache.
The compiler tells me "Not the most specific" if I replace it with "Nothing".
And if I declare "contours" like that (to be more specific), the compiler tells me that "Nothing" is an invalid argument:
Dim contours As OpenCvSharp.Mat()
Here is my VB.NET attempt:
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
I'm not sure what the compiler wants from me. Does anybody know? Using an underscore or double underscore (as some online converters suggested) won't work.
These are the declarations of 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)
Upvotes: 1
Views: 328
Reputation: 545618
An underscore argument in C# is a discard. VB has no equivalent of this, as far as I know. Since this is an out
parameter, you need to declare a local dummy variable and pass that:
Dim contours As Point()()
Dim unused As HierarchyIndex()
Cv2.FindContours(bin, contours, unused, RetrievalModes.External, ContourApproximationModes.ApproxNone)
Also note that your local declaration of contours
lacks a type and is thus incomplete. The compiler should reject this as invalid (if you’re compiling with Option Strict
, which you should).
Upvotes: 6
Reputation: 556
The compiler does not know what overload you want to use, because you did not give a type to contours
. Specify what Type contours
is, either Dim contours as Point()
or Dim contours as Mat
.
An underscore _
is used in csharp when you're not interested in the parameter value. Because VB does not have a out mechanism, you have to specify a temporary variable for the underscore argument Dim hierarchy() As HierarchyIndex
or Dim hierarchy As OutputArray
.
When using VB.net make sure you specify parameter types - and if possible enable strict type checking. This forces you to write cleaner code that is more type safe. This will save you a lot of headaches.
Upvotes: 1