using OpenCvSharp
;
using OpenCvSharp
.Extensions
;
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 static System
.Windows
.Forms
.VisualStyles
.VisualStyleElement
;
using Point
= OpenCvSharp
.Point
;
using Size
= OpenCvSharp
.Size
;
namespace DrawROI
{
public partial class Form1 : Form
{
private System.Drawing.Point RectStartPoint
, tempEndPoint
;
bool blnDraw
;
Mat ImageROI
;
Mat OrgMat
;
private string FilePath
;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender
, MouseEventArgs e
)
{
RectStartPoint
= e
.Location
;
Invalidate();
blnDraw
= true;
}
private void pictureBox1_MouseMove(object sender
, MouseEventArgs e
)
{
if (blnDraw
)
{
if (e
.Button
!= MouseButtons
.Left
)
{
return;
}
tempEndPoint
= e
.Location
;
pictureBox1
.Invalidate();
int X0
, Y0
;
Utilities
.ConvertCoordinates(pictureBox1
, out X0
, out Y0
, e
.X
, e
.Y
);
Utilities
.ConvertCoordinates(pictureBox1
, out X0
, out Y0
, RectStartPoint
.X
, RectStartPoint
.Y
);
int X1
, Y1
;
Utilities
.ConvertCoordinates(pictureBox1
, out X1
, out Y1
, tempEndPoint
.X
, tempEndPoint
.Y
);
Rect tmp_Rect
= new Rect(Math
.Min(X0
, X1
), Math
.Min(Y0
, Y1
), Math
.Abs(X0
- X1
), Math
.Abs(Y0
- Y1
));
ImageROI
= new Mat(OrgMat
, tmp_Rect
);
}
}
private void pictureBox1_MouseUp(object sender
, MouseEventArgs e
)
{
pictureBox2
.Image
= ImageROI
.ToBitmap();
blnDraw
= false;
}
private void button1_Click(object sender
, EventArgs e
)
{
OpenFileDialog openFileDialog
= new OpenFileDialog();
openFileDialog
.ShowDialog();
FilePath
= openFileDialog
.FileName
;
OrgMat
= new Mat(FilePath
, ImreadModes
.Grayscale
);
OrgMat
.MedianBlur(3);
pictureBox1
.Image
= BitmapConverter
.ToBitmap(OrgMat
);
}
private void button3_Click(object sender
, EventArgs e
)
{
if (ImageROI
== null)
{
MessageBox
.Show("请先绘制模板");
return ;
}
Mat RoiClone
= ImageROI
.Clone();
Mat mat3
= new Mat();
Cv2
.MatchTemplate(OrgMat
, RoiClone
, mat3
, TemplateMatchModes
.SqDiff
);
Cv2
.Normalize(mat3
, mat3
, 1, 0, NormTypes
.MinMax
, -1);
Point minLocation
, maxLocation
;
Cv2
.MinMaxLoc(mat3
, out minLocation
, out maxLocation
);
Mat OrgMatClone
= OrgMat
.Clone();
CircleSegment
[] cs
= Cv2
.HoughCircles(RoiClone
, HoughMethods
.Gradient
, 1, 80, 70, 100, 100, 200);
for (int i
= 0; i
< cs
.Count(); i
++)
{
Cv2
.Circle(OrgMatClone
, (int)(cs
[i
].Center
.X
+ minLocation
.X
), (int)(cs
[i
].Center
.Y
+ minLocation
.Y
), (int)cs
[i
].Radius
, new Scalar(0, 0, 255), 2, LineTypes
.AntiAlias
);
Cv2
.Circle(OrgMatClone
, (int)cs
[i
].Center
.X
, (int)cs
[i
].Center
.Y
, 3, new Scalar(0, 0, 255), 2, LineTypes
.AntiAlias
);
textBox1
.Text
= cs
[i
].Center
.X
.ToString();
textBox2
.Text
= cs
[i
].Center
.Y
.ToString();
}
pictureBox1
.Image
= BitmapConverter
.ToBitmap(OrgMatClone
);
}
public class Utilities
{
public static void ConvertCoordinates(PictureBox pic
,
out int X0
, out int Y0
, int x
, int y
)
{
int pic_hgt
= pic
.ClientSize
.Height
;
int pic_wid
= pic
.ClientSize
.Width
;
int img_hgt
= pic
.Image
.Height
;
int img_wid
= pic
.Image
.Width
;
X0
= x
;
Y0
= y
;
switch (pic
.SizeMode
)
{
case PictureBoxSizeMode
.AutoSize
:
case PictureBoxSizeMode
.StretchImage
:
X0
= (int)(img_wid
* x
/ (float)pic_wid
);
Y0
= (int)(img_hgt
* y
/ (float)pic_hgt
);
break;
}
}
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-37673.html