C# Emgucv绘制旋转矩形
时间: 2023-11-22 18:44:42 浏览: 47
好的,以下是C# Emgucv绘制旋转矩形的代码:
```csharp
using Emgu.CV;
using Emgu.CV.Structure;
using System.Drawing;
// 定义旋转矩形的中心点、宽度、高度和旋转角度
PointF center = new PointF(100, 100);
float width = 200;
float height = 100;
float angle = 45;
// 创建一个空白的图像
Image<Bgr, byte> img = new Image<Bgr, byte>(500, 500, new Bgr(255, 255, 255));
// 计算旋转矩形的四个顶点
PointF[] vertices = new PointF[4];
vertices[0] = new PointF(center.X - width / 2, center.Y - height / 2);
vertices[1] = new PointF(center.X + width / 2, center.Y - height / 2);
vertices[2] = new PointF(center.X + width / 2, center.Y + height / 2);
vertices[3] = new PointF(center.X - width / 2, center.Y + height / 2);
// 创建旋转矩阵
Matrix<float> rotationMatrix = new Matrix<float>(new float[,] {
{ (float)Math.Cos(angle * Math.PI / 180), (float)Math.Sin(angle * Math.PI / 180), 0 },
{ -(float)Math.Sin(angle * Math.PI / 180), (float)Math.Cos(angle * Math.PI / 180), 0 },
{ 0, 0, 1 }
});
// 将旋转矩阵应用于顶点
rotationMatrix.TransformPoints(vertices);
// 绘制旋转矩形
img.DrawPolyline(vertices, true, new Bgr(0, 0, 255), 2);
// 显示图像
CvInvoke.Imshow("Rotated Rectangle", img);
CvInvoke.WaitKey(0);
```
这段代码首先定义了旋转矩形的中心点、宽度、高度和旋转角度,然后创建了一个空白的图像。接着计算旋转矩形的四个顶点,并创建旋转矩阵。最后将旋转矩阵应用于顶点,绘制旋转矩形,并显示图像。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)