using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using System.Threading;
namespace come
{
public partial class Form1 : Form
{
// Local iconic variables
HObject ho_Image1 = null, ho_Image = null, ho_EdgeAmplitude = null;
HObject ho_Regions = null, ho_RegionClosing = null, ho_ConnectedRegions = null;
HObject ho_SelectedRegions = null, ho_RegionTrans = null, ho_ImageReduced = null;
// Local control variables
HTuple hv_Protocol = new HTuple(), hv_Timeout = new HTuple();
HTuple hv_TmplMean = new HTuple(), hv_SendStr = new HTuple();
HTuple hv_AcqHandle = new HTuple(), hv_Socket = new HTuple();
HTuple hv_Address = new HTuple(), hv_To = new HTuple();
HTuple hv_Answer = new HTuple(), hv_From = new HTuple();
HTuple hv_Mean = new HTuple(), hv_Deviation = new HTuple();
HTuple m_hv_HWindowID = new HTuple();
//自适应窗口变量
HTuple row1, column1, row2, column2;
Thread imageStartThread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image1);
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_EdgeAmplitude);
HOperatorSet.GenEmptyObj(out ho_Regions);
HOperatorSet.GenEmptyObj(out ho_RegionClosing);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
HOperatorSet.GenEmptyObj(out ho_RegionTrans);
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
}
private void btnStart_Click(object sender, EventArgs e)
{
imageStartThread = new Thread(ImageStart);
imageStartThread.Start();
}
void ImageStart() {
//Initialize program
hv_Protocol.Dispose();
hv_Protocol = "TCP4";
hv_Timeout.Dispose();
hv_Timeout = -1;
hv_TmplMean.Dispose();
hv_TmplMean = 30;
hv_SendStr.Dispose();
hv_SendStr = "NG";
//Image Acquisition 01: Code generated by Image Acquisition 01
hv_AcqHandle.Dispose();
try
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] ", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
//Open a socket connection
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_Socket.Dispose();
HOperatorSet.OpenSocketConnect("127.0.0.1", 9001, (new HTuple("protocol")).TupleConcat(
"timeout"), hv_Protocol.TupleConcat(hv_Timeout), out hv_Socket);
}
hv_Address.Dispose();
HOperatorSet.GetSocketParam(hv_Socket, "address_info", out hv_Address);
//
//To is not evaluated for TCP and connected UDP sockets
hv_To.Dispose();
hv_To = new HTuple();
//
//send_data (Socket, 'cz-csi', [1,'Generic sockets are great!',-1,-2,-3], To)
while ((int)(1) != 0)
{
hv_Answer.Dispose(); hv_From.Dispose();
HOperatorSet.ReceiveData(hv_Socket, (new HTuple("c")).TupleConcat("z"), out hv_Answer,
out hv_From);
if ((int)(new HTuple(hv_Answer.TupleEqual("mc"))) != 0)
{
ho_Image1.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image1, hv_AcqHandle, -1);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, "E:/AAAA项目程序及文档/C111/海康威视飞拍实验/Image_20181105113611715.bmp");
ReSizePic();
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_Image1, hSmartWindowControl1.HalconWindow);
ImageProcess();
HOperatorSet.SendData(hv_Socket, "A3", hv_SendStr, hv_To);
}
}
}
catch (Exception err)
{
//MessageBox.Show(err.ToString());
//throw;
HOperatorSet.CloseSocket(hv_Socket);
}
finally {
HOperatorSet.CloseSocket(hv_Socket);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
// stop(...); only in hdevelop
HOperatorSet.CloseSocket(hv_Socket);
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
imageStartThread.Abort();
ho_Image1.Dispose();
ho_Image.Dispose();
ho_EdgeAmplitude.Dispose();
ho_Regions.Dispose();
ho_RegionClosing.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
ho_RegionTrans.Dispose();
ho_ImageReduced.Dispose();
hv_Protocol.Dispose();
hv_Timeout.Dispose();
hv_TmplMean.Dispose();
hv_SendStr.Dispose();
hv_AcqHandle.Dispose();
hv_Socket.Dispose();
hv_Address.Dispose();
hv_To.Dispose();
hv_Answer.Dispose();
hv_From.Dispose();
hv_Mean.Dispose();
hv_Deviation.Dispose();
}
public void ReSizePic() {
HTuple width, height;
HOperatorSet.GetImageSize(ho_Image, out width, out height);
double ratioWidth = (1.0) * width[0].I / hWindowControl1.Width;
double ratioHeight = (1.0) * height[0].I / hWindowControl1.Height;
if (ratioWidth >= ratioHeight)
{
row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
column1 = 0;
row2 = row1 + hWindowControl1.Height * ratioWidth;
column2 = column1 + hWindowControl1.Width * ratioWidth;
}
else
{
row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
column1 = 0;
row2 = row1 + hWindowControl1.Height * ratioWidth;
column2 = column1 + hWindowControl1.Width * ratioWidth;
}
}
//图像处理
public void ImageProcess()
{
ho_EdgeAmplitude.Dispose();
HOperatorSet.SobelAmp(ho_Image, out ho_EdgeAmplitude, "sum_abs", 3);
ho_Regions.Dispose();
HOperatorSet.Threshold(ho_EdgeAmplitude, out ho_Regions, 23, 40);
ho_RegionClosing.Dispose();
HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 9);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions);
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("area")).TupleConcat(
"width"), "and", (new HTuple(8256.03)).TupleConcat(0), (new HTuple(14378.5)).TupleConcat(
193.58));
ho_RegionTrans.Dispose();
HOperatorSet.ShapeTrans(ho_SelectedRegions, out ho_RegionTrans, "rectangle1");
ho_ImageReduced.Dispose();
HOperatorSet.ReduceDomain(ho_Image, ho_RegionTrans, out ho_ImageReduced);
hv_Mean.Dispose(); hv_Deviation.Dispose();
HOperatorSet.Intensity(ho_RegionTrans, ho_ImageReduced, out hv_Mean, out hv_Deviation);
if ((int)(new HTuple((new HTuple(((hv_TmplMean - hv_Mean)).TupleLength())).TupleLess(
10))) != 0)
{
hv_SendStr.Dispose();
hv_SendStr = "OK";
}else {
hv_SendStr.Dispose();
hv_SendStr = "NG";
}
}
}
}