c# 使用opencvsharp实现yolov图像数据集增强

c# 使用opencvsharp实现图像数据集增强

1. 前言

1.在深度学习过程中,有时会需要使用一些算法来达到增强数据集的目的,在这里通过opencvsharp使图片旋转90°、180°、270°、沿着y轴翻转,给图像降灰阶、升灰阶等方式来达到数据集的增强。

2.文件放置:

total文件夹使目标文件夹,total1-total8是运行代码后生成的文件夹;
在这里插入图片描述
total文件夹中内容
在这里插入图片描述
在这里插入图片描述

3.代码

因为这些图片是要进行深度学习的,所以标注文档随着图像数据集增强也在变化,特别是旋转和翻转。

        //图像增强 一个大文件夹,包含多个子文件夹
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"E:\ZhuW\Personal\CSharp\yolov3小程序合集\1202\1202\total";
            string[] str = path.Split('\\');

            //获取前路径
            string startPath = string.Empty;
            for (int i = 0; i < str.Length - 1; i++)
            {
                startPath = startPath + str[i] + "\\";

            }
            //获取总目录下的所有文件夹
            string[] dir = Directory.GetDirectories(path);
            //这里是4类
            string endPath = Path.GetFileNameWithoutExtension(path);
            Task.Run(() =>
            {
                for (int i = 0; i < 8; i++)
                {
                    string tmpEndpath = endPath + (i + 1);
                    string tmpPath = startPath + tmpEndpath;
                    Directory.CreateDirectory(tmpPath);
                    if (i == 0)//旋转90°
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    Bitmap bitmap = new Bitmap(files[r]);
                                    bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                    bitmap.Save(path3 + "\\" + name + ".jpg");
                                    for (int z = 0; z < files.Length; z++)
                                    {
                                        string txtname = name + ".txt";
                                        //Directory.CreateDirectory(path3 + "\\" + txtname);
                                        if (Path.GetFileName(files[z]) == txtname)
                                        {
                                            string[] lines = File.ReadAllLines(files[z]);

                                            string[,] msg = new string[lines.Length, 5];
                                            for (int x = 0; x < lines.Length; x++)
                                            {
                                                string[] contents = lines[x].Split(' ');
                                                double w = 1000;
                                                double h = 1000;
                                                string x_ = contents[1];
                                                string y_ = contents[2];
                                                string w_ = contents[3];
                                                string h_ = contents[4];
                                                double x1, y1, x2, y2;
                                                x1 = w * Convert.ToDouble(x_) - 0.5 * w * Convert.ToDouble(w_);
                                                x2 = w * Convert.ToDouble(x_) + 0.5 * w * Convert.ToDouble(w_);
                                                y1 = h * Convert.ToDouble(y_) - 0.5 * h * Convert.ToDouble(h_);
                                                y2 = h * Convert.ToDouble(y_) + 0.5 * h * Convert.ToDouble(h_);

                                                int w1 = (int)(x2 - x1);//获取标注框的宽
                                                int h1 = (int)(y2 - y1);//获取标注框的高
                                                int new_x1 = (int)(1000 - y1 - h1);
                                                int new_y1 = (int)(x1);
                                                int new_w1 = (int)h1;
                                                int new_h1 = (int)w1;
                                                int new_x2 = new_x1 + new_w1;
                                                int new_y2 = new_y1 + new_h1;
                                                double new_x_ = (double)(new_x1 + new_x2) / (double)(2 * 1000);
                                                double new_y_ = (double)(new_y1 + new_y2) / (double)(2 * 1000);
                                                double new_w_ = (double)(new_x2 - new_x1) / (double)1000;
                                                double new_h_ = (double)(new_y2 - new_y1) / (double)1000;
                                                msg[x, 0] = contents[0];
                                                msg[x, 1] = Convert.ToString(new_x_);
                                                msg[x, 2] = Convert.ToString(new_y_);
                                                msg[x, 3] = Convert.ToString(new_w_);
                                                msg[x, 4] = Convert.ToString(new_h_);
                                                #region
                                                //FileStream fileStream1 = new FileStream(item,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
                                                //string msg = str[0] + " " + x1 + " " + y1 + " " + x2 + " " + y2;
                                                //byte[] bytes = Encoding.UTF8.GetBytes(msg);
                                                //fileStream1.Write(bytes,0,bytes.Length);
                                                #endregion
                                            }
                                            StreamWriter streamWriter = new StreamWriter(path3 + "\\" + txtname);
                                            for (int d = 0; d < msg.GetLength(0); d++)
                                            {
                                                streamWriter.WriteLine(msg[d, 0] + " " + msg[d, 1] + " " + msg[d, 2] + " " + msg[d, 3] + " " + msg[d, 4]);
                                            }
                                            streamWriter.Flush();
                                            streamWriter.Close();
                                            break;
                                        }
                                    }
                                }

                                #region MyRegion
                                //using (Mat mat = new Mat(files[j], ImreadModes.AnyColor))
                                //{
                                //    string name = Path.GetFileNameWithoutExtension(files[j]);
                                //    mat.SaveImage(path3 + "\\" + name + ".bmp");
                                //}
                                #endregion

                            }

                        }
                    }
                    else if (i == 1) //旋转180°
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    Bitmap bitmap = new Bitmap(files[r]);
                                    bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    bitmap.Save(path3 + "\\" + name + ".jpg");
                                    for (int z = 0; z < files.Length; z++)
                                    {
                                        string txtname = name + ".txt";
                                        //Directory.CreateDirectory(path3 + "\\" + txtname);
                                        if (Path.GetFileName(files[z]) == txtname)
                                        {
                                            string[] lines = File.ReadAllLines(files[z]);

                                            string[,] msg = new string[lines.Length, 5];
                                            for (int x = 0; x < lines.Length; x++)
                                            {
                                                string[] contents = lines[x].Split(' ');
                                                double w = 1000;
                                                double h = 1000;
                                                string x_ = contents[1];
                                                string y_ = contents[2];
                                                string w_ = contents[3];
                                                string h_ = contents[4];
                                                double x1, y1, x2, y2;
                                                x1 = w * Convert.ToDouble(x_) - 0.5 * w * Convert.ToDouble(w_);
                                                x2 = w * Convert.ToDouble(x_) + 0.5 * w * Convert.ToDouble(w_);
                                                y1 = h * Convert.ToDouble(y_) - 0.5 * h * Convert.ToDouble(h_);
                                                y2 = h * Convert.ToDouble(y_) + 0.5 * h * Convert.ToDouble(h_);

                                                int w1 = (int)(x2 - x1);//获取标注框的宽
                                                int h1 = (int)(y2 - y1);//获取标注框的高
                                                int new_x1 = (int)(1000 - x1 - w1);
                                                int new_y1 = (int)(1000 - y1 - h1);
                                                int new_w1 = w1;
                                                int new_h1 = h1;
                                                int new_x2 = new_x1 + w1;
                                                int new_y2 = new_y1 + h1;
                                                double new_x_ = (double)(new_x1 + new_x2) / (double)(2 * 1000);
                                                double new_y_ = (double)(new_y1 + new_y2) / (double)(2 * 1000);
                                                double new_w_ = (double)(new_x2 - new_x1) / (double)1000;
                                                double new_h_ = (double)(new_y2 - new_y1) / (double)1000;
                                                msg[x, 0] = contents[0];
                                                msg[x, 1] = Convert.ToString(new_x_);
                                                msg[x, 2] = Convert.ToString(new_y_);
                                                msg[x, 3] = Convert.ToString(new_w_);
                                                msg[x, 4] = Convert.ToString(new_h_);
                                                #region
                                                //FileStream fileStream1 = new FileStream(item,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
                                                //string msg = str[0] + " " + x1 + " " + y1 + " " + x2 + " " + y2;
                                                //byte[] bytes = Encoding.UTF8.GetBytes(msg);
                                                //fileStream1.Write(bytes,0,bytes.Length);
                                                #endregion
                                            }
                                            StreamWriter streamWriter = new StreamWriter(path3 + "\\" + txtname);
                                            for (int d = 0; d < msg.GetLength(0); d++)
                                            {
                                                streamWriter.WriteLine(msg[d, 0] + " " + msg[d, 1] + " " + msg[d, 2] + " " + msg[d, 3] + " " + msg[d, 4]);
                                            }
                                            streamWriter.Flush();
                                            streamWriter.Close();
                                            break;
                                        }
                                    }
                                }

                                #region MyRegion
                                //using (Mat mat = new Mat(files[j], ImreadModes.AnyColor))
                                //{
                                //    string name = Path.GetFileNameWithoutExtension(files[j]);
                                //    mat.SaveImage(path3 + "\\" + name + ".bmp");
                                //}
                                #endregion

                            }

                        }
                    }
                    else if (i == 2)//旋转270°
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    Bitmap bitmap = new Bitmap(files[r]);
                                    bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                    bitmap.Save(path3 + "\\" + name + ".jpg");
                                    for (int z = 0; z < files.Length; z++)
                                    {
                                        string txtname = name + ".txt";
                                        //  Directory.CreateDirectory(path3 + "\\" + txtname);
                                        if (Path.GetFileName(files[z]) == txtname)
                                        {
                                            string[] lines = File.ReadAllLines(files[z]);

                                            string[,] msg = new string[lines.Length, 5];
                                            for (int x = 0; x < lines.Length; x++)
                                            {
                                                string[] contents = lines[x].Split(' ');
                                                double w = 1000;
                                                double h = 1000;
                                                string x_ = contents[1];
                                                string y_ = contents[2];
                                                string w_ = contents[3];
                                                string h_ = contents[4];
                                                double x1, y1, x2, y2;
                                                x1 = w * Convert.ToDouble(x_) - 0.5 * w * Convert.ToDouble(w_);
                                                x2 = w * Convert.ToDouble(x_) + 0.5 * w * Convert.ToDouble(w_);
                                                y1 = h * Convert.ToDouble(y_) - 0.5 * h * Convert.ToDouble(h_);
                                                y2 = h * Convert.ToDouble(y_) + 0.5 * h * Convert.ToDouble(h_);

                                                int w1 = (int)(x2 - x1);//获取标注框的宽
                                                int h1 = (int)(y2 - y1);//获取标注框的高
                                                int new_x1 = (int)(y1);
                                                int new_y1 = (int)(1000 - x1 - w1);
                                                int new_w1 = h1;
                                                int new_h1 = w1;
                                                int new_x2 = new_x1 + new_w1;
                                                int new_y2 = new_y1 + new_h1;
                                                double new_x_ = (double)(new_x1 + new_x2) / (double)(2 * 1000);
                                                double new_y_ = (double)(new_y1 + new_y2) / (double)(2 * 1000);
                                                double new_w_ = (double)(new_x2 - new_x1) / (double)1000;
                                                double new_h_ = (double)(new_y2 - new_y1) / (double)1000;
                                                msg[x, 0] = contents[0];
                                                msg[x, 1] = Convert.ToString(new_x_);
                                                msg[x, 2] = Convert.ToString(new_y_);
                                                msg[x, 3] = Convert.ToString(new_w_);
                                                msg[x, 4] = Convert.ToString(new_h_);
                                                #region
                                                //FileStream fileStream1 = new FileStream(item,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
                                                //string msg = str[0] + " " + x1 + " " + y1 + " " + x2 + " " + y2;
                                                //byte[] bytes = Encoding.UTF8.GetBytes(msg);
                                                //fileStream1.Write(bytes,0,bytes.Length);
                                                #endregion
                                            }
                                            StreamWriter streamWriter = new StreamWriter(path3 + "\\" + txtname);
                                            for (int d = 0; d < msg.GetLength(0); d++)
                                            {
                                                streamWriter.WriteLine(msg[d, 0] + " " + msg[d, 1] + " " + msg[d, 2] + " " + msg[d, 3] + " " + msg[d, 4]);
                                            }
                                            streamWriter.Flush();
                                            streamWriter.Close();
                                            break;
                                        }
                                    }
                                }

                                #region MyRegion
                                //using (Mat mat = new Mat(files[j], ImreadModes.AnyColor))
                                //{
                                //    string name = Path.GetFileNameWithoutExtension(files[j]);
                                //    mat.SaveImage(path3 + "\\" + name + ".bmp");
                                //}
                                #endregion

                            }

                        }
                    }
                    else if (i == 3)//沿y轴进行翻转
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    using (Mat mat = new Mat(files[r], ImreadModes.AnyColor))
                                    {
                                        Mat dst2 = new Mat();
                                        Cv2.Flip(mat, dst2, FlipMode.Y);
                                        dst2.SaveImage(path3 + "\\" + name + ".jpg");

                                    }

                                    for (int z = 0; z < files.Length; z++)
                                    {
                                        string txtname = name + ".txt";
                                        //    Directory.CreateDirectory(path3 + "\\" + txtname);
                                        if (Path.GetFileName(files[z]) == txtname)
                                        {
                                            string[] lines = File.ReadAllLines(files[z]);

                                            string[,] msg = new string[lines.Length, 5];
                                            for (int x = 0; x < lines.Length; x++)
                                            {
                                                string[] contents = lines[x].Split(' ');
                                                double w = 1000;
                                                double h = 1000;
                                                string x_ = contents[1];
                                                string y_ = contents[2];
                                                string w_ = contents[3];
                                                string h_ = contents[4];
                                                double x1, y1, x2, y2;
                                                x1 = w * Convert.ToDouble(x_) - 0.5 * w * Convert.ToDouble(w_);
                                                x2 = w * Convert.ToDouble(x_) + 0.5 * w * Convert.ToDouble(w_);
                                                y1 = h * Convert.ToDouble(y_) - 0.5 * h * Convert.ToDouble(h_);
                                                y2 = h * Convert.ToDouble(y_) + 0.5 * h * Convert.ToDouble(h_);

                                                int w1 = (int)(x2 - x1);//获取标注框的宽
                                                int h1 = (int)(y2 - y1);//获取标注框的高
                                                int new_x1 = (int)(1000 - x1 - w1);
                                                int new_y1 = (int)(y1);
                                                int new_w1 = w1;
                                                int new_h1 = h1;
                                                int new_x2 = new_x1 + new_w1;
                                                int new_y2 = new_y1 + new_h1;
                                                double new_x_ = (double)(new_x1 + new_x2) / (double)(2 * w);
                                                double new_y_ = (double)(new_y1 + new_y2) / (double)(2 * w);
                                                double new_w_ = (double)(new_x2 - new_x1) / (double)w;
                                                double new_h_ = (double)(new_y2 - new_y1) / (double)w;
                                                msg[x, 0] = contents[0];
                                                msg[x, 1] = Convert.ToString(new_x_);
                                                msg[x, 2] = Convert.ToString(new_y_);
                                                msg[x, 3] = Convert.ToString(new_w_);
                                                msg[x, 4] = Convert.ToString(new_h_);

                                            }
                                            StreamWriter streamWriter = new StreamWriter(path3 + "\\" + txtname);
                                            for (int d = 0; d < msg.GetLength(0); d++)
                                            {
                                                streamWriter.WriteLine(msg[d, 0] + " " + msg[d, 1] + " " + msg[d, 2] + " " + msg[d, 3] + " " + msg[d, 4]);
                                            }
                                            streamWriter.Flush();
                                            streamWriter.Close();
                                            break;
                                        }
                                    }
                                }



                            }

                        }
                    }
                    else if (i == 4)//灰阶加5
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    //将图像加5
                                    using (Mat gray = new Mat(files[r], ImreadModes.Grayscale))
                                    {
                                        int cols = gray.Width;
                                        int rows = gray.Height;
                                        for (int r1 = 0; r1 < rows; r1++)
                                        {
                                            for (int c = 0; c < cols; c++)
                                            {
                                                byte p = gray.At<byte>(r1, c);

                                                if (p != 0)
                                                {
                                                    int cal = p + (5);
                                                    if (cal > 255) cal = 255;
                                                    else if (cal < 0) cal = 0;
                                                    byte value = byte.Parse((cal).ToString());
                                                    gray.Set(r1, c, value);
                                                }

                                            }
                                        }
                                        gray.SaveImage(path3 + "\\" + name + ".jpg");
                                    }

                                }
                                else if (Path.GetExtension(files[r]) == ".txt")
                                {
                                    FileInfo fileInfo = new FileInfo(files[r]);
                                    string newPath = path3 + "\\" + fileInfo.Name;
                                    File.Copy(files[r], newPath, true);

                                }

                                #region MyRegion
                                //using (Mat mat = new Mat(files[j], ImreadModes.AnyColor))
                                //{
                                //    string name = Path.GetFileNameWithoutExtension(files[j]);
                                //    mat.SaveImage(path3 + "\\" + name + ".bmp");
                                //}
                                #endregion

                            }

                        }
                    }
                    else if (i == 5)//灰阶加10
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    //将图像加5
                                    using (Mat gray = new Mat(files[r], ImreadModes.Grayscale))
                                    {
                                        int cols = gray.Width;
                                        int rows = gray.Height;
                                        for (int r1 = 0; r1 < rows; r1++)
                                        {
                                            for (int c = 0; c < cols; c++)
                                            {
                                                byte p = gray.At<byte>(r1, c);

                                                if (p != 0)
                                                {
                                                    int cal = p + (10);
                                                    if (cal > 255) cal = 255;
                                                    else if (cal < 0) cal = 0;
                                                    byte value = byte.Parse((cal).ToString());
                                                    gray.Set(r1, c, value);
                                                }

                                            }
                                        }
                                        gray.SaveImage(path3 + "\\" + name + ".jpg");
                                    }

                                }
                                else if (Path.GetExtension(files[r]) == ".txt")
                                {
                                    FileInfo fileInfo = new FileInfo(files[r]);
                                    string newPath = path3 + "\\" + fileInfo.Name;
                                    File.Copy(files[r], newPath, true);

                                }

                          
                            }

                        }
                    }
                    else if (i == 6)//灰阶-5
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    //将图像加5
                                    using (Mat gray = new Mat(files[r], ImreadModes.Grayscale))
                                    {
                                        int cols = gray.Width;
                                        int rows = gray.Height;
                                        for (int r1 = 0; r1 < rows; r1++)
                                        {
                                            for (int c = 0; c < cols; c++)
                                            {
                                                byte p = gray.At<byte>(r1, c);

                                                if (p != 0)
                                                {
                                                    int cal = p + (-5);
                                                    if (cal > 255) cal = 255;
                                                    else if (cal < 0) cal = 0;
                                                    byte value = byte.Parse((cal).ToString());
                                                    gray.Set(r1, c, value);
                                                }

                                            }
                                        }
                                        gray.SaveImage(path3 + "\\" + name + ".jpg");
                                    }

                                }
                                else if (Path.GetExtension(files[r]) == ".txt")
                                {
                                    FileInfo fileInfo = new FileInfo(files[r]);
                                    string newPath = path3 + "\\" + fileInfo.Name;
                                    File.Copy(files[r], newPath, true);

                                }


                            }

                        }
                    }
                    else if (i == 7)//灰阶-10
                    {
                        for (int j = 0; j < dir.Length; j++)
                        {
                            string a = Path.GetFileNameWithoutExtension(dir[j]);
                            string path3 = Path.Combine(tmpPath, a);
                            Directory.CreateDirectory(Path.Combine(tmpPath, a));
                            string[] files = Directory.GetFiles(dir[j]);
                            //遍历每个子文件夹下的文件
                            for (int r = 0; r < files.Length; r++)
                            {
                                if (Path.GetExtension(files[r]) == ".jpg")
                                {
                                    string name = Path.GetFileNameWithoutExtension(files[r]);
                                    //将图像加5
                                    using (Mat gray = new Mat(files[r], ImreadModes.Grayscale))
                                    {
                                        int cols = gray.Width;
                                        int rows = gray.Height;
                                        for (int r1 = 0; r1 < rows; r1++)
                                        {
                                            for (int c = 0; c < cols; c++)
                                            {
                                                byte p = gray.At<byte>(r1, c);

                                                if (p != 0)
                                                {
                                                    int cal = p + (-10);
                                                    if (cal > 255) cal = 255;
                                                    else if (cal < 0) cal = 0;
                                                    byte value = byte.Parse((cal).ToString());
                                                    gray.Set(r1, c, value);
                                                }

                                            }
                                        }
                                        gray.SaveImage(path3 + "\\" + name + ".jpg");
                                    }
                                }
                                else if (Path.GetExtension(files[r]) == ".txt")
                                {
                                    FileInfo fileInfo = new FileInfo(files[r]);
                                    string newPath = path3 + "\\" + fileInfo.Name;
                                    File.Copy(files[r], newPath, true);

                                }


                            }

                        }
                    }
                }
            });


        }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、主要内容:OpenCV能够实现强大丰富的图像处理,但是它缺少一个能够支持它运行的界面。Csharp经过多年的发展,得益于它的“所见及所得”能力,非常方便编写界面。这两者如果能够“双剑合璧”,将有效帮助实际工作产出。本课着重推荐GOCW采用“Csharp基于CLR直接调用Opencv编写的算法库”方法,能够将最新的OpenCV技术引入进来,同时保证生成程序的最小化。    为了进一步说明Csharp和OpenCV的结合使用,首先一个较为完整的基于winform实现答题卡识别的例子,相比较之前的实现,本次进一步贴近生产实际、内涵丰富,对算法也进行了进一步提炼。同时我们对WPF下对OpenCV函数的调用、OpenCV.js的调用进行相关教授。       二、课程结构1、 EmguCV、OpenCVSharp和GOCW之间进行比较(方便代码编写、能够融入最新的算法、速度有保障、方便调试找错、拒绝黑箱化);2、视频采集模块的构建,视频采集和图像处理之间的关系;3、视频采集专用的SDK和“陪练”系统的介绍;4、在视频增强类项目中和图像处理项目中,算法的选择;5、Csharp界面设计、图片的存储和其他构建设计;6、较为完整的答题卡识别例子,兼顾界面设计和算法分析;8、WPF基于GOCW也同样可以基于GOCW实现算法调用;webForm虽然也可以通过类似方法调用,但是OpenCV.JS的方法更现代高效。9、关于软件部署的相关要点和窍门。       三、知识要点:1、基本环境构建和程序框架;2、CLR基本原理和应用方法;3、接入、采集、模拟输入;4、图像处理,通过构建循环采集图片;5、增强和实时处理;6、基于投影等技术的答题卡识别算法;7、存储、转换;8、部署交付。        课程能够帮助你掌握Csharp调用Opencv的基本方法,获得相应框架代码和指导;从而进一步提升实现“基于图像处理”的解决方案能力。  
