|
|
using ChoiceTech.Halcon.Control;
|
|
|
using HalconDotNet;
|
|
|
using Rs.Camera;
|
|
|
using Rs.Controls;
|
|
|
using Rs.Framework;
|
|
|
using Rs.Motion;
|
|
|
using Rs.MotionPlat.Commom;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Data;
|
|
|
using System.Diagnostics;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Forms;
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
namespace Rs.MotionPlat
|
|
|
{
|
|
|
public enum ERoiShape
|
|
|
{
|
|
|
Rectange,
|
|
|
Circle,
|
|
|
Any
|
|
|
}
|
|
|
|
|
|
public enum EFindShapeMode
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 模板查找
|
|
|
/// </summary>
|
|
|
Model,
|
|
|
/// <summary>
|
|
|
/// 自定义查找
|
|
|
/// </summary>
|
|
|
SelfDefine
|
|
|
}
|
|
|
public partial class CameraTemplate : UserControl
|
|
|
{
|
|
|
public event Action ManualGrab;
|
|
|
private EFindShapeMode eFindShapeMode = EFindShapeMode.Model;
|
|
|
public event Action<HWindow_Final, HObject> FindShapeEvent;
|
|
|
private string removedColor = "#FF0000B4";
|
|
|
ProductModel pm = new ProductModel();
|
|
|
HWindow winHandle = null;
|
|
|
HObject hImage = null;
|
|
|
HObject final_Region = new HObject();
|
|
|
private string cameraName = "";
|
|
|
private string m_savePath = "";
|
|
|
double matchResultRow = 0.0;
|
|
|
double matchResultColumn = 0.0;
|
|
|
double matchResultAngle= 0.0;
|
|
|
double matchResultScore = 0.0;
|
|
|
string LightName = "";
|
|
|
bool bGrab = false;
|
|
|
/// <summary>
|
|
|
/// 自定义拍照事件
|
|
|
/// </summary>
|
|
|
public event Action CustomDefineGrabEvent;
|
|
|
public CameraTemplate()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
HikCamera.Instance.ManualGrabOkEvent += Instance_GrabOkEvent;
|
|
|
winHandle = hWindow_Final1.hWindowControl.HalconWindow;
|
|
|
HOperatorSet.GenEmptyObj(out final_Region);
|
|
|
}
|
|
|
|
|
|
public string ItemName { get; set; } = "Default";
|
|
|
|
|
|
public void RemoveGrabEvent()
|
|
|
{
|
|
|
HikCamera.Instance.ManualGrabOkEvent -= Instance_GrabOkEvent;
|
|
|
}
|
|
|
|
|
|
public void SetFindShapeMode(EFindShapeMode eFindShapeMode)
|
|
|
{
|
|
|
this.eFindShapeMode = eFindShapeMode;
|
|
|
}
|
|
|
|
|
|
public void AttachFindShapeEvent(Action<HWindow_Final, HObject> fun)
|
|
|
{
|
|
|
FindShapeEvent= fun;
|
|
|
}
|
|
|
|
|
|
static int i = 0;
|
|
|
private void Instance_GrabOkEvent(int arg1, HObject arg2)
|
|
|
{
|
|
|
lock(this)
|
|
|
{
|
|
|
if(arg2.IsInitialized())
|
|
|
{
|
|
|
//HOperatorSet.WriteImage(arg2, "bmp", 0, $"d:/temp/{i++}.bmp");
|
|
|
////HOperatorSet.SetPart(hWindow_Final1.hWindowControl.HalconWindow, 0, 0, - 1, - 1);
|
|
|
//hWindow_Final1.ClearWindow();
|
|
|
//HOperatorSet.RotateImage(arg2, out hImage, 0, "constant");
|
|
|
hImage = arg2.CopyObj(1,1);
|
|
|
//hWindow_Final1.HobjectToHimage(hImage);
|
|
|
////hWindow_Final1.ClearWindow();
|
|
|
////HOperatorSet.GetImageSize(hImage,out HTuple width,out HTuple height);
|
|
|
|
|
|
////hWindow_Final1.hWindowControl.ImagePart = new Rectangle()
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
arg2.Dispose();
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
//hWindow_Final1.ClearWindow();
|
|
|
//hWindow_Final1.DispObj(hImage);
|
|
|
|
|
|
HOperatorSet.GetImageSize(hImage, out HTuple imgWidth, out HTuple imgHeight);
|
|
|
HOperatorSet.GenCrossContourXld(out HObject camCenter, imgHeight / 2, imgWidth / 2, Math.Max(imgHeight, imgWidth), 0);
|
|
|
hWindow_Final1.DispObj(camCenter);
|
|
|
//505 685
|
|
|
HOperatorSet.GenCrossContourXld(out HObject ccc, 505, 685, 100, 0);
|
|
|
hWindow_Final1.DispObj(ccc,"red");
|
|
|
IoManager.Instance.WriteOut(LightName, 0);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
HTuple circle1Row = new HTuple();
|
|
|
HTuple circle1Column = new HTuple();
|
|
|
HTuple circle1Radius = new HTuple();
|
|
|
HTuple circle1StartPhi = new HTuple();
|
|
|
HTuple circle1EndPhi = new HTuple();
|
|
|
HTuple[] c1 = new HTuple[] { 659.789184570313, 1660, 358.429901123047, 0.531410278720821, 1.1323930701261 };
|
|
|
HTuple[] c2 = new HTuple[] { 1436.11840820313, 1639, 393.620727539063, 5.2841693241961, 5.80238301910421 };
|
|
|
HTuple[] c3 = new HTuple[] { 1444, 952, 394.189666748047, 3.68072622782857, 4.15055583399968 };
|
|
|
|
|
|
List<HDrawingObject> hDrawingObjects = new List<HDrawingObject>();
|
|
|
private void btnLoadImage_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
HOperatorSet.GenEmptyObj(out hImage);
|
|
|
OpenFileDialog fileOpen=new OpenFileDialog();
|
|
|
DialogResult dr = fileOpen.ShowDialog();
|
|
|
if(dr == DialogResult.OK)
|
|
|
{
|
|
|
string filename = fileOpen.FileName;
|
|
|
HOperatorSet.ReadImage(out hImage, filename);
|
|
|
HOperatorSet.SetDraw(hWindow_Final1.hWindowControl.HalconWindow, "margin");
|
|
|
// HOperatorSet.DispCross(hWindow_Final1.hWindowControl.HalconWindow, hWindow_Final1.Width / 2, hWindow_Final1.Height / 2, 300, 0);
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
// pm.SourceImage = hImage.CopyObj(1,1);
|
|
|
HOperatorSet.GetImageSize(hImage,out HTuple imgWidth,out HTuple imgHeight);
|
|
|
|
|
|
HOperatorSet.GenCrossContourXld(out HObject cross, imgHeight/2, imgWidth/2, Math.Max(imgHeight,imgWidth), 0);
|
|
|
hWindow_Final1.DispObj(cross,"green");
|
|
|
//#region 添加圆
|
|
|
|
|
|
////添加圆1
|
|
|
//circle1Row = 100;
|
|
|
//circle1Column= 100;
|
|
|
//circle1Radius = 80;
|
|
|
//circle1StartPhi = 6.5;
|
|
|
//circle1EndPhi = 1.4;
|
|
|
|
|
|
//HDrawingObject circle1 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, c1);
|
|
|
//HDrawingObject circle2 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, c2);
|
|
|
//HDrawingObject circle3 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, c3);
|
|
|
//hWindow_Final1.hWindowControl.HalconWindow.AttachDrawingObjectToWindow(circle1);
|
|
|
//hWindow_Final1.hWindowControl.HalconWindow.AttachDrawingObjectToWindow(circle2);
|
|
|
//hWindow_Final1.hWindowControl.HalconWindow.AttachDrawingObjectToWindow(circle3);
|
|
|
//hDrawingObjects.Add(circle1);
|
|
|
//hDrawingObjects.Add(circle2);
|
|
|
//hDrawingObjects.Add(circle3);
|
|
|
|
|
|
////创建卡尺模型
|
|
|
//HOperatorSet.CreateMetrologyModel(out HTuple metrologyHandle);
|
|
|
////添加圆弧卡尺工具
|
|
|
//HTuple circleParam = new HTuple(new double[] { c1[0], c1[1], c1[2] });
|
|
|
//HTuple genNames = new HTuple(new string[] { "start_phi", "end_phi" });
|
|
|
//HTuple genValues = new HTuple(new double[] { c1[3], c1[4] });
|
|
|
//HOperatorSet.AddMetrologyObjectGeneric(metrologyHandle, "circle", circleParam, 36, 20, 1, 30, genNames, genValues, out HTuple index);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_transition", "positive");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_select", "first");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_threshold", 30);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_length1", 36);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_length2", 5);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "measure_distance", 30);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "min_score", 0.8);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index, "num_instances", 6);
|
|
|
//HOperatorSet.ApplyMetrologyModel(img, metrologyHandle);
|
|
|
|
|
|
//HOperatorSet.GetMetrologyObjectMeasures(out HObject contour, metrologyHandle, "all", "all", out HTuple rows, out HTuple columns);
|
|
|
//HOperatorSet.GenCrossContourXld(out HObject crosss, rows, columns, 20, 0);
|
|
|
//HOperatorSet.GenContourPolygonXld(out HObject circleContour, rows, columns);
|
|
|
////拟合圆
|
|
|
//HOperatorSet.FitCircleContourXld(circleContour, "algebraic", -1, 0, 0, 3, 2, out HTuple circleRow, out HTuple circleColumn, out HTuple circleRadius, out HTuple circleStartPhi, out HTuple circleEndPhi, out HTuple pointOrder);
|
|
|
//HOperatorSet.GenCircle(out HObject realCicle, circleRow, circleColumn, circleRadius);
|
|
|
//hWindow_Final1.DispObj(contour);
|
|
|
//hWindow_Final1.DispObj(realCicle, "green");
|
|
|
//hWindow_Final1.DispObj(crosss, "green");
|
|
|
|
|
|
|
|
|
|
|
|
////添加圆弧卡尺工具
|
|
|
//HTuple circleParam2 = new HTuple(new double[] { c2[0], c2[1], c2[2] });
|
|
|
//HTuple genNames2 = new HTuple(new string[] { "start_phi", "end_phi" });
|
|
|
//HTuple genValues2 = new HTuple(new double[] { c2[3], c2[4] });
|
|
|
//HOperatorSet.AddMetrologyObjectGeneric(metrologyHandle, "circle", circleParam2, 36, 20, 1, 30, genNames2, genValues2, out HTuple index2);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_transition", "positive");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_select", "first");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_threshold", 40);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_length1", 36);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_length2", 5);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "measure_distance", 30);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "min_score", 0.8);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index2, "num_instances", 6);
|
|
|
//HOperatorSet.ApplyMetrologyModel(img, metrologyHandle);
|
|
|
|
|
|
//HOperatorSet.GetMetrologyObjectMeasures(out HObject contour2, metrologyHandle, index2, "all", out HTuple rows2, out HTuple columns2);
|
|
|
//HOperatorSet.GenCrossContourXld(out HObject crosss2, rows2, columns2, 20, 0);
|
|
|
//HOperatorSet.GenContourPolygonXld(out HObject circleContour2, rows2, columns2);
|
|
|
////拟合圆
|
|
|
//HOperatorSet.FitCircleContourXld(circleContour2, "algebraic", -1, 0, 0, 3, 2, out HTuple circleRow2, out HTuple circleColumn2, out HTuple circleRadius2, out HTuple circleStartPhi2, out HTuple circleEndPhi2, out HTuple pointOrder2);
|
|
|
//HOperatorSet.GenCircle(out HObject realCicle2, circleRow2, circleColumn2, circleRadius2);
|
|
|
//hWindow_Final1.DispObj(contour2);
|
|
|
//hWindow_Final1.DispObj(realCicle2, "green");
|
|
|
//hWindow_Final1.DispObj(crosss2, "green");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////添加圆弧卡尺工具
|
|
|
//HTuple circleParam3 = new HTuple(new double[] { c3[0], c3[1], c3[2] });
|
|
|
//HTuple genNames3 = new HTuple(new string[] { "start_phi", "end_phi" });
|
|
|
//HTuple genValues3 = new HTuple(new double[] { c3[3], c3[4] });
|
|
|
//HOperatorSet.AddMetrologyObjectGeneric(metrologyHandle, "circle", circleParam3, 36, 10, 1, 30, genNames3, genValues3, out HTuple index3);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_transition", "positive");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_select", "first");
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_threshold", 30);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_length1", 36);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_length2", 5);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "measure_distance", 30);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "min_score", 0.8);
|
|
|
//HOperatorSet.SetMetrologyObjectParam(metrologyHandle, index3, "num_instances", 6);
|
|
|
//HOperatorSet.ApplyMetrologyModel(img, metrologyHandle);
|
|
|
|
|
|
//HOperatorSet.GetMetrologyObjectMeasures(out HObject contour3, metrologyHandle, index3, "all", out HTuple rows3, out HTuple columns3);
|
|
|
//HOperatorSet.GenCrossContourXld(out HObject crosss3, rows3, columns3, 20, 0);
|
|
|
//HOperatorSet.GenContourPolygonXld(out HObject circleContour3, rows3, columns3);
|
|
|
////拟合圆
|
|
|
//HOperatorSet.FitCircleContourXld(circleContour3, "algebraic", -1, 0, 0, 3, 2, out HTuple circleRow3, out HTuple circleColumn3, out HTuple circleRadius3, out HTuple circleStartPhi3, out HTuple circleEndPhi3, out HTuple pointOrder3);
|
|
|
//HOperatorSet.GenCircle(out HObject realCicle3, circleRow3, circleColumn3, circleRadius3);
|
|
|
//hWindow_Final1.DispObj(contour3);
|
|
|
//hWindow_Final1.DispObj(realCicle3, "green");
|
|
|
//hWindow_Final1.DispObj(crosss3, "green");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//HOperatorSet.GenRegionLine(out HObject regionLines, circleRow2, circleColumn2, circleRow3, circleColumn3);
|
|
|
//hWindow_Final1.DispObj(regionLines, "green");
|
|
|
|
|
|
//foreach (HDrawingObject hdo in hDrawingObjects)
|
|
|
//{
|
|
|
// hdo.ClearDrawingObject();
|
|
|
//}
|
|
|
|
|
|
//#endregion
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void btnGrab_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
ImageProcess.ClearManualTrigger();
|
|
|
HikCamera.Instance.SetTrigger(cameraName, ETriggerMode.Manual);
|
|
|
bGrabContinue = false;
|
|
|
if (CustomDefineGrabEvent!=null)
|
|
|
{
|
|
|
CustomDefineGrabEvent?.Invoke();
|
|
|
LoadImage();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
LightName = SysConfigParam.GetValue<string>(cameraName + "LightName");
|
|
|
if (!string.IsNullOrEmpty(LightName))
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(cameraName))
|
|
|
{
|
|
|
bGrab = true;
|
|
|
|
|
|
if (ManualGrab == null)
|
|
|
{
|
|
|
IoManager.Instance.WriteOut(LightName, 0);
|
|
|
Thread.Sleep(50);
|
|
|
IoManager.Instance.WriteOut(LightName, 1);
|
|
|
Thread.Sleep(100);
|
|
|
HikCamera.Instance.Grab(cameraName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ManualGrab?.Invoke();
|
|
|
}
|
|
|
//Thread.Sleep(50);
|
|
|
|
|
|
LoadImage();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Msg.ShowError("请先先择相机");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Msg.ShowError("未设置相机对应的光源,请到变量中配置!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
bool bGrabContinue = false;
|
|
|
|
|
|
public void StopGrab()
|
|
|
{
|
|
|
bGrabContinue = false;
|
|
|
}
|
|
|
private void btnGrabContinue_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
bGrabContinue = !bGrabContinue;
|
|
|
if (bGrabContinue)
|
|
|
{
|
|
|
btnGrabContinue.Text = "停止取图";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
btnGrabContinue.Text = "连续取图";
|
|
|
}
|
|
|
if (CustomDefineGrabEvent!=null)
|
|
|
{
|
|
|
Task.Run(() => {
|
|
|
while (bGrabContinue)
|
|
|
{
|
|
|
bGrab = true;
|
|
|
ImageProcess.ClearManualTrigger();
|
|
|
CustomDefineGrabEvent?.Invoke();
|
|
|
LoadImage();
|
|
|
Thread.Sleep(300);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
LightName = SysConfigParam.GetValue<string>(cameraName + "LightName");
|
|
|
if (!string.IsNullOrEmpty(cameraName))
|
|
|
{
|
|
|
HikCamera.Instance.SetTrigger(cameraName, ETriggerMode.Manual);
|
|
|
//bGrabContinue = !bGrabContinue;
|
|
|
Task.Run(() => {
|
|
|
while (bGrabContinue)
|
|
|
{
|
|
|
bGrab = true;
|
|
|
ImageProcess.ClearManualTrigger();
|
|
|
IoManager.Instance.WriteOut(LightName, 0);
|
|
|
Thread.Sleep(50);
|
|
|
IoManager.Instance.WriteOut(LightName, 1);
|
|
|
Thread.Sleep(50);
|
|
|
HikCamera.Instance.Grab(cameraName);
|
|
|
LoadImage();
|
|
|
Thread.Sleep(300);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected override void OnVisibleChanged(EventArgs e)
|
|
|
{
|
|
|
base.OnVisibleChanged(e);
|
|
|
}
|
|
|
|
|
|
private void LoadImage()
|
|
|
{
|
|
|
Stopwatch time = new Stopwatch();
|
|
|
time.Start();
|
|
|
while (true && time.ElapsedMilliseconds < 3000 && bGrab == true)
|
|
|
{
|
|
|
if (ImageProcess.manualTriggerEvent.WaitOne(100))
|
|
|
{
|
|
|
if (ImageProcess.GetManualImage().IsInitialized())
|
|
|
{
|
|
|
hImage = ImageProcess.GetManualImage().CopyObj(1, 1);
|
|
|
hWindow_Final1.ClearWindow();
|
|
|
if(winHandle.IsInitialized())
|
|
|
{
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
HObject imgRotate = new HObject();
|
|
|
//arg2.Dispose();
|
|
|
if (cameraName==SysConfigParam.GetValue<string>("UpCameraName"))
|
|
|
{
|
|
|
HOperatorSet.RotateImage(hImage, out hImage, SysConfigParam.GetValue<double>("UpCameraRotate"), "constant");
|
|
|
}
|
|
|
else if(cameraName== SysConfigParam.GetValue<string>("DownLocationCameraName"))
|
|
|
{
|
|
|
HOperatorSet.RotateImage(hImage, out hImage, SysConfigParam.GetValue<double>("DownLocationCameraRotate"), "constant");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
HOperatorSet.RotateImage(hImage, out hImage, 0, "constant");
|
|
|
}
|
|
|
//HOperatorSet.WriteImage(hImage, "bmp", 0, "d://1");
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
HOperatorSet.GetImageSize(hImage, out HTuple imgWidth, out HTuple imgHeight);
|
|
|
HOperatorSet.GenCrossContourXld(out HObject camCenter, imgHeight / 2, imgWidth / 2, Math.Max(imgHeight, imgWidth), 0);
|
|
|
hWindow_Final1.DispObj(camCenter);
|
|
|
IoManager.Instance.WriteOut(LightName, 0);
|
|
|
if(eFindShapeMode == EFindShapeMode.SelfDefine)
|
|
|
{
|
|
|
FindShapeEvent?.Invoke(hWindow_Final1, hImage);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if(time.ElapsedMilliseconds>3000)
|
|
|
{
|
|
|
IoManager.Instance.WriteOut(LightName, 0);
|
|
|
Msg.ShowError("grab fail");
|
|
|
|
|
|
}
|
|
|
bGrab = false;
|
|
|
}
|
|
|
|
|
|
HObject imageReduced, partImage;
|
|
|
private void btnCreateModel_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
hWindow_Final1.Focus();
|
|
|
hWindow_Final1.DrawModel = true;
|
|
|
|
|
|
//winHandle.SetColor("green");
|
|
|
//winHandle.SetDraw("margin");
|
|
|
//HTuple row = new HTuple(), column = new HTuple(), radius = new HTuple();
|
|
|
//HOperatorSet.DrawRegion(out final_Region, winHandle);
|
|
|
//hWindow_Final1.DispObj(final_Region, "green");
|
|
|
|
|
|
//HTuple row1 = new HTuple(), col1 = new HTuple(), row2 = new HTuple(), col2 = new HTuple();
|
|
|
|
|
|
//HOperatorSet.DrawRectangle1(winHandle, out row1, out col1, out row2, out col2);
|
|
|
// HObject rect = new HObject();
|
|
|
//HOperatorSet.GenEmptyObj(out rect);
|
|
|
//HOperatorSet.GenRectangle1(out rect, row1, col1, row2, col2);
|
|
|
//hWindow_Final1.DispObj(rect, "green");
|
|
|
|
|
|
//HOperatorSet.DispObj(rect, hWindow_Final1.hWindowControl.HalconWindow);
|
|
|
|
|
|
|
|
|
try
|
|
|
{
|
|
|
winHandle.SetColor("green");
|
|
|
winHandle.SetDraw("margin");
|
|
|
HOperatorSet.GenEmptyObj(out imageReduced);
|
|
|
HOperatorSet.GenEmptyObj(out partImage);
|
|
|
HTuple row1 = new HTuple(), column1 = new HTuple(), row2 = new HTuple(), column2 = new HTuple();
|
|
|
HOperatorSet.DrawRectangle1(winHandle, out row1, out column1, out row2, out column2);
|
|
|
HOperatorSet.GenRectangle1(out final_Region, row1, column1, row2, column2);
|
|
|
pm.RoiRegion = final_Region;
|
|
|
|
|
|
|
|
|
HOperatorSet.CropPart(hImage, out partImage, row1, column1, column2 - column1, row2 - row1);
|
|
|
HOperatorSet.ReduceDomain(hImage, final_Region,out imageReduced);
|
|
|
pm.TemplateImage= partImage;
|
|
|
|
|
|
HOperatorSet.InspectShapeModel(imageReduced, out HObject shapeModel, out HObject modelRegions, 1, 30);
|
|
|
hWindow_Final1.DispObj(modelRegions);
|
|
|
hWindow_Final2.HobjectToHimage(partImage);
|
|
|
HOperatorSet.SetColor(winHandle, "green");
|
|
|
//HOperatorSet.DispObj(pm.RoiRegion, winHandle);
|
|
|
hWindow_Final1.DispObj(pm.RoiRegion);
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
hWindow_Final1.DrawModel = false;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private void button4_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
HObject removedRegion;
|
|
|
HTuple hv_Button = null;
|
|
|
HTuple hv_Row = null, hv_Column = null;
|
|
|
HObject brushRegion, brushRegionAffine;
|
|
|
HOperatorSet.GenCircle(out brushRegion, 50, 50, 20);
|
|
|
HTuple brushRow, brushColumn, brushArea;
|
|
|
//HOperatorSet.GenEmptyObj(out brushRegion);
|
|
|
HOperatorSet.GenEmptyObj(out brushRegionAffine);
|
|
|
HOperatorSet.GenEmptyObj(out removedRegion);
|
|
|
hWindow_Final1.DrawModel = true;
|
|
|
Task.Run(() => {
|
|
|
hv_Button = 0;
|
|
|
int a = 0;
|
|
|
HOperatorSet.AreaCenter(brushRegion, out brushArea, out brushRow, out brushColumn);
|
|
|
while(true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
HOperatorSet.GetMposition(winHandle, out hv_Row, out hv_Column, out hv_Button);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//MessageBox.Show(ex.Message);
|
|
|
}
|
|
|
|
|
|
if (removedRegion.IsInitialized())
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
//////HOperatorSet.DispObj(final_region, Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID);
|
|
|
HObject image1, image2, resultImage;
|
|
|
resultImage = new HObject();
|
|
|
HObject iamgeR, iamgeG, iamgeB;
|
|
|
//HOperatorSet.Decompose3(ho_image, out iamgeR, out iamgeG, out iamgeB);
|
|
|
int c = removedRegion.CountObj();
|
|
|
if(c>0)
|
|
|
{
|
|
|
//HOperatorSet.PaintRegion(removedRegion, ho_image, out image1, 120, "fill");
|
|
|
//HOperatorSet.Compose3(image1, image1, ho_image, out resultImage);
|
|
|
//HOperatorSet.SetSystem("flush_graphic", "false");
|
|
|
//HOperatorSet.DispObj(resultImage, winHandle);
|
|
|
}
|
|
|
|
|
|
//hWindow_Final1.HobjectToHimage(resultImage);
|
|
|
if (hv_Row >= 0 && hv_Column >= 0)
|
|
|
{
|
|
|
//当鼠标移动时,画出画笔
|
|
|
//生成移动矩阵
|
|
|
|
|
|
HOperatorSet.SetColor(winHandle, "gray");
|
|
|
HOperatorSet.VectorAngleToRigid(brushRow, brushColumn, 0, hv_Row, hv_Column, 0, out HTuple homMoveMat2D);
|
|
|
brushRegionAffine.Dispose();
|
|
|
//移动
|
|
|
HOperatorSet.AffineTransRegion(brushRegion, out brushRegionAffine, homMoveMat2D, "nearest_neighbor");
|
|
|
HOperatorSet.SetSystem("flush_graphic", "true");
|
|
|
HOperatorSet.DispObj(brushRegionAffine, winHandle);
|
|
|
if (hv_Button == 1)
|
|
|
{
|
|
|
if(a==1)
|
|
|
{
|
|
|
HOperatorSet.Difference(removedRegion, brushRegionAffine, out removedRegion);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
HOperatorSet.ConcatObj(removedRegion, brushRegionAffine,out removedRegion);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//HOperatorSet.SetPart(Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID, row1 - 20, col1 - 20, row2 + 20, col2 + 20);
|
|
|
|
|
|
// HOperatorSet.DispObj(resultImage, Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID);
|
|
|
//HOperatorSet.SetColor(Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID, "gray");
|
|
|
//HOperatorSet.DispObj(final_region, Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID);
|
|
|
}
|
|
|
catch { }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private void button5_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
private void trackBar2_ValueChanged(object sender, EventArgs e)
|
|
|
{
|
|
|
lblContrast.Text = tbarContrast.Value.ToString();
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
if(pm.FinalRegion!=null && pm.FinalRegion.IsInitialized())
|
|
|
{
|
|
|
HOperatorSet.ReduceDomain(hImage, pm.FinalRegion, out HObject imageReduced);
|
|
|
HOperatorSet.InspectShapeModel(imageReduced, out HObject modelImages, out HObject modelRegions, 1, tbarContrast.Value);
|
|
|
hWindow_Final1.DispObj(pm.SourceImage);
|
|
|
hWindow_Final1.DispObj(modelRegions, "green");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void btnStudy_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
if(pm.FinalRegion== null || pm.FinalRegion.CountObj()==0)
|
|
|
{
|
|
|
Msg.ShowInfo("请先绘制ROI区域");
|
|
|
return;
|
|
|
}
|
|
|
HOperatorSet.ClearWindow(hWindow_Final2.hWindowControl.HalconWindow);
|
|
|
HOperatorSet.ReduceDomain(hImage, pm.FinalRegion, out HObject imageReduced);
|
|
|
HOperatorSet.InspectShapeModel(imageReduced, out HObject modelImages, out HObject modelRegions, 1, 30);
|
|
|
hWindow_Final1.DispObj(hImage);
|
|
|
hWindow_Final1.DispObj(modelRegions, "green");
|
|
|
HOperatorSet.SmallestRectangle1(pm.FinalRegion,out HTuple row1,out HTuple column1,out HTuple row2,out HTuple column2);
|
|
|
HOperatorSet.SetPart(hWindow_Final2.hWindowControl.HalconWindow, row1 - 20, column1 - 20, row2 + 20, column2 + 20);
|
|
|
hWindow_Final2.DispObj(imageReduced);
|
|
|
hWindow_Final2.DispObj(modelRegions, "green");
|
|
|
}
|
|
|
|
|
|
private void btnDrawRectangle(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Rectange);
|
|
|
}
|
|
|
|
|
|
private void btnDrawCircle_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Circle);
|
|
|
}
|
|
|
|
|
|
bool bPaint = true;
|
|
|
bool isPaint = false;
|
|
|
private void btnPaint_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
bPaint = true;
|
|
|
PaintOrClear();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void btnClear_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
bPaint = false;
|
|
|
PaintOrClear();
|
|
|
}
|
|
|
|
|
|
private void PaintOrClear()
|
|
|
{
|
|
|
if (isPaint)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
HTuple brushSize = new HTuple(int.Parse(txtBrushSize.Text));
|
|
|
HObject removedRegion;
|
|
|
HObject hoImage;
|
|
|
HOperatorSet.GenEmptyObj(out removedRegion);
|
|
|
HTuple hv_Button = null;
|
|
|
HTuple hv_Row = null, hv_Column = null;
|
|
|
HObject brushRegion, brushRegionAffine;
|
|
|
HOperatorSet.GenEmptyObj(out brushRegionAffine);
|
|
|
HOperatorSet.GenEmptyObj(out brushRegion);
|
|
|
if(rbtnBrushCircle.Checked)
|
|
|
{
|
|
|
HOperatorSet.GenCircle(out brushRegion, 50, 50, brushSize.I);
|
|
|
}
|
|
|
if (rbtnBrushRectangle.Checked)
|
|
|
{
|
|
|
HOperatorSet.GenRectangle1(out brushRegion, 50, 50, 50+brushSize.I,50+brushSize.I);
|
|
|
}
|
|
|
HTuple brushRow, brushColumn, brushArea;
|
|
|
hWindow_Final1.DrawModel = true;
|
|
|
if (pm.RemovedRegion != null && pm.RemovedRegion.CountObj() > 0)
|
|
|
{
|
|
|
removedRegion = pm.RemovedRegion;
|
|
|
}
|
|
|
Task.Run(() => {
|
|
|
isPaint = true;
|
|
|
hv_Button = 0;
|
|
|
int a = 0;
|
|
|
HOperatorSet.ReduceDomain(hImage, pm.FinalRegion, out HObject imageReduced);
|
|
|
HOperatorSet.InspectShapeModel(imageReduced, out HObject modelImages, out HObject modelRegions, 1, 30);
|
|
|
HOperatorSet.SetColor(winHandle, "green");
|
|
|
HOperatorSet.DispObj(modelRegions, winHandle);
|
|
|
//hWindow_Final1.DispObj(modelRegions);
|
|
|
HOperatorSet.AreaCenter(brushRegion, out brushArea, out brushRow, out brushColumn);
|
|
|
while (true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//HOperatorSet.GetMbutton(winHandle, out HTuple mrow, out HTuple mcol, out hv_Button);
|
|
|
//获取鼠标位置信息
|
|
|
HOperatorSet.GetMposition(winHandle, out hv_Row, out hv_Column, out hv_Button);
|
|
|
if (hv_Button == 4)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//MessageBox.Show(ex.Message);
|
|
|
}
|
|
|
|
|
|
if (removedRegion.IsInitialized())
|
|
|
{
|
|
|
Thread.Sleep(10);
|
|
|
try
|
|
|
{
|
|
|
//////HOperatorSet.DispObj(final_region, Frm_ShapeMatchTool.Instance.hWindow_Final1.HWindowHalconID);
|
|
|
HObject image1, image2, resultImage;
|
|
|
resultImage = new HObject();
|
|
|
HObject iamgeR, iamgeG, iamgeB;
|
|
|
//HOperatorSet.Decompose3(ho_image, out iamgeR, out iamgeG, out iamgeB);
|
|
|
int c = removedRegion.CountObj();
|
|
|
if (c > 0)
|
|
|
{
|
|
|
//HOperatorSet.PaintRegion(removedRegion, pm.SourceImage, out image1, 180, "fill");
|
|
|
//HOperatorSet.Compose3(image1, image1, pm.SourceImage, out resultImage);
|
|
|
HOperatorSet.SetSystem("flush_graphic", "false");
|
|
|
HOperatorSet.DispObj(hImage, winHandle);
|
|
|
HOperatorSet.SetColor(winHandle, "#ff000099");
|
|
|
HOperatorSet.SetDraw(winHandle, "fill");
|
|
|
HOperatorSet.DispObj(pm.RemovedRegion, winHandle);
|
|
|
HOperatorSet.SetColor(winHandle, "green");
|
|
|
//HOperatorSet.DispObj(resultImage, winHandle);
|
|
|
HOperatorSet.DispObj(modelRegions, winHandle);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
HOperatorSet.SetSystem("flush_graphic", "false");
|
|
|
HOperatorSet.SetColor(winHandle, "green");
|
|
|
HOperatorSet.DispObj(hImage, winHandle);
|
|
|
HOperatorSet.DispObj(modelRegions, winHandle);
|
|
|
}
|
|
|
//HOperatorSet.DispObj(modelRegions, winHandle);
|
|
|
//hWindow_Final1.HobjectToHimage(pm.SourceImage);
|
|
|
if (hv_Row >= 0 && hv_Column >= 0)
|
|
|
{
|
|
|
//当鼠标移动时,画出画笔
|
|
|
//生成移动矩阵
|
|
|
HOperatorSet.SetColor(winHandle, "gray");
|
|
|
HOperatorSet.VectorAngleToRigid(brushRow, brushColumn, 0, hv_Row, hv_Column, 0, out HTuple homMoveMat2D);
|
|
|
brushRegionAffine.Dispose();
|
|
|
//移动
|
|
|
HOperatorSet.AffineTransRegion(brushRegion, out brushRegionAffine, homMoveMat2D, "nearest_neighbor");
|
|
|
HOperatorSet.SetSystem("flush_graphic", "true");
|
|
|
HOperatorSet.DispObj(brushRegionAffine, winHandle);
|
|
|
if (hv_Button == 1)
|
|
|
{
|
|
|
if(bPaint)
|
|
|
{
|
|
|
HOperatorSet.ConcatObj(removedRegion, brushRegionAffine, out removedRegion);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
HOperatorSet.Difference(removedRegion, brushRegionAffine, out removedRegion);
|
|
|
}
|
|
|
pm.RemovedRegion = removedRegion;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch { }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
//HOperatorSet.DispObj(pm.SourceImage,winHandle);
|
|
|
|
|
|
HObject img, rltimg;
|
|
|
if(removedRegion.CountObj()>0)
|
|
|
{
|
|
|
//HOperatorSet.PaintRegion(removedRegion, pm.SourceImage, out img, 120, "fill");
|
|
|
//HOperatorSet.Compose3(img, img, pm.SourceImage, out rltimg);
|
|
|
//hWindow_Final1.DispObj(rltimg);
|
|
|
//HOperatorSet.DispObj(pm.SourceImage, winHandle);
|
|
|
//HOperatorSet.SetColor(winHandle, "#ff000099");
|
|
|
HOperatorSet.SetDraw(winHandle, "fill");
|
|
|
//HOperatorSet.DispObj(pm.RemovedRegion, winHandle);
|
|
|
hWindow_Final1.viewWindow.setDrawModel(true);
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
hWindow_Final1.hWindowControl.HalconWindow.SetDraw("fill");
|
|
|
hWindow_Final1.DispObj(pm.RemovedRegion, "#ff000099");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//hWindow_Final1.DispObj(pm.SourceImage);
|
|
|
//HOperatorSet.ClearWindow(winHandle);
|
|
|
HOperatorSet.ReduceDomain(hImage, pm.RoiRegion, out imageReduced);
|
|
|
HOperatorSet.InspectShapeModel(imageReduced, out modelImages, out modelRegions, 1, 30);
|
|
|
////HOperatorSet.SetColor(winHandle, "green");
|
|
|
//hWindow_Final1.DispObj(modelRegions, "green");
|
|
|
//HOperatorSet.DispObj(modelRegions, winHandle);
|
|
|
|
|
|
isPaint = false;
|
|
|
|
|
|
|
|
|
//hWindow_Final1.DispObj(modelRegions);
|
|
|
hWindow_Final1.DrawModel = false;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private void btnWholePaint_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
if(pm.RoiRegion==null || pm.RoiRegion.CountObj()<=0)
|
|
|
{
|
|
|
Msg.ShowError("请选绘制ROI区域后再进行涂抹");
|
|
|
return;
|
|
|
}
|
|
|
pm.RemovedRegion = pm.RoiRegion;
|
|
|
//显示涂抹区域
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
//把ROI区域画到原图上
|
|
|
//HOperatorSet.PaintRegion(pm.RoiRegion,pm.SourceImage,out HObject imageResult,150,"fill");
|
|
|
//HOperatorSet.Compose3(imageResult, imageResult, pm.SourceImage, out HObject multiChannelImage);
|
|
|
//hWindow_Final1.DispObj(multiChannelImage);
|
|
|
hWindow_Final1.DispObj(pm.SourceImage);
|
|
|
HOperatorSet.SetDraw(hWindow_Final1.hWindowControl.HalconWindow, "fill");
|
|
|
//HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "#b91ff9ff");
|
|
|
|
|
|
hWindow_Final1.DispObj(pm.RemovedRegion,removedColor);
|
|
|
//HOperatorSet.DispObj(pm.RemovedRegion, hWindow_Final1.hWindowControl.HalconWindow);
|
|
|
}
|
|
|
|
|
|
private void btnSaveModel_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
//for(int i=0;i<3;i++)
|
|
|
//{
|
|
|
// HTuple row1 = hDrawingObjects[i].GetDrawingObjectParams("row");
|
|
|
// HTuple column1 = hDrawingObjects[i].GetDrawingObjectParams("column");
|
|
|
// HTuple radius1 = hDrawingObjects[i].GetDrawingObjectParams("radius");
|
|
|
// HTuple start_phi = hDrawingObjects[i].GetDrawingObjectParams("start_angle");
|
|
|
// HTuple end_phi = hDrawingObjects[i].GetDrawingObjectParams("end_angle");
|
|
|
// string content = $"{row1.D},{column1.D},{radius1.D},{start_phi.D},{end_phi.D}";
|
|
|
//}
|
|
|
|
|
|
pm.SourceImage = hImage.CopyObj(1, 1);
|
|
|
pm.Contrast = tbarContrast.Value;
|
|
|
pm.AngleStart = int.Parse(txtAngleStart.Text);
|
|
|
pm.AngleExtent = int.Parse(txtAngleExtent.Text);
|
|
|
pm.Score=double.Parse(txtScore.Text);
|
|
|
//pm.ItemName = m_itemName;
|
|
|
pm.SaveModel(m_savePath);
|
|
|
Msg.ShowInfo("Save product model success!");
|
|
|
}
|
|
|
|
|
|
private void btnTestModel_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
if(eFindShapeMode== EFindShapeMode.Model)
|
|
|
{
|
|
|
//先加载模板
|
|
|
IniHelper ini = new IniHelper(Path.Combine(m_savePath, "default.ini"));
|
|
|
string AngleStart = ini.ReadValue("ModelParam", "AngleStart", "-10");
|
|
|
string AngleExtent = ini.ReadValue("ModelParam", "AngleExtent", "10");
|
|
|
string Score = ini.ReadValue("ModelParam", "Score","0.5");
|
|
|
|
|
|
dataGridView1.Rows.Clear();
|
|
|
string filePath = Path.Combine(m_savePath, "model.shm");
|
|
|
HOperatorSet.ReadShapeModel(filePath, out HTuple modelID);
|
|
|
HOperatorSet.ReadRegion(out HObject searchRegion, Path.Combine(m_savePath + "\\search.reg"));
|
|
|
if (searchRegion.IsInitialized())
|
|
|
{
|
|
|
pm.SearchRegion = searchRegion;
|
|
|
HOperatorSet.ReduceDomain(hImage, pm.SearchRegion, out HObject searchImg);
|
|
|
HOperatorSet.GetShapeModelContours(out HObject modelContours, modelID, 1);
|
|
|
HOperatorSet.FindShapeModel(searchImg, modelID, AngleTool.Deg2Rad(double.Parse(AngleStart)), AngleTool.Deg2Rad(Math.Abs((double.Parse(AngleExtent) - double.Parse(AngleStart)))), new HTuple(double.Parse(Score)), 1, 0.7, "least_squares", 0, 0.9, out HTuple row, out HTuple column, out HTuple angle, out HTuple score);
|
|
|
if (score != null && score.Length > 0)
|
|
|
{
|
|
|
int len = score.Length;
|
|
|
for (int i = 0; i < len; i++)
|
|
|
{
|
|
|
HOperatorSet.VectorAngleToRigid(0, 0, 0, row[i].D, column[i].D, angle[i].D, out HTuple homMat2D);
|
|
|
HOperatorSet.AffineTransContourXld(modelContours, out HObject contoursAffineTrans, homMat2D);
|
|
|
hWindow_Final1.DispObj(contoursAffineTrans, "green");
|
|
|
|
|
|
matchResultRow = row.DArr[i];
|
|
|
matchResultColumn = column.DArr[i];
|
|
|
matchResultAngle =AngleTool.Rad2Deg( angle.DArr[i]);
|
|
|
matchResultScore = score.DArr[i];
|
|
|
int rowIndex = dataGridView1.Rows.Add();
|
|
|
dataGridView1.Rows[rowIndex].Cells[0].Value = matchResultRow.ToString("0.000");
|
|
|
dataGridView1.Rows[rowIndex].Cells[1].Value = matchResultColumn.ToString("0.000");
|
|
|
dataGridView1.Rows[rowIndex].Cells[2].Value = matchResultAngle.ToString("0.000");
|
|
|
dataGridView1.Rows[rowIndex].Cells[3].Value = matchResultScore.ToString("0.000");
|
|
|
HOperatorSet.GenCrossContourXld(out HObject cross, matchResultRow, matchResultColumn, 100, 0);
|
|
|
hWindow_Final1.DispObj(cross, "red");
|
|
|
}
|
|
|
//hWindow_Final1.ClearWindow();
|
|
|
//hWindow_Final1.HobjectToHimage(searchImg);
|
|
|
hWindow_Final1.DispObj(pm.SearchRegion, "red");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Msg.ShowError("未定位到产品");
|
|
|
}
|
|
|
HOperatorSet.GetImageSize(hImage, out HTuple imgWidth, out HTuple imgHeight);
|
|
|
HOperatorSet.GenCrossContourXld(out HObject camCenter, imgHeight / 2, imgWidth / 2, Math.Max(imgHeight, imgWidth), 0);
|
|
|
hWindow_Final1.DispObj(camCenter, "green");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
FindShapeEvent?.Invoke(hWindow_Final1, hImage);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void GetMatchResult(out double row,out double column,out double angle,out double score)
|
|
|
{
|
|
|
row=column=angle=score=0;
|
|
|
row = matchResultRow;
|
|
|
column=matchResultColumn;
|
|
|
angle = matchResultAngle;
|
|
|
score = matchResultScore;
|
|
|
|
|
|
}
|
|
|
private void btnDrawAny_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Any);
|
|
|
}
|
|
|
|
|
|
#region 加载
|
|
|
private void CameraTemplate_Load(object sender, EventArgs e)
|
|
|
{
|
|
|
if(!DesignMode)
|
|
|
{
|
|
|
m_savePath = Path.Combine("Recipe", SysConfigParam.GetValue<string>("CurRecipe"), ItemName);
|
|
|
//加载所有相机
|
|
|
foreach (string cam in HikCamera.Instance.GetCameras())
|
|
|
{
|
|
|
cboxCamera.Items.Add(cam);
|
|
|
comboBox1.Items.Add(cam);
|
|
|
}
|
|
|
//当窗口加载时,检测是否已经创建了模板,如果已经创建了,则加载模板
|
|
|
string filename = Path.Combine("Recipe", SysConfigParam.GetValue<string>("CurRecipe"), ItemName, "default.bmp");
|
|
|
if (File.Exists(filename))
|
|
|
{
|
|
|
HOperatorSet.ReadImage(out hImage, filename);
|
|
|
HOperatorSet.SetDraw(hWindow_Final1.hWindowControl.HalconWindow, "margin");
|
|
|
// HOperatorSet.DispCross(hWindow_Final1.hWindowControl.HalconWindow, hWindow_Final1.Width / 2, hWindow_Final1.Height / 2, 300, 0);
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
pm.SourceImage = hImage.CopyObj(1, 1);
|
|
|
HOperatorSet.GetImageSize(pm.SourceImage, out HTuple imgWidth, out HTuple imgHeight);
|
|
|
HOperatorSet.GenCrossContourXld(out HObject cross, imgHeight / 2, imgWidth / 2, Math.Max(imgHeight, imgWidth), 0);
|
|
|
hWindow_Final1.DispObj(cross, "green");
|
|
|
|
|
|
HOperatorSet.ReadRegion(out HObject RoiRegion, Path.Combine(m_savePath, "roi.reg"));
|
|
|
pm.RoiRegion = RoiRegion;
|
|
|
hWindow_Final1.DispObj(RoiRegion, "green");
|
|
|
HOperatorSet.ReadRegion(out HObject RemovedRegion, Path.Combine(m_savePath, "remove.reg"));
|
|
|
pm.RemovedRegion = RemovedRegion;
|
|
|
|
|
|
HOperatorSet.ReadRegion(out HObject SearchRegion, Path.Combine(m_savePath, "search.reg"));
|
|
|
pm.SearchRegion = SearchRegion;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (!DesignMode)
|
|
|
{
|
|
|
filename = Path.Combine(Environment.CurrentDirectory, "default.bmp");
|
|
|
HOperatorSet.ReadImage(out hImage, filename);
|
|
|
hWindow_Final1.HobjectToHimage(hImage);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
//查看参数文件是否存在,如果存在则加载参数
|
|
|
string paramFile = Path.Combine(m_savePath, "default.ini");
|
|
|
if (File.Exists(paramFile))
|
|
|
{
|
|
|
IniHelper ini = new IniHelper(paramFile);
|
|
|
string contrast = ini.ReadValue("ModelParam", "Contrast");
|
|
|
string angleStart = ini.ReadValue("ModelParam", "AngleStart");
|
|
|
string angleExtent = ini.ReadValue("ModelParam", "AngleExtent");
|
|
|
string score = ini.ReadValue("ModelParam", "Score");
|
|
|
if (!string.IsNullOrEmpty(contrast) && !string.IsNullOrEmpty(angleStart) && !string.IsNullOrEmpty(angleExtent) && !string.IsNullOrEmpty(score))
|
|
|
{
|
|
|
tbarContrast.Value = int.Parse(contrast);
|
|
|
txtAngleStart.Text = angleStart;
|
|
|
txtAngleExtent.Text = angleExtent;
|
|
|
txtScore.Text = score;
|
|
|
}
|
|
|
string name = ini.ReadValue("Camera", "Name");
|
|
|
string exposure = ini.ReadValue("Camera", "Exposure");
|
|
|
string gain = ini.ReadValue("Camera", "Gain");
|
|
|
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(exposure) && !string.IsNullOrEmpty(gain))
|
|
|
{
|
|
|
cboxCamera.SelectedItem = name;
|
|
|
txtExposure.Text = exposure;
|
|
|
txtGain.Text = gain;
|
|
|
cameraName = name;
|
|
|
HikCamera.Instance.SetExposure(name, float.Parse(exposure));
|
|
|
HikCamera.Instance.SetGain(name, float.Parse(gain));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
private void btnDrawSearch_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Rectange, true);
|
|
|
}
|
|
|
|
|
|
private void txtExposure_KeyUp(object sender, KeyEventArgs e)
|
|
|
{
|
|
|
if(e.KeyCode== Keys.Enter)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(cameraName))
|
|
|
{
|
|
|
HikCamera.Instance.SetExposure(cameraName, float.Parse(txtExposure.Text));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void txtGain_KeyUp(object sender, KeyEventArgs e)
|
|
|
{
|
|
|
if (e.KeyCode == Keys.Enter)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(cameraName))
|
|
|
{
|
|
|
HikCamera.Instance.SetGain(cameraName, float.Parse(txtGain.Text));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void rbtnSearchSelectedRegion_CheckedChanged(object sender, EventArgs e)
|
|
|
{
|
|
|
if(rbtnSearchSelectedRegion.Checked)
|
|
|
{
|
|
|
btnDrawSearch.Visible = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
btnDrawSearch.Visible = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void btnSaveParamer_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string dirPath = Path.Combine("Recipe",SysConfigParam.GetValue<string>("CurRecipe"),ItemName);
|
|
|
if (!Directory.Exists(m_savePath))
|
|
|
{
|
|
|
Directory.CreateDirectory(m_savePath);
|
|
|
}
|
|
|
|
|
|
IniHelper ini = new IniHelper(Path.Combine(m_savePath, "default.ini"));
|
|
|
ini.WriteValue("Camera", "Name", cboxCamera.SelectedItem.ToString());
|
|
|
ini.WriteValue("Camera", "Exposure", txtExposure.Text);
|
|
|
ini.WriteValue("Camera", "Gain", txtGain.Text);
|
|
|
cameraName = cboxCamera.SelectedItem.ToString();
|
|
|
Msg.ShowInfo("Save parameter success!");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Msg.ShowError(ex.Message);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private void DrawRoi(ERoiShape roiShape,bool isSearch=false)
|
|
|
{
|
|
|
HImage img = hWindow_Final1.Image;
|
|
|
hImage = img.CopyObj(1, 1);
|
|
|
pm.SourceImage = hImage.CopyObj(1,1);
|
|
|
if (img!=null)
|
|
|
{
|
|
|
pm.SourceImage = img;
|
|
|
}
|
|
|
if(pm.SourceImage== null || pm.SourceImage.CountObj()<=0)
|
|
|
{
|
|
|
Msg.ShowError("请先拍照或者选择照片");
|
|
|
return;
|
|
|
}
|
|
|
hWindow_Final1.Focus();
|
|
|
HalconTool.SetDisplayFont(winHandle, 20, "sans", new HTuple("true"), new HTuple("false"));
|
|
|
HalconTool.DispMessage(winHandle, "请在图像窗口中绘制ROI区域,点击右键结束", 20, 20, "red", "false");
|
|
|
HOperatorSet.SetDraw(winHandle, "margin");
|
|
|
HOperatorSet.SetColor(winHandle, "green");
|
|
|
hWindow_Final1.DrawModel = true;
|
|
|
HObject roiRegion;
|
|
|
HOperatorSet.GenEmptyObj(out roiRegion);
|
|
|
if(roiShape== ERoiShape.Rectange)
|
|
|
{
|
|
|
HOperatorSet.DrawRectangle1(winHandle, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2);
|
|
|
HOperatorSet.GenRectangle1(out roiRegion, row1, column1, row2, column2);
|
|
|
}
|
|
|
else if(roiShape== ERoiShape.Circle)
|
|
|
{
|
|
|
HOperatorSet.DrawCircle(winHandle, out HTuple row, out HTuple column, out HTuple radius);
|
|
|
HOperatorSet.GenCircle(out roiRegion, row, column, radius);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
HOperatorSet.DrawRegion(out roiRegion, winHandle);
|
|
|
}
|
|
|
|
|
|
if(isSearch)
|
|
|
{
|
|
|
pm.SearchRegion = roiRegion;
|
|
|
hWindow_Final1.DispObj(pm.SearchRegion, "green");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
pm.RoiRegion = roiRegion;
|
|
|
hWindow_Final1.DispObj(pm.RoiRegion, "green");
|
|
|
|
|
|
|
|
|
}
|
|
|
//pm.RoiRow1 = row1; pm.RoiRow2 = row2;
|
|
|
//pm.RoiColumn1 = column1; pm.RoiColumn2 = column2;
|
|
|
|
|
|
hWindow_Final1.DrawModel = false;
|
|
|
}
|
|
|
}
|
|
|
}
|