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.

118 lines
5.3 KiB
C#

using ChoiceTech.Halcon.Control;
using HalconDotNet;
using Rs.Framework;
using Rs.MotionPlat.Flow;
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.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace Rs.MotionPlat.SysConfig
{
public partial class DownCameraCalibration : BaseForm
{
public DownCameraCalibration()
{
InitializeComponent();
cameraTemplate1.ItemName = "下相机标定";
cameraTemplate1.SetFindShapeMode(EFindShapeMode.SelfDefine);
cameraTemplate1.FindShapeEvent += CameraTemplate1_FindShapeEvent;
}
private void CameraTemplate1_FindShapeEvent(ChoiceTech.Halcon.Control.HWindow_Final win, HObject sourceImage)
{
HOperatorSet.Threshold(sourceImage, out HObject Region, new HTuple(0), new HTuple(100));
HOperatorSet.Connection(Region, out HObject connectedRegion);
if (connectedRegion.IsInitialized() && connectedRegion.CountObj() > 0)
{
HOperatorSet.SelectShape(connectedRegion, out HObject selectedRegion, "area", "and", new HTuple(2000), new HTuple(3000));
if (selectedRegion.IsInitialized() && selectedRegion.CountObj() > 0)
{
HOperatorSet.SelectShape(selectedRegion, out HObject lastRegion, "circularity", "and", new HTuple(0.8), new HTuple(1));
if (lastRegion.IsInitialized() && lastRegion.CountObj() > 0)
{
HOperatorSet.SmallestCircle(lastRegion, out HTuple row, out HTuple column, out HTuple radius);
HOperatorSet.GenCrossContourXld(out HObject cross, row, column, new HTuple(20), new HTuple(0));
win.DispObj(cross, "green");
int rowIndex = dgvPixCalib.Rows.Add();
dgvPixCalib.Rows[rowIndex].Cells[0].Value = row.DArr[0].ToString("0.000");
dgvPixCalib.Rows[rowIndex].Cells[1].Value = column.DArr[0].ToString("0.000");
}
}
}
//HOperatorSet.GetImageSize(sourceImage, out HTuple imgWidth, out HTuple imgHeight);
//HOperatorSet.GenCrossContourXld(out HObject camCenter, imgHeight / 2, imgWidth / 2, Math.Max(imgHeight, imgWidth), 0);
//win.DispObj(camCenter);
//HOperatorSet.Threshold(sourceImage, out HObject regions, 40, 130);
//HOperatorSet.Connection(regions, out HObject connectedRegions);
//HOperatorSet.SelectShape(connectedRegions, out HObject selectedRegion, "circularity", "and", 0.8, 1);
//HOperatorSet.SelectShape(selectedRegion, out HObject selectedRegion1, "area", "and", 800, 1500);
////HOperatorSet.FillUp(selectedRegion, out HObject regionFillUp);
////win.DispObj(regionFillUp, "green");
//HOperatorSet.SmallestCircle(selectedRegion1, out HTuple row, out HTuple column, out HTuple radius);
//HOperatorSet.GenCrossContourXld(out HObject cross, row, column, 30, 0);
//HOperatorSet.GenCircle(out HObject cicle, row, column, radius);
//win.DispObj(cicle, "green");
//win.DispObj(cross, "green");
//File.AppendAllText("c:test.csv",$"camerX:{imgHeight / 2},camerX:{imgWidth / 2},row:{row},column:{column}\r\n");
}
private void DownCameraCalibration_FormClosing(object sender, FormClosingEventArgs e)
{
cameraTemplate1.RemoveGrabEvent();
}
private void btnGetMatchResult_Click(object sender, EventArgs e)
{
cameraTemplate1.GetMatchResult(out double row, out double column, out double angle, out double score);
int rowIndex = dgvPixCalib.Rows.Add();
dgvPixCalib.Rows[rowIndex].Cells[0].Value = row.ToString("0.000");
dgvPixCalib.Rows[rowIndex].Cells[1].Value = column.ToString("0.000");
dgvPixCalib.Rows[rowIndex].Cells[2].Value = angle.ToString("0.000");
dgvPixCalib.Rows[rowIndex].Cells[3].Value = score.ToString("0.000");
Msg.ShowInfo($"{row},{column},{angle},{score}");
}
private void DownCameraCalibration_Load(object sender, EventArgs e)
{
dgvRotateCenter.AutoGenerateColumns = false;
dgvPixCalib.AutoGenerateColumns = false;
}
private void btnMove_Click(object sender, EventArgs e)
{
AxisControl.LoadY.MoveOffset(double.Parse(txtMoveOffset.Text), 10);
}
private void btnCalcPix_Click(object sender, EventArgs e)
{
int count = dgvPixCalib.Rows.Count;
if(count<2)
{
Msg.ShowError("移动次数少于一次");
return;
}
double columnValY1 = double.Parse(dgvPixCalib.Rows[0].Cells[1].Value.ToString());
double columnValY2 = double.Parse(dgvPixCalib.Rows[1].Cells[1].Value.ToString());
double mmPerPix = (double.Parse(txtMoveOffset.Text) / (columnValY2 - columnValY1));
txtMmPerPixX.Text = mmPerPix.ToString("0.0000");
txtMmPerPixY.Text = mmPerPix.ToString("0.0000");
}
}
}