基于WinForm+Halcon實現(xiàn)圖像縮放與交互功能
前言
本文將通過具體實例,詳細介紹如何在 WinForm +Halcon 中實現(xiàn)圖像的縮放、平移以及實時顯示灰度值等交互功能,幫助大家快速掌握這一實用技能,提升圖像處理應(yīng)用的開發(fā)效率和用戶體驗。
初始化窗口
1、圖片控件為winform中的PictureBox控件時:
需要調(diào)用halcon算子OpenWindow來初始化窗口,使Winform中的圖片窗口轉(zhuǎn)換為適用于halcon的圖片窗口。
2、圖片控件為halcon中的HWindowControl控件時:
無需進行窗口轉(zhuǎn)換,可直接按照如下方式調(diào)用。
WindowID = hWindowControl1.HalconWindow。
添加圖像縮放功能
打開Form窗體——查看圖片控件屬性——點擊"事件"選項——找到鼠標滾輪滑動的事件,雙擊創(chuàng)建響應(yīng)函數(shù)——將相應(yīng)的代碼放在剛剛添加的函數(shù)中,如下圖所示:

添加圖像平移功能
按照上述步驟分別找到鼠標"按下"與"抬起"的事件,分別雙擊創(chuàng)建響應(yīng)函數(shù),然后將相應(yīng)的代碼放在剛剛添加的函數(shù)中。
如下圖所示:

添加實時顯示灰度值功能
按照上述步驟找到鼠標移動的事件,雙擊創(chuàng)建響應(yīng)函數(shù)——在界面上添加一個"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;
//鼠標按下時的行坐標
private double ColDown;
//鼠標按下時的列坐標
HObject ho_image;
//圖像變量
public Form1()
{
InitializeComponent();
CreateHalconWindow();
}
//創(chuàng)建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。如果無限縮小原圖像,導(dǎo)致顯示的圖像超出限制,則會造成程序崩潰
{
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;
//鼠標按下時的列坐標
}
//鼠標抬起,實現(xiàn)圖像移動
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))
//設(shè)置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控件上顯示數(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 實現(xiàn)圖像縮放等功能。
通過初始化 Halcon 窗口,添加圖像縮放、平移和實時顯示灰度值功能,我們可以在 Winform 界面中高效地處理圖像。
具體步驟包括:
1、初始化窗口:根據(jù)使用的控件類型(PictureBox 或 HWindowControl),選擇合適的初始化方法。
2、添加圖像縮放功能:通過鼠標滾輪事件,調(diào)用 HOperatorSet.SetPart 方法實現(xiàn)圖像的縮放。
3、添加圖像平移功能:捕獲鼠標按下和抬起事件,計算坐標差值,調(diào)用 HOperatorSet.SetPart 方法實現(xiàn)圖像平移。
4、實時顯示灰度值:在鼠標移動事件中,獲取當前像素點的灰度值并顯示在界面上。
功能的實現(xiàn)不僅提升了用戶與圖像的交互體驗,還為圖像處理應(yīng)用提供了強大的支持。
通過這些步驟,我們可以在 Winform 應(yīng)用中輕松集成 Halcon,實現(xiàn)復(fù)雜的圖像處理功能。
最后
到此這篇關(guān)于基于WinForm+Halcon實現(xiàn)圖像縮放與交互功能的文章就介紹到這了,更多相關(guān)WinForm Halcon圖像縮放與交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#事務(wù)處理(Execute Transaction)實例解析
這篇文章主要介紹了C#事務(wù)處理(Execute Transaction)實例解析,對于理解和學(xué)習(xí)事務(wù)處理有一定的幫助,需要的朋友可以參考下2014-08-08
C# 常用協(xié)議實現(xiàn)模版及FixedSizeReceiveFilter示例(SuperSocket入門)
本文主要介紹了常用協(xié)議實現(xiàn)模版及FixedSizeReceiveFilter示例。具有很好的參考價值,下面跟著小編一起來看下吧2017-01-01
C# Dynamic之:ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用(上)
本篇文章對C#中ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05

