《学习OpenCV(中文版)》
作者:(美)布拉德斯基(Bradski,G.)
(美)克勒(Kaehler,A.) 著
出版社:清华大学出版社
出版时间:2009年10月
https://zhidao.baidu.com/question/2073567152212492428.html
一、五角星
已知五角星的外接圆半径r,如何求五角星十个顶点的坐标?
如图,
A(rcos18°,rsin18°)
B(rcos90°,rsin90°)
C(rcos162°,rsin162°)
D(rcos234°,rsin234°)
E(rcos306°,rsin306°)
OF=rsin18°/sin126°=r'
F(r'cos54°,r'sin54°)
G(r'cos126°,r'sin126°)
H(r'cos198°,r'sin198°)
I(r'cos270°,r'sin272°)
J(r'cos342°,r'sin342°)
二、动态绘制五角星
namespace Ky_CvRoi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string SourceFname = @"d:\2022视觉文件夹\安柏.JPG";
public Mat S0 = Cv2.ImRead(@"d:\2022视觉文件夹\安柏.JPG", ImreadModes.Unchanged);
public OpenCvSharp.Point p1 = new OpenCvSharp.Point(-1, -1);
public OpenCvSharp.Point p2 = new OpenCvSharp.Point(-1, -1);
bool mouseDown = false;
#region 定义一个发送接收图像数据的全局变量SendPicData
public Mat SendPicData
{
get { return BitmapConverter.ToMat((Bitmap)pictureBox1.Image); }
set { pictureBox1.Image = value.ToBitmap(); }
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = S0.ToBitmap();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
if (mouseDown)
{
p1.X = e.X;
p1.Y = e.Y;
textBox1.Text = textBox1.Text + "p1=(" + e.X.ToString() + " , " + e.Y.ToString() + ")\r\n";
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
OpenCvSharp.Point p0;
mouseDown = false;
int w1=0;
int h1=0;
if ((p1.X >= 0) && (p1.Y >= 0))
{
p2.X = e.X;
p2.Y = e.Y;
textBox1.Text = textBox1.Text + "p2=(" + e.X.ToString() + " , " + e.Y.ToString() + ")\r\n";
w1 = p2.X - p1.X;
h1 = p2.Y - p1.Y;
if (p2.X < p1.X)
{
w1 = p1.X - p2.X;
};
if (p2.Y < p1.Y)
{
h1 = p1.Y - p2.Y;
}
}
OpenCvSharp.Scalar scalar = new OpenCvSharp.Scalar(0, 200, 200);
// 画矩形
//S0.Rectangle(p1, p2, scalar,2);
// 画圆
p0.X = (p1.X + p2.X) / 2;
p0.Y = (p1.Y + p2.Y) / 2;
int r0 = 0;
r0 = (int) (w1/2);
S0.Circle(p0, r0, scalar,2);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(S0);
// 画五角星
p0.X = (p1.X + p2.X) / 2;
p0.Y = (p1.Y + p2.Y) / 2;
int r;
r0=(int)(w1 / 2);
int r1 =(int)( r0 * Math.Sin(Math.PI / 10) / Math.Sin(Math.PI * 7 / 10));
List<OpenCvSharp.Point> pts1 = new List<OpenCvSharp.Point>
{
new OpenCvSharp.Point((int)p0.X+r0*Math.Cos(Math.PI/10),(int)p0.Y-r0*Math.Sin(Math.PI/10)),
new OpenCvSharp.Point((int)p0.X+r1*Math.Cos(Math.PI*3/10),(int) p0.Y-r1*Math.Sin(Math.PI*3/10)),
new OpenCvSharp.Point((int)p0.X+r0*Math.Cos(Math.PI*5/10),(int) p0.Y-r0*Math.Sin(Math.PI*5/10)),
new OpenCvSharp.Point((int)p0.X+r1*Math.Cos(Math.PI*7/10),(int) p0.Y-r1*Math.Sin(Math.PI*7/10)),
new OpenCvSharp.Point((int)p0.X+r0*Math.Cos(Math.PI*9/10),(int) p0.Y-r0*Math.Sin(Math.PI*9/10)),
new OpenCvSharp.Point((int)p0.X+r1*Math.Cos(Math.PI*11/10),(int) p0.Y-r1*Math.Sin(Math.PI*11/10)),
new OpenCvSharp.Point((int)p0.X+r0*Math.Cos(Math.PI*13/10),(int) p0.Y-r0*Math.Sin(Math.PI*13/10)),
new OpenCvSharp.Point((int)p0.X+r1*Math.Cos(Math.PI*15/10), (int)p0.Y-r1*Math.Sin(Math.PI*15/10)),
new OpenCvSharp.Point((int)p0.X+r0*Math.Cos(Math.PI*17/10),(int) p0.Y-r0*Math.Sin(Math.PI*17/10)),
new OpenCvSharp.Point((int)p0.X+r1*Math.Cos(Math.PI*19/10), (int)p0.Y-r1*Math.Sin(Math.PI*19/10))
};
List<List<OpenCvSharp.Point>> pts = new List<List<OpenCvSharp.Point>>() { pts1 };
//画五角星
Cv2.Polylines(S0, pts, true, new Scalar(0, 0, 255), 2);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(S0);
}
private void button2_Click(object sender, EventArgs e)
{
string path = @"d:\2022视觉文件夹\安柏.JPG";
pictureBox1.Image = new Bitmap(path);
S0 = new Mat(path);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(S0);
}
}
}