You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
22 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using ChoiceTech.Halcon.Control;
using HalconDotNet;
using Rs.Camera;
using Rs.Framework;
using Rs.MotionPlat.Commom;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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 partial class CameraTemplate : UserControl
{
ProductModel pm = new ProductModel();
HWindow winHandle = null;
HObject hImage = null;
HObject final_Region = new HObject();
public CameraTemplate()
{
InitializeComponent();
HikCamera.Instance.GrabOkEvent += Instance_GrabOkEvent;
winHandle = hWindow_Final1.hWindowControl.HalconWindow;
HOperatorSet.GenEmptyObj(out final_Region);
}
private void Instance_GrabOkEvent(int arg1, HObject arg2)
{
hImage = arg2;
hWindow_Final1.HobjectToHimage(hImage);
}
HObject ho_image;
private void btnLoadImage_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_image);
OpenFileDialog fileOpen=new OpenFileDialog();
DialogResult dr = fileOpen.ShowDialog();
if(dr == DialogResult.OK)
{
string filename = fileOpen.FileName;
HOperatorSet.ReadImage(out HObject img, 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(img);
ho_image = img;
pm.SourceImage = ho_image;
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);
}
}
private void btnGrab_Click(object sender, EventArgs e)
{
bGrabContinue = false;
HikCamera.Instance.Grab("MV-CA050-10GM (00C32922251)");
}
bool bGrabContinue = false;
private void btnGrabContinue_Click(object sender, EventArgs e)
{
bGrabContinue = true;
Task.Run(() => {
while(bGrabContinue)
{
HikCamera.Instance.Grab("MV-CA050-10GM (00C32922251)");
Thread.Sleep(200);
}
});
}
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(ho_image, out partImage, row1, column1, column2 - column1, row2 - row1);
HOperatorSet.ReduceDomain(ho_image,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);
HOperatorSet.ReduceDomain(pm.SourceImage, 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)
{
MessageBox.Show("请先绘制ROI区域");
return;
}
HOperatorSet.ClearWindow(hWindow_Final2.hWindowControl.HalconWindow);
HOperatorSet.ReduceDomain(pm.SourceImage, pm.FinalRegion, out HObject imageReduced);
HOperatorSet.InspectShapeModel(imageReduced, out HObject modelImages, out HObject modelRegions, 1, 30);
hWindow_Final1.DispObj(pm.SourceImage);
hWindow_Final1.DispObj(modelRegions, "green");
//HOperatorSet
HOperatorSet.SmallestRectangle1(pm.FinalRegion,out HTuple row1,out HTuple column1,out HTuple row2,out HTuple column2);
//HOperatorSet.SetPart(hWindow_Final2.hWindowControl.HalconWindow, pm.RoiRow1 - 20, pm.RoiColumn1 - 20, pm.RoiRow2 + 20, pm.RoiColumn2 + 20);
HOperatorSet.SetPart(hWindow_Final2.hWindowControl.HalconWindow, row1 - 20, column1 - 20, row2 + 20, column2 + 20);
HOperatorSet.DispObj(imageReduced, hWindow_Final2.hWindowControl.HalconWindow);
//HOperatorSet.CropPart(ho_image, out partImage, row1, column1, column2 - column1, row2 - row1);
}
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;
}
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.GenCircle(out brushRegion, 50, 50, 20);
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(pm.SourceImage, pm.RoiRegion, 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())
{
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, 120, "fill");
HOperatorSet.Compose3(image1, image1, pm.SourceImage, out resultImage);
HOperatorSet.SetSystem("flush_graphic", "false");
HOperatorSet.SetColor(winHandle, "green");
HOperatorSet.DispObj(resultImage, 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;
HOperatorSet.PaintRegion(removedRegion, pm.SourceImage, out img, 120, "fill");
HOperatorSet.Compose3(img, img, pm.SourceImage, out rltimg);
hWindow_Final1.DispObj(rltimg);
//hWindow_Final1.DispObj(pm.SourceImage);
//HOperatorSet.ClearWindow(winHandle);
HOperatorSet.ReduceDomain(pm.SourceImage, 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);
}
private void btnSaveModel_Click(object sender, EventArgs e)
{
pm.SaveModel();
}
private void btnTestModel_Click(object sender, EventArgs e)
{
//先加载模板
dataGridView1.Rows.Clear();
HOperatorSet.ReadShapeModel("Recipe/Test/m.shm", out HTuple modelID);
HOperatorSet.GetShapeModelContours(out HObject modelContours, modelID, 1);
HOperatorSet.FindShapeModel(pm.SourceImage, modelID, AngleTool.Deg2Rad(-10), AngleTool.Deg2Rad(200), 0.1, 1, 0.5, "least_squares", 0, 0.9, out HTuple row, out HTuple column, out HTuple angle, out HTuple score);
if(score!=null&&score.Length>0)
{
HOperatorSet.VectorAngleToRigid(0, 0, 0, row, column, angle, out HTuple homMat2D);
HOperatorSet.AffineTransContourXld(modelContours, out HObject contoursAffineTrans, homMat2D);
hWindow_Final1.DispObj(contoursAffineTrans, "green");
int len = score.Length;
for (int i = 0; i < len; i++)
{
double rVal = row.DArr[i];
double cVal = column.DArr[i];
double angleVal = angle.DArr[i];
double scoreVal = score.DArr[i];
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells[0].Value = rVal.ToString("0.000");
dataGridView1.Rows[rowIndex].Cells[1].Value = cVal.ToString("0.000");
dataGridView1.Rows[rowIndex].Cells[2].Value = angleVal.ToString("0.000");
dataGridView1.Rows[rowIndex].Cells[3].Value = scoreVal.ToString("0.000");
HOperatorSet.GenCrossContourXld(out HObject cross, rVal, cVal, 100, angle);
hWindow_Final1.DispObj(cross, "black");
}
}
else
{
Msg.ShowError("未定位到产品");
}
}
private void btnDrawAny_Click(object sender, EventArgs e)
{
DrawRoi(ERoiShape.Any);
}
private void DrawRoi(ERoiShape roiShape)
{
if(pm.SourceImage== null || pm.SourceImage.CountObj()<=0)
{
Msg.ShowError("请先拍照或者选择照片");
return;
}
hWindow_Final1.Focus();
HalconTool.SetDisplayFont(winHandle, 50, "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);
}
pm.RoiRegion = roiRegion;
//pm.RoiRow1 = row1; pm.RoiRow2 = row2;
//pm.RoiColumn1 = column1; pm.RoiColumn2 = column2;
hWindow_Final1.DispObj(pm.RoiRegion, "green");
hWindow_Final1.DrawModel = false;
}
}
}