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);
}
}
}
}
}
});
}