实例介绍
【实例简介】本实例展示了halcon与C#联合编程之鼠标控制图片缩放,拖动,roi
【实例截图】
【核心代码】using ChoiceTech.Halcon.Control;
using HalconDotNet;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace HalconPaint
{
public partial class PaintForm : Form
{
private HObject image = new HObject();//图片
private HObject brush_region = new HObject();//笔刷
private HObject final_region = new HObject();//需要获得的区域
private HTuple hv_WindowHandle;
public PaintForm()
{
InitializeComponent();
//hv_WindowHandle = hWindow_Final1.HWindowHalconID;
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VisionForm_Load(object sender, EventArgs e)
{
try
{
HOperatorSet.ReadImage(out image, "kk.bmp");
hWindow_Final1.HobjectToHimage(image);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 设置笔刷
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_brush_Click(object sender, EventArgs e)
{
string brushType = ((Button)sender).Text;//笔刷类型
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
HObject ho_temp_brush = new HObject();
try
{
//画图模式 开
hWindow_Final1.DrawModel = true;
hWindow_Final1.Focus();
//锁住功能区
groupBox_tool.Enabled = false;
//显示提示
hWindow_Final1.ClearWindow();
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "在窗口画出" brushType ",点击右键结束", "window", 20, 20, "red", "false");
//显示为黄色
HOperatorSet.SetColor(hv_WindowHandle, "yellow");
//
switch (brushType)
{
case "矩形1":
HOperatorSet.DrawRectangle1(hv_WindowHandle, out hv_Row1, out hv_Column1, out hv_Row2,
out hv_Column2);
ho_temp_brush.Dispose();
HOperatorSet.GenRectangle1(out ho_temp_brush, hv_Row1, hv_Column1, hv_Row2,
hv_Column2);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
case "矩形2":
HTuple phi, lenght1, length2;
HOperatorSet.DrawRectangle2(hv_WindowHandle, out hv_Row1, out hv_Column1, out phi, out lenght1, out length2);
ho_temp_brush.Dispose();
HOperatorSet.GenRectangle2(out ho_temp_brush, hv_Row1, hv_Column1, phi, lenght1, length2);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
case "圆":
HTuple radius;
HOperatorSet.DrawCircle(hv_WindowHandle, out hv_Row1, out hv_Column1, out radius);
ho_temp_brush.Dispose();
HOperatorSet.GenCircle(out ho_temp_brush, hv_Row1, hv_Column1, radius);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
default:
MessageBox.Show("错误指令");
return;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, brushType " 笔刷创建成果", "window", 20, 20, "red", "false");
hWindow_Final1.DispObj(ho_temp_brush, "yellow");
hWindow_Final1.DrawModel = false;
//解锁功能区
groupBox_tool.Enabled = true;
}
}
/// <summary>
/// 擦除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_drawAndwipe_Click(object sender, EventArgs e)
{
string actionType = ((Button)sender).Name;// 画区域 或者 擦除
hWindow_Final1.DrawModel = true;
hWindow_Final1.Focus();
groupBox_tool.Enabled = false;
HTuple hv_Button = null;
HTuple hv_Row = null, hv_Column = null;
HTuple areaBrush, rowBrush, columnBrush, homMat2D;
HObject brush_region_affine = new HObject();
HObject ho_Image = new HObject(image);
try
{
if (!brush_region.IsInitialized())
{
MessageBox.Show("请先设置笔刷");
return;
}
else
{
HOperatorSet.AreaCenter(brush_region, out areaBrush, out rowBrush, out columnBrush);
}
//显示
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region);
//画出笔刷
switch (actionType)
{
case "button_draw":
HOperatorSet.SetColor(hv_WindowHandle, "blue");
break;
case "button_wipe":
HOperatorSet.SetColor(hv_WindowHandle, "red");
//检查final_region是否有效
if (!final_region.IsInitialized())
{
MessageBox.Show("请先使用画出合适区域,在使用擦除功能");
return;
}
break;
default:
MessageBox.Show("设置错误");
return;
}
#region "循环,等待涂抹"
//鼠标状态
hv_Button = 0;
// 4为鼠标右键
while (hv_Button != 4)
{
//一直在循环,需要让halcon控件也响应事件,不然到时候跳出循环,之前的事件会一起爆发触发,
Application.DoEvents();
hv_Row = -1;
hv_Column = -1;
//获取鼠标坐标
try
{
HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button);
}
catch (HalconException ex)
{
hv_Button = 0;
}
HOperatorSet.SetSystem("flush_graphic", "false");
HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
if (final_region.IsInitialized())
{
HOperatorSet.DispObj(final_region, hv_WindowHandle);
}
//check if mouse cursor is over window
if (hv_Row >= 0 && hv_Column >= 0)
{
//放射变换
HOperatorSet.VectorAngleToRigid(rowBrush, columnBrush, 0, hv_Row, hv_Column, 0, out homMat2D);
brush_region_affine.Dispose();
HOperatorSet.AffineTransRegion(brush_region, out brush_region_affine, homMat2D, "nearest_neighbor");
HOperatorSet.DispObj(brush_region_affine, hv_WindowHandle);
HOperatorSet.SetSystem("flush_graphic", "true");
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "按下鼠标左键涂画,右键结束", "window", 20, 20, "red", "false");
//1为鼠标左键
if (hv_Button == 1)
{
//画出笔刷
switch (actionType)
{
case "button_draw":
{
if (final_region.IsInitialized())
{
HObject ExpTmpOutVar_0;
HOperatorSet.Union2(final_region, brush_region_affine, out ExpTmpOutVar_0);
final_region.Dispose();
final_region = ExpTmpOutVar_0;
}
else
{
final_region =new HObject( brush_region_affine);
}
}
break;
case "button_wipe":
{
HObject ExpTmpOutVar_0;
HOperatorSet.Difference(final_region, brush_region_affine, out ExpTmpOutVar_0);
final_region.Dispose();
final_region = ExpTmpOutVar_0;
}
break;
default:
MessageBox.Show("设置错误");
return;
}//end switch
}//end if
}
else
{
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "请将鼠标移动到窗口内部", "window", 20, 20, "red", "false");
}
}//end while
#endregion
}
catch (HalconException HDevExpDefaultException)
{
throw HDevExpDefaultException;
}
finally
{
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region, "blue");
hWindow_Final1.DrawModel = false;
groupBox_tool.Enabled = true;
}
}
/// <summary>
/// 显示结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_show_finalRegion_Click(object sender, EventArgs e)
{
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region,"blue");
}
}
}
【实例截图】
【核心代码】using ChoiceTech.Halcon.Control;
using HalconDotNet;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace HalconPaint
{
public partial class PaintForm : Form
{
private HObject image = new HObject();//图片
private HObject brush_region = new HObject();//笔刷
private HObject final_region = new HObject();//需要获得的区域
private HTuple hv_WindowHandle;
public PaintForm()
{
InitializeComponent();
//hv_WindowHandle = hWindow_Final1.HWindowHalconID;
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VisionForm_Load(object sender, EventArgs e)
{
try
{
HOperatorSet.ReadImage(out image, "kk.bmp");
hWindow_Final1.HobjectToHimage(image);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 设置笔刷
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_brush_Click(object sender, EventArgs e)
{
string brushType = ((Button)sender).Text;//笔刷类型
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
HObject ho_temp_brush = new HObject();
try
{
//画图模式 开
hWindow_Final1.DrawModel = true;
hWindow_Final1.Focus();
//锁住功能区
groupBox_tool.Enabled = false;
//显示提示
hWindow_Final1.ClearWindow();
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "在窗口画出" brushType ",点击右键结束", "window", 20, 20, "red", "false");
//显示为黄色
HOperatorSet.SetColor(hv_WindowHandle, "yellow");
//
switch (brushType)
{
case "矩形1":
HOperatorSet.DrawRectangle1(hv_WindowHandle, out hv_Row1, out hv_Column1, out hv_Row2,
out hv_Column2);
ho_temp_brush.Dispose();
HOperatorSet.GenRectangle1(out ho_temp_brush, hv_Row1, hv_Column1, hv_Row2,
hv_Column2);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
case "矩形2":
HTuple phi, lenght1, length2;
HOperatorSet.DrawRectangle2(hv_WindowHandle, out hv_Row1, out hv_Column1, out phi, out lenght1, out length2);
ho_temp_brush.Dispose();
HOperatorSet.GenRectangle2(out ho_temp_brush, hv_Row1, hv_Column1, phi, lenght1, length2);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
case "圆":
HTuple radius;
HOperatorSet.DrawCircle(hv_WindowHandle, out hv_Row1, out hv_Column1, out radius);
ho_temp_brush.Dispose();
HOperatorSet.GenCircle(out ho_temp_brush, hv_Row1, hv_Column1, radius);
//
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_temp_brush;
}
else
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
return;
}
break;
default:
MessageBox.Show("错误指令");
return;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
hWindow_Final1.HobjectToHimage(image);
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, brushType " 笔刷创建成果", "window", 20, 20, "red", "false");
hWindow_Final1.DispObj(ho_temp_brush, "yellow");
hWindow_Final1.DrawModel = false;
//解锁功能区
groupBox_tool.Enabled = true;
}
}
/// <summary>
/// 擦除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_drawAndwipe_Click(object sender, EventArgs e)
{
string actionType = ((Button)sender).Name;// 画区域 或者 擦除
hWindow_Final1.DrawModel = true;
hWindow_Final1.Focus();
groupBox_tool.Enabled = false;
HTuple hv_Button = null;
HTuple hv_Row = null, hv_Column = null;
HTuple areaBrush, rowBrush, columnBrush, homMat2D;
HObject brush_region_affine = new HObject();
HObject ho_Image = new HObject(image);
try
{
if (!brush_region.IsInitialized())
{
MessageBox.Show("请先设置笔刷");
return;
}
else
{
HOperatorSet.AreaCenter(brush_region, out areaBrush, out rowBrush, out columnBrush);
}
//显示
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region);
//画出笔刷
switch (actionType)
{
case "button_draw":
HOperatorSet.SetColor(hv_WindowHandle, "blue");
break;
case "button_wipe":
HOperatorSet.SetColor(hv_WindowHandle, "red");
//检查final_region是否有效
if (!final_region.IsInitialized())
{
MessageBox.Show("请先使用画出合适区域,在使用擦除功能");
return;
}
break;
default:
MessageBox.Show("设置错误");
return;
}
#region "循环,等待涂抹"
//鼠标状态
hv_Button = 0;
// 4为鼠标右键
while (hv_Button != 4)
{
//一直在循环,需要让halcon控件也响应事件,不然到时候跳出循环,之前的事件会一起爆发触发,
Application.DoEvents();
hv_Row = -1;
hv_Column = -1;
//获取鼠标坐标
try
{
HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button);
}
catch (HalconException ex)
{
hv_Button = 0;
}
HOperatorSet.SetSystem("flush_graphic", "false");
HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
if (final_region.IsInitialized())
{
HOperatorSet.DispObj(final_region, hv_WindowHandle);
}
//check if mouse cursor is over window
if (hv_Row >= 0 && hv_Column >= 0)
{
//放射变换
HOperatorSet.VectorAngleToRigid(rowBrush, columnBrush, 0, hv_Row, hv_Column, 0, out homMat2D);
brush_region_affine.Dispose();
HOperatorSet.AffineTransRegion(brush_region, out brush_region_affine, homMat2D, "nearest_neighbor");
HOperatorSet.DispObj(brush_region_affine, hv_WindowHandle);
HOperatorSet.SetSystem("flush_graphic", "true");
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "按下鼠标左键涂画,右键结束", "window", 20, 20, "red", "false");
//1为鼠标左键
if (hv_Button == 1)
{
//画出笔刷
switch (actionType)
{
case "button_draw":
{
if (final_region.IsInitialized())
{
HObject ExpTmpOutVar_0;
HOperatorSet.Union2(final_region, brush_region_affine, out ExpTmpOutVar_0);
final_region.Dispose();
final_region = ExpTmpOutVar_0;
}
else
{
final_region =new HObject( brush_region_affine);
}
}
break;
case "button_wipe":
{
HObject ExpTmpOutVar_0;
HOperatorSet.Difference(final_region, brush_region_affine, out ExpTmpOutVar_0);
final_region.Dispose();
final_region = ExpTmpOutVar_0;
}
break;
default:
MessageBox.Show("设置错误");
return;
}//end switch
}//end if
}
else
{
HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconTool.disp_message(hv_WindowHandle, "请将鼠标移动到窗口内部", "window", 20, 20, "red", "false");
}
}//end while
#endregion
}
catch (HalconException HDevExpDefaultException)
{
throw HDevExpDefaultException;
}
finally
{
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region, "blue");
hWindow_Final1.DrawModel = false;
groupBox_tool.Enabled = true;
}
}
/// <summary>
/// 显示结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_show_finalRegion_Click(object sender, EventArgs e)
{
hWindow_Final1.HobjectToHimage(image);
hWindow_Final1.DispObj(final_region,"blue");
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)