C#/halcon影象滾輪縮放
阿新 • • 發佈:2021-08-23
分類專欄: # Halcon
版權宣告:本文為博主原創文章,遵循CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本宣告。 本文連結:https://blog.csdn.net/jgj123321/article/details/96479014 版權 Halcon 專欄收錄該內容 39 篇文章 3 訂閱 訂閱專欄-
初始化視窗
1)圖片控制元件為winform中的PictureBox控制元件時:
需要呼叫halcon運算元OpenWindow來初始化視窗,使winform中的圖片視窗轉換為適用於halcon的圖片視窗。
2)圖片控制元件為halcon中的HWindowControl 控制元件時:
無需進行視窗轉換,可直接按照如下方式呼叫。
WindowID = hWindowControl1.HalconWindow。
-
新增影象縮放功能
開啟Form窗體——檢視圖片控制元件屬性——點選“事件”選項——找到滑鼠滾輪滑動的事件,雙擊建立響應函式——將相應的程式碼放在剛剛新增的函式中,如下圖所示:
-
新增影象平移功能
按照上述步驟分別找到滑鼠“按下”與“抬起”的事件,分別雙擊建立響應函式,然後將相應的程式碼放在剛剛新增的函式中。如下圖所示:
-
新增實時顯示灰度值功能
按照上述步驟找到滑鼠移動的事件,雙擊建立響應函式——在介面上新增一個“label”控制元件,然後將相應的程式碼放在剛剛新增的函式中。
-
程式碼:
自動驅動未來using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using HalconDotNet; namespace CSharpAndHalcon12 { public partial class Form1 : Form { HTuple WindowID, ImageWidth, ImageHeight; private double RowDown;//滑鼠按下時的行座標 private double ColDown;//滑鼠按下時的列座標 HObject ho_image; //影象變數 public Form1() { InitializeComponent(); CreateHalconWindow(); } //建立Halcon視窗 public void CreateHalconWindow() { // ///圖片控制元件為winform中的PictureBox控制元件時/ //HTuple FatherWindow = this.hWindowControl1.Handle; //HOperatorSet.SetWindowAttr("background_color", "green"); //HOperatorSet.OpenWindow(0, 0, this.hWindowControl1.Width, this.hWindowControl1.Height, FatherWindow, "visible", "", out WindowID); /圖片控制元件為halcon中的HWindowControl控制元件時/ WindowID = hWindowControl1.HalconWindow; } //讀圖 private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.Filter = "JPEG檔案|*.jpg*|BMP檔案|*.bmp*|TIFF檔案|*.tiff*"; openFileDialog.Filter = "所有影象檔案 | *.bmp; *.pcx; *.png; *.jpg; *.gif;*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf"; if (openFileDialog.ShowDialog() == DialogResult.OK) { HTuple ImagePath = openFileDialog.FileName; HOperatorSet.ReadImage(out ho_image, ImagePath); } HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight); HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth); HOperatorSet.DispObj(ho_image, WindowID); } //縮放影象 private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e) { HTuple Zoom, Row, Col, Button; HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2; if (e.Delta > 0) { Zoom = 1.5; } else { Zoom = 0.5; } HOperatorSet.GetMposition(WindowID, out Row, out Col, out Button); HOperatorSet.GetPart(WindowID, out Row0, out Column0, out Row00, out Column00); Ht = Row00 - Row0; Wt = Column00 - Column0; if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能處理的影象最大尺寸是32K*32K。如果無限縮小原影象,導致顯示的影象超出限制,則會造成程式崩潰 { r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0))); c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0))); r2 = r1 + (Ht / Zoom); c2 = c1 + (Wt / Zoom); HOperatorSet.SetPart(WindowID, r1, c1, r2, c2); HOperatorSet.ClearWindow(WindowID); HOperatorSet.DispObj(ho_image, WindowID); } } //滑鼠按下,記錄當前座標值 private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e) { HTuple Row, Column, Button; HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button); RowDown = Row; //滑鼠按下時的行座標 ColDown = Column; //滑鼠按下時的列座標 } //滑鼠抬起,實現影象移動 private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e) { HTuple row1, col1, row2, col2,Row, Column, Button; HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button); double RowMove = Row - RowDown; //滑鼠彈起時的行座標減去按下時的行座標,得到行座標的移動值 double ColMove = Column - ColDown;//滑鼠彈起時的列座標減去按下時的列座標,得到列座標的移動值 HOperatorSet.GetPart(WindowID, out row1, out col1, out row2, out col2);//得到當前的視窗座標 HOperatorSet.SetPart(WindowID, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//這裡可能有些不好理解。以左上角原點為參考點 HOperatorSet.ClearWindow(WindowID); if (ImageHeight != null) { HOperatorSet.DispObj(ho_image, WindowID); } else { MessageBox.Show("請載入一張圖片"); } } //滑鼠移動,實時顯示當前座標與灰度值 private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e) { HTuple Row, Column, Button, pointGray; HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button); //獲取當前滑鼠的座標值 if (ImageHeight != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//設定3個條件項,防止程式崩潰。 { HOperatorSet.GetGrayval(ho_image, Row, Column, out pointGray); //獲取當前點的灰度值 } else { pointGray = "_"; } String str = String.Format("Row:{0} Column:{1} Gray:{2}", Row, Column, pointGray); //格式化字串 label1.Text = str; //在label控制元件上顯示數值 } //全屏顯示影象,使縮放後的影象回到原始大小 private void button_FullWindow_Click(object sender, EventArgs e) { HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight - 1, ImageWidth - 1); HOperatorSet.ClearWindow(WindowID); HOperatorSet.DispObj(ho_image, WindowID); } } }