c#截取旋转矩形区域内的图像

c#截取旋转矩形区域内的图像

先来看看最终效果。
在这里插入图片描述
在这里插入图片描述
因为实际项目中要用到opencvsharp,所以这里面的旋转矩形就直接用opencvsharp里面封装好了的RotatedRect。

        private Size2f cropSize = new Size2f(400, 300);
        private RotatedRect cropRect = new RotatedRect();
        private PictureBox cropRectBox;
        private SolidBrush brush;

        public Form1()
        {
   
            InitializeComponent();
            brush = new SolidBrush(Color.FromArgb(50, 255, 0, 0));
            cropRectBox = new PictureBox()
            {
   
                Visible = false,
                BackColor = Color.Transparent,
                SizeMode = PictureBoxSizeMode.Normal,
                BorderStyle = BorderStyle.None,
                Parent = rawImgPb
            };
            cropRectBox.MouseDown += CropRectBox_MouseDown;
            rawImgPb.Image = Image.FromFile("C:\\Users\\Administrator\\Desktop\\测试\\1.jpg");
        }

        private void CropRectBox_MouseDown(object sender, MouseEventArgs e)
        {
   
            if (e.Button != MouseButtons.Right)
                return;
            cropRect.Angle += 10;
            ShowCropRectBox(cropRect);
        }

        private void rawImgPb_MouseDown(object sender, MouseEventArgs e)
        {
   
            cropRect.Center = new Point2f(e.X + cropSize.Width / 2, e.Y + cropSize.Height / 2);
            cropRect.Size = cropSize;
            cropRect.Angle = 0;
            ShowCropRectBox(cropRect)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是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); ``` 这段代码首先定义了旋转矩形的中心点、宽度、高度和旋转角度,然后创建了一个空白的图像。接着计算旋转矩形的四个顶点,并创建旋转矩阵。最后将旋转矩阵应用于顶点,绘制旋转矩形,并显示图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值