作者:韩兆新
出处:http://hanzhaoxin.cnblogs.com/
示例:读取并处理图片
halcon源码:
read_image (Image, 'claudia')
get_image_size(Image,Width,Height)
*图像处理
rgb1_to_gray(Image,GrayImage)
*使图像在C#控件铺开显示
dev_set_part (0, 0, Height-1, Width-1)
dev_display (GrayImage)
dev_set_part (0, 0, Height-1, Width-1)
dev_display (GrayImage)
导出的代码(ImagePro.cs):
// File generated by HDevelop for HALCON/.NET (C#) Version 13.0.1.1
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using HalconDotNet;
public partial class HDevelopExport
{
public HTuple hv_ExpDefaultWinHandle;
// Main procedure
private void action()
{
// Local iconic variables
HObject ho_Image, ho_GrayImage;
// Local control variables
HTuple hv_Width = null, hv_Height = null;
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_GrayImage);
//读取图片
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, "claudia");
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
//图像处理
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
ho_Image.Dispose();
ho_GrayImage.Dispose();
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public void RunHalcon(HTuple Window)
{
hv_ExpDefaultWinHandle = Window;
action();
}
}
C#工程:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//图像处理按钮是否使能
this.button2.Enabled = false;
}
HDevelopExport HD = new HDevelopExport();
private void button1_Click(object sender, EventArgs e)
{
HD.Readimage_action(hWindowControl1.HalconWindow);
//图像处理按钮是否使能
this.button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
HD.Imagepro_action();
//图像处理按钮是否使能
this.button2.Enabled = false;
}
}
}
//项目,添加现有项ReadImagepro.cs到工程
//修改ImagePro.cs如下
//
// File generated by HDevelop for HALCON/.NET (C#) Version 13.0.1.1
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using HalconDotNet;
public partial class HDevelopExport
{
public HTuple hv_ExpDefaultWinHandle;
// Local iconic variables
HObject ho_Image, ho_GrayImage;
// Local control variables
HTuple hv_Width = null, hv_Height = null;
// Main procedure
public void Readimage_action(HTuple Window)
{
hv_ExpDefaultWinHandle = Window;
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_GrayImage);
//读取图片
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, "claudia");
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
//HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
}
public void Imagepro_action()
{
//图像处理
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
ho_Image.Dispose();
ho_GrayImage.Dispose();
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public void RunHalcon(HTuple Window)
{
hv_ExpDefaultWinHandle = Window;
//action();
}
}
运行结果:
单击读取图片按钮并选取图片:
点击图像处理按钮: