C#中使用opencv

安装下述三个包

安装:OpenCvSharp4.Extensions


Mat img1 = new Mat(imgPath, ImreadModes.Color);
OpenCvSharp.Size imgSize = img1.Size();
int img1Width = imgSize.Width;
int img1Height = imgSize.Height;
string name = Path.GetFileName(imgPath).Split('.')[0];
string dirName = Path.GetDirectoryName(imgPath);
DirectoryInfo path001 = new DirectoryInfo(dirName);
DirectoryInfo path111 = Directory.GetParent(textBox1.Text.Trim());
DirectoryInfo path112 = Directory.GetParent(path111.FullName);
string labelPath = Path.Combine(Path.Combine(path112.FullName,"labels", path001.Name), name + ".txt");
Current_LabelPath = labelPath;
label2.Text = labelPath;
try
{
using (StreamReader sr = new StreamReader(labelPath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
string[] res1 = line.Split(' ');
double center_x = img1Width * Convert.ToDouble(res1[1]);
int centerX = Convert.ToInt32(center_x);
double center_y = img1Height * Convert.ToDouble(res1[2]);
int centerY = Convert.ToInt32(center_y);
double wd = img1Height * Convert.ToDouble(res1[3]);
int Width = Convert.ToInt32(wd);
double hg = img1Height * Convert.ToDouble(res1[4]);
int Height = Convert.ToInt32(hg);
OpenCvSharp.Point truck_a = new OpenCvSharp.Point(centerX - Width/2, centerY-Height/2);
OpenCvSharp.Point truck_b = new OpenCvSharp.Point(centerX + Width/2, centerY + Height/2);
Cv2.Rectangle(img1, truck_a, truck_b, Scalar.Red, 15);
}
Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(img1);
pictureBox1.Image = bitmap;