使用OpenCvSharp实现Yolov3可以按照以下步骤进行: 1. 下载Yolov3的预训练权重文件。Yolov3是一个深度学习网络模型,预训练权重文件包含了模型在大规模数据上经过训练的参数。 2. 在项目中集成OpenCvSharp库。OpenCvSharp是一个用于处理图像和视频的开源计算机视觉库,可以在C#使用该库进行图像处理和分析。 3. 加载预训练权重文件。使用OpenCvSharp的接口加载预训练权重文件,并将其转换为OpenCvSharp的Mat对象。 4. 对输入图像进行处理。使用OpenCvSharp相应的函数对从图像中提取感兴趣区域(Region of Interest)或进行图像预处理。 5. 将处理后的图像输入Yolov3模型中。使用OpenCvSharp的接口将处理后的图像输入到Yolov3模型中进行目标检测。 6. 解析Yolov3模型的输出。根据Yolov3的输出格式解析模型的输出,得到检测到的目标的类别、位置和置信度等信息。 7. 绘制出检测结果。使用OpenCvSharp的相应函数将检测到的目标的位置和类别信息绘制到原图上,以便观察和分析结果。 8. 根据需求进行后续处理。根据具体需求,可以对检测结果进行进一步的分析、筛选或后处理。 需要注意的是,Yolov3是一个基于深度学习的复杂模型,对计算资源要求较高,因此在使用时需要配置好相应的硬件环境。此外,OpenCvSharp是一个图像处理库,对于深度学习模型的训练以及模型参数的调优等步骤,需要使用其他工具来完成。以上只是基本的步骤,具体的实现细节还需要根据实际情况进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值