using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 审核打标图像
{
public partial class Form1 : Form
{
string Current_ImgPath = string.Empty;
string Current_LabelPath = string.Empty;
System.Drawing.Point mouseDownPoint = new System.Drawing.Point();
bool isMove = false;
int zoomStep = 160;
string ParamPath = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
this.dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Index";
dataGridView1.Columns[1].Name = "图像路径";
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
try
{
this.ParamPath = File.ReadAllText(@"./ParamPath.txt");
textBox1.Text = this.ParamPath;
DirectoryInfo imgDir = new DirectoryInfo(this.ParamPath);
FileInfo[] filesInfo = imgDir.GetFiles();
int count = 1;
foreach (var item in filesInfo)
{
dataGridView1.Rows.Add(count.ToString(), item.FullName);
count++;
}
}
catch
{
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.Description = "选择目录";
folder.SelectedPath = ParamPath;
if (folder.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folder.SelectedPath;
ParamPath = folder.SelectedPath;
}
try
{
System.IO.File.WriteAllText(@"./ParamPath.txt", ParamPath, Encoding.UTF8);
}
catch (Exception exp)
{
MessageBox.Show("参数配置路径写入本地失败-失败原因:" + exp.Message);
}
dataGridView1.Rows.Clear();
DirectoryInfo imgDir = new DirectoryInfo(folder.SelectedPath);
FileInfo[] filesInfo = imgDir.GetFiles();
int count = 1;
foreach (var item in filesInfo)
{
dataGridView1.Rows.Add(count.ToString(), item.FullName);
count++;
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("你确定移除图像和标签到“待保留文件夹”中吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
}
else
{
return;
}
DirectoryInfo path111 = Directory.GetParent(textBox1.Text.Trim());
DirectoryInfo path112 = Directory.GetParent(path111.FullName);
string dst_dir = Path.Combine(path112.FullName, "待保留图像和标签");
if (!Directory.Exists(dst_dir))
{
Directory.CreateDirectory(dst_dir);
}
string NowTime1 = DateTime.Now.ToString("hhmmsss");
string imgName = Path.GetFileName(Current_ImgPath);
string dst_path_img = Path.Combine(dst_dir, NowTime1+"---"+imgName);
File.Move(Current_ImgPath, dst_path_img);
string labelName = Path.GetFileName(Current_LabelPath);
string dst_path_label = Path.Combine(dst_dir, NowTime1 + "---" + labelName);
File.Move(Current_LabelPath, dst_path_label);
dataGridView1.Rows.Clear();
DirectoryInfo imgDir = new DirectoryInfo(textBox1.Text.Trim());
FileInfo[] filesInfo = imgDir.GetFiles();
int count = 1;
foreach (var item in filesInfo)
{
dataGridView1.Rows.Add(count.ToString(), item.FullName);
count++;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
try
{
string imgPath = dataGridView1.CurrentCell.Value.ToString();
label1.Text = imgPath;
Current_ImgPath = imgPath;
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;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
pictureBox1.Focus();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Focus();
if (isMove)
{
int x, y;
int moveX, moveY;
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
pictureBox1.Location = new System.Drawing.Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
int ow = pictureBox1.Width;
int oh = pictureBox1.Height;
int VX, VY;
if (e.Delta > 0)
{
pictureBox1.Width += zoomStep;
pictureBox1.Height += zoomStep;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
if (e.Delta < 0)
{
if (pictureBox1.Width < pictureBox1.Image.Width / 5 || pictureBox1.Height < pictureBox1.Image.Height / 5)
{
pictureBox1.Width = pictureBox1.Image.Width / 5;
pictureBox1.Height = pictureBox1.Image.Height / 5;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
else
{
pictureBox1.Width -= zoomStep;
pictureBox1.Height -= zoomStep;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
}
VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
}
}
}