1. 程式人生 > >C#中利用emgucv的ImageBox()打開並顯示一副圖像

C#中利用emgucv的ImageBox()打開並顯示一副圖像

窗口 void 生成 from oid line 圖片 display hand

1、添加一個工具箱中的PictureBox技術分享到界面上。

技術分享

2、在XXXXX.Designer.cs的Windows窗口設計器生成的代碼段中找到剛剛添加的PictureBox控件的定義代碼:

技術分享

修改為:

技術分享

這樣就可以在主體程序中通過技術分享代碼顯示圖像(Image、Mat)到這個控件上面。

3、添加一個文本框控件textBox用來顯示圖片路徑,在屬性中改成“只讀”。

4、添加一個打開文件的控件openFileDialog。

5、添加一個用於打開圖像的按鈕,在對於調用函數中添加下面代碼:

            DialogResult result = openFileDialog1.ShowDialog();
            
if (result == DialogResult.OK || result == DialogResult.Yes) { textBox1.Text = openFileDialog1.FileName; }

上面代碼中openFileDialog1和textBox1根據實際修改。

6、添加讀入並顯示圖像的代碼

        public void PerformShapeDetection()
        {
            if (textBox1.Text != String.Empty)//
判斷文本框中地址是否為空 { StringBuilder msgBuilder = new StringBuilder("Performance: "); //Load the image from file and resize it for display Image < Bgr, byte > img = new Image<Bgr, byte>(textBox1.Text) .Resize(
814, 539, Emgu.CV.CvEnum.Inter.Linear, true);//圖像的大小由實際的控件大小決定 //Mat srcImg = CvInvoke.Imread(textBox1.Text); pictureBox1.Image = img;//顯示圖像到控件 this.Text = msgBuilder.ToString();

7、添加文本框控件中地址改變時,顯示圖像代碼的調用代碼。

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            PerformShapeDetection();
        }
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);//在XXXXX.Designer.cs的Windows窗口設計器生成的代碼段中

技術分享

C#中利用emgucv的ImageBox()打開並顯示一副圖像