C#:Form窗体绘制ROI实例

《学习OpenCV(中文版)》

作者:(美)布拉德斯基(Bradski,G.)
(美)克勒(Kaehler,A.) 著
出版社:清华大学出版社
出版时间:2009年10月

参考资料:https://blog.csdn.net/F_soar/article/details/123357849
https://blog.csdn.net/zanllp/article/details/79830571

一、感兴趣区域(ROI)

  在图像处理过程中,我们可能会对图像的某一个特定区域感兴趣,该区域被称为感兴趣区域(Region of Interest, ROI)。在设定感兴趣区域ROI后,就可以对该区域进行整体操作。

  • 打码
  • 美颜
  • 置换
    将一个感兴趣区域A赋值给变量B后,可以将该变量B赋值给另外一个区域C,从而达到在区域C内复制区域A的目的。

二、Mat(Mat m, Rect roi);

复制一个图像的ROI到另外一个图像

三、Form窗体绘制ROI实例

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);
        public OpenCvSharp.Rect rect0=new OpenCvSharp.Rect(0,0,0,0);
        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;
                    }
                }
            rect0 = new OpenCvSharp.Rect(p1.X, p1.Y, w1, h1);
                OpenCvSharp.Scalar scalar = new OpenCvSharp.Scalar(0, 200, 200);
            // 画矩形
            S0.Rectangle(p1, p2, scalar,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);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenCvSharp.Rect roi = rect0;
            Mat ImageROI = new Mat(S0, roi);//新建一个mat,把roi内的图像加载到里面去
            pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(ImageROI);
        }
    }
}

四、运行结果

版权声明:
作者:ht
链接:https://www.techfm.club/p/43714.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>