Halcon中 有两种参数类型:
分为 Iconic 与 Control. Iconic 为图片, control 为a1 b2
这两种, 每一种又分为输入和输出两种参数
固定参数顺序
input Iconic
output Iconic
input control
output control
using HalconDotNet;
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 System.Threading;
namespace CsharpHaldemon2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region HObject 转换为 HImage
private void HobjectToHimage(HObject hobject, ref HImage image)
{
HTuple pointer, type, width, height;
HOperatorSet.GetImagePointer1(hobject, out pointer, out type, out width, out height);
image.GenImage1(type, width, height, pointer);
}
#endregion
#region 设置图片与窗口等比例
private void WindowAdaptation(int width,int height, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2)
{
double ratioWidth = (1.0) * width / hWindowControl1.Width;
double ratioHeight = (1.0) * height / hWindowControl1.Height;
if (ratioWidth > ratioHeight)
{
row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
column1 = 0;
row2 = row1 + hWindowControl1.Height * ratioWidth;
column2 = column1 + hWindowControl1.Width * ratioWidth;
}
else
{
row1 = 0;
column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
row2 = row1 + hWindowControl1.Height * ratioHeight;
column2 = column1 + hWindowControl1.Width * ratioHeight;
}
}
#endregion
private CancellationTokenSource cts = new CancellationTokenSource();
private CancellationTokenSource cts1 = new CancellationTokenSource();
private void btnOpenPic_Click(object sender, EventArgs e)
{
//HObject ho_Image, ho_ROI_0, ho_Cross;
//HXLD
//HXLDCont
//HXLDPoly //HXLDCont轮廓和HXLDPoly多边形
//HImage ho_Image = new HImage();
//HRegion ho_ROI_0 = new HRegion(); //区域
//HXLDCont ho_Cross = new HXLDCont(); //轮廓
//ho_Image.ReadImage(@"C:\Users\ZG\Pictures\claudia.png");
//ho_ROI_0.GenRectangle1(30.0, 20, 100, 200);
//hWindowControl1.HalconWindow.SetColor("blue");
//ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);
//hWindowControl1.HalconWindow.SetColor("green");
//ho_Cross.GenCrossContourXld(180.0, 180, 61, 0.785398);
//ho_Cross.DispXld(hWindowControl1.HalconWindow);
//this.hSmartWindowControl1.SetFullImagePart(ho_Image);
//this.hSmartWindowControl1.HalconWindow.DispObj(ho_Image);
//this.hWindowControl1.SetFullImagePart(ho_Image);
//this.hWindowControl1.HalconWindow.DispObj(ho_Image);
HObject ho_Image;
HImage ho_Image1 = new HImage();
HXLDCont ho_Cross = new HXLDCont(); //轮廓
HTuple hv_AcqHandle = new HTuple();
HTuple row1, column1, row2, column2;
int width, height;
Task.Run(() =>
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] Chicony USB2.0 Camera", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
//调用摄像头异步采集图像
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//关闭句柄
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
//把obj变为Himage
HobjectToHimage(ho_Image, ref ho_Image1);
//获取图像的长宽
ho_Image1.GetImageSize(out width, out height);
//XLD边缘提取
ho_Cross = ho_Image1.Rgb1ToGray().EdgesSubPix("canny", 2, 12, 22);
WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
//ho_Image1.DispImage(hWindowControl1.HalconWindow);
ho_Cross.DispXld(hWindowControl1.HalconWindow);
}, cts.Token);
}
//threshould阈值分割
private void btnThreshold_Click(object sender, EventArgs e)
{
int width, height;
HTuple row1, column1, row2, column2;
HImage ho_Image = new HImage();
HRegion ho_ROI_0 = new HRegion();
ho_Image.ReadImage(@"C:/Users/ZG/Pictures/clip.png");
ho_Image.GetImageSize(out width, out height);
ho_ROI_0 = ho_Image.Rgb1ToGray().Threshold(0.0, 106).Connection().SelectShape("area", "and", 150, 99999);
WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
hWindowControl1.HalconWindow.SetColor("blue");
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);
}
private void picConvertXLD_Click(object sender, EventArgs e)
{
int width, height;
HTuple row1, column1, row2, column2;
HImage ho_Image = new HImage();
HRegion ho_ROI_0 = new HRegion();
HXLDCont ho_Cross = new HXLDCont(); //轮廓
ho_Image.ReadImage(@"C:/Users/ZG/Pictures/fabrik.png");
ho_Cross = ho_Image.Rgb1ToGray().EdgesSubPix("canny", 2, 12, 22);
//适配窗口
ho_Image.GetImageSize(out width, out height);
WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
ho_Cross.DispXld(hWindowControl1.HalconWindow);
}
private void btnThresholdMe_Click(object sender, EventArgs e)
{
HObject ho_Image;
HImage ho_Image1 = new HImage();
HRegion ho_ROI_0 = new HRegion();
HXLDCont ho_Cross = new HXLDCont(); //轮廓
HTuple hv_AcqHandle = new HTuple();
HTuple row1, column1, row2, column2;
int width, height;
Task.Run(() =>
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] Chicony USB2.0 Camera", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
//调用摄像头异步采集图像
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//关闭句柄
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
//把obj变为Himage
HobjectToHimage(ho_Image, ref ho_Image1);
//获取图像的长宽
ho_Image1.GetImageSize(out width, out height);
//XLD边缘提取
ho_ROI_0 = ho_Image1.Rgb1ToGray().Threshold(75.0, 122).Connection().SelectShape("area", "and", 150, 46614.6);
WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
hWindowControl1.HalconWindow.SetColor("blue");
//ho_Image1.DispImage(hWindowControl1.HalconWindow);
ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);
//ho_Cross.DispXld(hWindowControl1.HalconWindow);
}, cts1.Token);
}
}
#region 设置图片与窗口等比例
//ho_Image.GetImageSize(out width, out height);
//double ratioWidth = (1.0) * width / hWindowControl1.Width;
//double ratioHeight = (1.0) * height / hWindowControl1.Height;
//if (ratioWidth > ratioHeight)
//{
// row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
// column1 = 0;
// row2 = row1 + hWindowControl1.Height * ratioWidth;
// column2 = column1 + hWindowControl1.Width * ratioWidth;
//}
//else
//{
// row1 = 0;
// column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
// row2 = row1 + hWindowControl1.Height * ratioHeight;
// column2 = column1 + hWindowControl1.Width * ratioHeight;
//}
#endregion
}
仿射变换
private void btnAff_Click(object sender, EventArgs e)
{
//this.hWindowControl1.HalconWindow.ClearWindow();
int width, height;
HTuple row1, column1, row2, column2;
HImage ho_Image = new HImage();
HImage ho_Image2 = new HImage();
HRegion ho_ROI_0 = new HRegion();
HXLDCont ho_Cross = new HXLDCont(); //轮廓
// region的面积, 中心坐标y,x, 单位矩阵 平移矩阵 旋转矩阵 缩放矩阵
HTuple hv_Area, hv_Row, hv_Column, hv_HomMat2DIdentity, hv_HomMat2DTranslate, hv_HomMat2DRotate, hv_HomMat2DScale;
HObject ImageAffineTrans = new HObject();
HObject ImageAffineRotate = new HObject();
HObject ImageAffineScale = new HObject(); //平移后的图片 旋转后的图片 缩放后的图片
ho_Image.ReadImage(@"C:/Users/hp/Pictures/sanjiao.png");
//ho_Image.ReadImage(@"C:/Users/ZG/Pictures/screw_thread.png");
ho_Image.GetImageSize(out width, out height); //获取图片长和宽
//*图像二值化, 阈值分割
HRegion GrayRegion = ho_Image.Rgb1ToGray().Threshold(0.0, 200);
//获取面积, region中心点的坐标(hv_Row, hv_Column)
HOperatorSet.AreaCenter(GrayRegion, out hv_Area, out hv_Row, out hv_Column);
*定义Home单位矩阵
HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
//图片平移 单位矩阵, 移动距离x,y, 平移后的矩阵
HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, (height / 2) - hv_Row, (width / 2) - hv_Column,
out hv_HomMat2DTranslate);
//HOperatorSet.AffineTransImage(ho_Image, out ImageAffineTrans, hv_HomMat2DTranslate, "constant", "false");
HOperatorSet.AffineTransImage(GrayRegion, out ImageAffineTrans, hv_HomMat2DTranslate, "constant", "false");
//图片旋转
//获得旋转矩阵 hv_HomMat2DRotate
HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -3.14 / 2, height / 2, width / 2, out hv_HomMat2DRotate);
//HOperatorSet.AffineTransImage(ImageAffineTrans, out ImageAffineRotate,hv_HomMat2DRotate, "constant", "false");
HOperatorSet.AffineTransImage(ImageAffineTrans, out ImageAffineRotate, hv_HomMat2DRotate, "constant", "false");
//图片缩放
HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, 0.5, 0.5, hv_Column, hv_Row,out hv_HomMat2DScale);
HOperatorSet.AffineTransImage(ImageAffineRotate, out ImageAffineScale, hv_HomMat2DScale, "constant", "false");
WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
hWindowControl1.HalconWindow.SetColor("blue");
ImageAffineScale.DispObj(hWindowControl1.HalconWindow);
//GrayRegion.DispRegion(hWindowControl1.HalconWindow);
//HOperatorSet.DispObj(ImageAffineTrans, hWindowControl1.HalconWindow);
//HOperatorSet.SetWindowAttr("background_color", "sky blue"); /必须的先设置一下background_color属性,再OpenWindow一下
//hWindowControl1.HalconWindow.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.Handle, "visible", "");
//HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
//HOperatorSet.DispObj(ImageAffineScale, hWindowControl1.HalconWindow);
//ImageAffineScale.DispObj(hWindowControl1.HalconWindow);
//HobjectToHimage(ImageAffineScale, ref ho_Image2);
//ho_Image2.DispImage(hWindowControl1.HalconWindow);
}
边缘亚像素提取
三维图中
| | |
Xb Yb Zb
| | |
二维图中
左边矩阵表示平移, 中间表示旋转, 右边表示缩放