|
|
using HalconDotNet;
|
|
|
using Rs.Framework;
|
|
|
using Rs.Motion;
|
|
|
using Rs.MotionPlat.Commom;
|
|
|
using Rs.MotionPlat;
|
|
|
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.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
using ChoiceTech.Halcon.Control;
|
|
|
|
|
|
namespace Rs.Controls
|
|
|
{
|
|
|
public partial class TemplateMatch : 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 m_savePath = "";
|
|
|
double matchResultRow = 0.0;
|
|
|
double matchResultColumn = 0.0;
|
|
|
double matchResultAngle = 0.0;
|
|
|
double matchResultScore = 0.0;
|
|
|
bool bGrab = false;
|
|
|
/// <summary>
|
|
|
/// 自定义拍照事件
|
|
|
/// </summary>
|
|
|
public event Func<HObject> CustomDefineGrabEvent;
|
|
|
public TemplateMatch()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
winHandle = hWindow_Final1.hWindowControl.HalconWindow;
|
|
|
HOperatorSet.GenEmptyObj(out final_Region);
|
|
|
}
|
|
|
|
|
|
public string ItemName { get; set; } = "Default";
|
|
|
public string RecipeName { get; set; } = "Default";
|
|
|
public void SetFindShapeMode(EFindShapeMode eFindShapeMode)
|
|
|
{
|
|
|
this.eFindShapeMode = eFindShapeMode;
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
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");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void btnGrab_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
if (CustomDefineGrabEvent != null)
|
|
|
{
|
|
|
bGrab = true;
|
|
|
HObject image = CustomDefineGrabEvent?.Invoke();
|
|
|
LoadImage(image);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
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;
|
|
|
HObject image = CustomDefineGrabEvent?.Invoke();
|
|
|
LoadImage(image);
|
|
|
Thread.Sleep(300);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protected override void OnVisibleChanged(EventArgs e)
|
|
|
{
|
|
|
base.OnVisibleChanged(e);
|
|
|
}
|
|
|
|
|
|
private void LoadImage(HObject image)
|
|
|
{
|
|
|
if(image!=null)
|
|
|
{
|
|
|
hImage = image;
|
|
|
hWindow_Final1.ClearWindow();
|
|
|
if (winHandle.IsInitialized())
|
|
|
{
|
|
|
HOperatorSet.ClearWindow(winHandle);
|
|
|
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);
|
|
|
if (eFindShapeMode == EFindShapeMode.SelfDefine)
|
|
|
{
|
|
|
FindShapeEvent?.Invoke(hWindow_Final1, hImage);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
HObject imageReduced, partImage;
|
|
|
private void btnCreateModel_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
hWindow_Final1.Focus();
|
|
|
hWindow_Final1.DrawModel = true;
|
|
|
|
|
|
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, ERegionType.Roi);
|
|
|
}
|
|
|
|
|
|
private void btnDrawCircle_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Circle, ERegionType.Roi);
|
|
|
}
|
|
|
|
|
|
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.Exposure=int.Parse(txtExposure.Text);
|
|
|
pm.Gain = int.Parse(txtGain.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");
|
|
|
FindShapeEvent?.Invoke(hWindow_Final1, hImage);
|
|
|
}
|
|
|
}
|
|
|
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, ERegionType.Roi);
|
|
|
}
|
|
|
|
|
|
#region 加载
|
|
|
private void TemplateMatch_Load(object sender, EventArgs e)
|
|
|
{
|
|
|
if (!DesignMode)
|
|
|
{
|
|
|
if (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN")
|
|
|
{
|
|
|
ControlLanguageHelper.SetDefaultLanguage("zh-CN");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ControlLanguageHelper.SetDefaultLanguage("en-US");
|
|
|
}
|
|
|
ControlLanguageHelper.LoadLanguage(this, this.GetType());
|
|
|
m_savePath = Path.Combine("Recipe",$"{RecipeName}", ItemName);
|
|
|
//当窗口加载时,检测是否已经创建了模板,如果已经创建了,则加载模板
|
|
|
string filename = Path.Combine("Recipe", $"{RecipeName}", 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");
|
|
|
string exposure = ini.ReadValue("ModelParam", "Exposure");
|
|
|
string gain = ini.ReadValue("ModelParam", "Gain");
|
|
|
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;
|
|
|
txtExposure.Text = exposure;
|
|
|
txtGain.Text = gain;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
private void btnDrawSearch_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Rectange, ERegionType.Search);
|
|
|
}
|
|
|
|
|
|
|
|
|
private void rbtnSearchSelectedRegion_CheckedChanged(object sender, EventArgs e)
|
|
|
{
|
|
|
if (rbtnSearchSelectedRegion.Checked)
|
|
|
{
|
|
|
btnDrawSearch.Visible = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
btnDrawSearch.Visible = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void btnDrawBarCode_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DrawRoi(ERoiShape.Rectange, ERegionType.Barcode);
|
|
|
}
|
|
|
|
|
|
private void DrawRoi(ERoiShape roiShape, ERegionType regionType)
|
|
|
{
|
|
|
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);
|
|
|
}
|
|
|
switch (regionType)
|
|
|
{
|
|
|
case ERegionType.Roi:
|
|
|
pm.RoiRegion = roiRegion;
|
|
|
hWindow_Final1.DispObj(pm.RoiRegion, "green");
|
|
|
break;
|
|
|
case ERegionType.Search:
|
|
|
pm.SearchRegion = roiRegion;
|
|
|
hWindow_Final1.DispObj(pm.SearchRegion, "green");
|
|
|
break;
|
|
|
case ERegionType.Barcode:
|
|
|
pm.BarCodeRegion = roiRegion;
|
|
|
hWindow_Final1.DispObj(pm.BarCodeRegion, "green");
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
//pm.RoiRow1 = row1; pm.RoiRow2 = row2;
|
|
|
//pm.RoiColumn1 = column1; pm.RoiColumn2 = column2;
|
|
|
|
|
|
hWindow_Final1.DrawModel = false;
|
|
|
}
|
|
|
|
|
|
public int GetExposure()
|
|
|
{
|
|
|
if(int.TryParse(txtExposure.Text,out int value))
|
|
|
return value;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
public int GetGain()
|
|
|
{
|
|
|
if(int.TryParse((txtGain.Text),out int value))
|
|
|
return value;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
public HObject GetImage()
|
|
|
{
|
|
|
return hImage;
|
|
|
}
|
|
|
|
|
|
public DataGridViewRowCollection GetResultRows()
|
|
|
{
|
|
|
return this.dataGridView1.Rows;
|
|
|
}
|
|
|
}
|
|
|
}
|