基于WinForm+Halcon實(shí)現(xiàn)圖像縮放與交互功能
前言
本文將通過具體實(shí)例,詳細(xì)介紹如何在 WinForm +Halcon 中實(shí)現(xiàn)圖像的縮放、平移以及實(shí)時(shí)顯示灰度值等交互功能,幫助大家快速掌握這一實(shí)用技能,提升圖像處理應(yīng)用的開發(fā)效率和用戶體驗(yàn)。
初始化窗口
1、圖片控件為winform中的PictureBox控件時(shí):
需要調(diào)用halcon算子OpenWindow來初始化窗口,使Winform中的圖片窗口轉(zhuǎn)換為適用于halcon的圖片窗口。
2、圖片控件為halcon中的HWindowControl控件時(shí):
無需進(jìn)行窗口轉(zhuǎn)換,可直接按照如下方式調(diào)用。
WindowID = hWindowControl1.HalconWindow。
添加圖像縮放功能
打開Form窗體——查看圖片控件屬性——點(diǎn)擊"事件"選項(xiàng)——找到鼠標(biāo)滾輪滑動(dòng)的事件,雙擊創(chuàng)建響應(yīng)函數(shù)——將相應(yīng)的代碼放在剛剛添加的函數(shù)中,如下圖所示:
添加圖像平移功能
按照上述步驟分別找到鼠標(biāo)"按下"與"抬起"的事件,分別雙擊創(chuàng)建響應(yīng)函數(shù),然后將相應(yīng)的代碼放在剛剛添加的函數(shù)中。
如下圖所示:
添加實(shí)時(shí)顯示灰度值功能
按照上述步驟找到鼠標(biāo)移動(dòng)的事件,雙擊創(chuàng)建響應(yīng)函數(shù)——在界面上添加一個(gè)"label"控件,然后將相應(yīng)的代碼放在剛剛添加的函數(shù)中。
示例代碼
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; //鼠標(biāo)按下時(shí)的行坐標(biāo) private double ColDown; //鼠標(biāo)按下時(shí)的列坐標(biāo) HObject ho_image; //圖像變量 public Form1() { InitializeComponent(); CreateHalconWindow(); } //創(chuàng)建Halcon窗口 public void CreateHalconWindow() { /////圖片控件為winform中的PictureBox控件時(shí)/ //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控件時(shí)/ 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。如果無限縮小原圖像,導(dǎo)致顯示的圖像超出限制,則會(huì)造成程序崩潰 { 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); } } //鼠標(biāo)按下,記錄當(dāng)前坐標(biāo)值 private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e) { HTuple Row, Column, Button; HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button); RowDown = Row; //鼠標(biāo)按下時(shí)的行坐標(biāo) ColDown = Column; //鼠標(biāo)按下時(shí)的列坐標(biāo) } //鼠標(biāo)抬起,實(shí)現(xiàn)圖像移動(dòng) 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; //鼠標(biāo)彈起時(shí)的行坐標(biāo)減去按下時(shí)的行坐標(biāo),得到行坐標(biāo)的移動(dòng)值 double ColMove = Column - ColDown; //鼠標(biāo)彈起時(shí)的列坐標(biāo)減去按下時(shí)的列坐標(biāo),得到列坐標(biāo)的移動(dòng)值 HOperatorSet.GetPart(WindowID, out row1, out col1, out row2, out col2); //得到當(dāng)前的窗口坐標(biāo) HOperatorSet.SetPart(WindowID, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove); //這里可能有些不好理解。以左上角原點(diǎn)為參考點(diǎn) HOperatorSet.ClearWindow(WindowID); if (ImageHeight != null) { HOperatorSet.DispObj(ho_image, WindowID); } else { MessageBox.Show("請(qǐng)加載一張圖片"); } } //鼠標(biāo)移動(dòng),實(shí)時(shí)顯示當(dāng)前坐標(biāo)與灰度值 private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e) { HTuple Row, Column, Button, pointGray; HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button); //獲取當(dāng)前鼠標(biāo)的坐標(biāo)值 if (ImageHeight != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth)) //設(shè)置3個(gè)條件項(xiàng),防止程序崩潰。 { HOperatorSet.GetGrayval(ho_image, Row, Column, out pointGray); //獲取當(dāng)前點(diǎn)的灰度值 } else { pointGray = "_"; } String str = String.Format("Row:{0} Column:{1} Gray:{2}", Row, Column, pointGray); //格式化字符串 label1.Text = str; //在label控件上顯示數(shù)值 } //全屏顯示圖像,使縮放后的圖像回到原始大小 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); } } }
總結(jié)
本文介紹了如何在 Winform 應(yīng)用程序中使用 Halcon 實(shí)現(xiàn)圖像縮放等功能。
通過初始化 Halcon 窗口,添加圖像縮放、平移和實(shí)時(shí)顯示灰度值功能,我們可以在 Winform 界面中高效地處理圖像。
具體步驟包括:
1、初始化窗口:根據(jù)使用的控件類型(PictureBox 或 HWindowControl),選擇合適的初始化方法。
2、添加圖像縮放功能:通過鼠標(biāo)滾輪事件,調(diào)用 HOperatorSet.SetPart 方法實(shí)現(xiàn)圖像的縮放。
3、添加圖像平移功能:捕獲鼠標(biāo)按下和抬起事件,計(jì)算坐標(biāo)差值,調(diào)用 HOperatorSet.SetPart 方法實(shí)現(xiàn)圖像平移。
4、實(shí)時(shí)顯示灰度值:在鼠標(biāo)移動(dòng)事件中,獲取當(dāng)前像素點(diǎn)的灰度值并顯示在界面上。
功能的實(shí)現(xiàn)不僅提升了用戶與圖像的交互體驗(yàn),還為圖像處理應(yīng)用提供了強(qiáng)大的支持。
通過這些步驟,我們可以在 Winform 應(yīng)用中輕松集成 Halcon,實(shí)現(xiàn)復(fù)雜的圖像處理功能。
最后
到此這篇關(guān)于基于WinForm+Halcon實(shí)現(xiàn)圖像縮放與交互功能的文章就介紹到這了,更多相關(guān)WinForm Halcon圖像縮放與交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#事務(wù)處理(Execute Transaction)實(shí)例解析
這篇文章主要介紹了C#事務(wù)處理(Execute Transaction)實(shí)例解析,對(duì)于理解和學(xué)習(xí)事務(wù)處理有一定的幫助,需要的朋友可以參考下2014-08-08C# 常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例(SuperSocket入門)
本文主要介紹了常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01C# Dynamic之:ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用(上)
本篇文章對(duì)C#中ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05