using HalconDotNet;
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 static System.Windows.Forms.MonthCalendar;
namespace WeldLineDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//添加 Detectionbutton1是否按下的判断
//bool Detectionbutton1State = false;
// Local iconic variables
HObject ho_Image = null, ho_ImageR = null, ho_ImageG = null;
HObject ho_ImageB = null, ho_GrayImage = null, ho_EdgeAmplitude = null;
HObject ho_ImageResult1 = null, ho_ImageResult2 = null, ho_ImageResult3 = null;
HObject ho_ImageScaled1 = null, ho_ROI_0 = null, ho_ImageReduced = null;
HObject ho_ImageMean = null, ho_RegionDynThresh = null, ho_ConnectedRegions = null;
HObject ho_RegionFillUp = null, ho_RegionOpening = null, ho_RegionUnion = null;
HObject ho_RegionClosing = null, ho_ConnectedRegions2 = null;
HObject ho_SelectedRegions = null, ho_SelectedRegions3 = null;
HObject ho_RegionUnion1 = null, ho_ImageReduced1 = null, ho_Region = null;
HObject ho_ConnectedRegions3 = null, ho_SelectedRegions2 = null;
HObject ho_RegionDilation = null, ho_RegionIntersection1 = null;
HObject ho_ConnectedRegions1 = null, ho_SelectedRegions1 = null;
private void label2_Click(object sender, EventArgs e)
{
}
HObject ho_RegionUnion2 = null, ho_RegionTrans1 = null;
// Local control variables
HTuple hv_Files = new HTuple(), hv_ImageFiles = new HTuple();
HTuple hv_I = new HTuple(), hv_Width = new HTuple(), hv_Height = new HTuple();
HTuple hv_WindowHandle = new HTuple(), hv_Min = new HTuple();
HTuple hv_Max = new HTuple(), hv_Range = new HTuple();
HTuple hv_WeldArea = new HTuple(), hv_WeldRow = new HTuple();
HTuple hv_WeldColumn = new HTuple(), hv_text = new HTuple();
HTuple hv_color = new HTuple();
HTuple hv_Selection = new HTuple();
HTuple hv_AcqHandle = new HTuple();
HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
HTuple hv_MSecond = new HTuple();
HTuple hv_Second = new HTuple(), hv_Minute = new HTuple();
HTuple hv_Hour = new HTuple(), hv_Day = new HTuple(), hv_YDay = new HTuple();
HTuple hv_Month = new HTuple(), hv_Year = new HTuple();
//函数原型
// Chapter: Graphics / Text
// Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen.
public void disp_continue_message(HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
HTuple hv_ContinueMessage = new HTuple();
HTuple hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
// Initialize local and output iconic variables
//This procedure displays 'Press Run (F5) to continue' in the
//lower right corner of the screen.
//It uses the procedure disp_message.
//
//Input parameters:
//WindowHandle: The window, where the text shall be displayed
//Color: defines the text color.
// If set to '' or 'auto', the currently set color is used.
//Box: If set to 'true', the text is displayed in a box.
//
//Convert the parameter Box to generic parameters.
hv_GenParamName.Dispose();
hv_GenParamName = new HTuple();
hv_GenParamValue.Dispose();
hv_GenParamValue = new HTuple();
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
{
//Display no box
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual("true"))) != 0)
{
//Set a color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(0));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
{
//Display no shadow.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual("true"))) != 0)
{
//Set a shadow color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(1));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
//
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
{
//disp_text does not accept an empty string for Color.
hv_Color_COPY_INP_TMP.Dispose();
hv_Color_COPY_INP_TMP = new HTuple();
}
//
//Display the message.
hv_ContinueMessage.Dispose();
hv_ContinueMessage = "Press Run (F5) to continue";
HOperatorSet.DispText(hWindowControl1.HalconWindow, hv_ContinueMessage, "window", "bottom",
"right", hv_Color_COPY_INP_TMP, hv_GenParamName, hv_GenParamValue);
hv_Color_COPY_INP_TMP.Dispose();
hv_GenParamName.Dispose();
hv_GenParamValue.Dispose();
hv_ContinueMessage.Dispose();
return;
}
// Chapter: Graphics / Text
// Short Description: This procedure writes one or multiple text messages.
public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
HTuple hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
HTuple hv_Column_COPY_INP_TMP = new HTuple(hv_Column);
HTuple hv_CoordSystem_COPY_INP_TMP = new HTuple(hv_CoordSystem);
HTuple hv_Row_COPY_INP_TMP = new HTuple(hv_Row);
// Initialize local and output iconic variables
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed.
//String: A tuple of strings containing the text messages to be displayed.
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position.
// You can pass a single value or a tuple of values.
// See the explanation below.
// Default: 12.
//Column: The column coordinate of the desired text position.
// You can pass a single value or a tuple of values.
// See the explanation below.
// Default: 12.
//Color: defines the color of the text as string.
// If set to [] or '' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically
// for every text position defined by Row and Column,
// or every new text line in case of |Row| == |Column| == 1.
//Box: A tuple controlling a possible box surrounding the text.
// Its entries:
// - Box[0]: Controls the box and its color. Possible values:
// -- 'true' (Default): An orange box is displayed.
// -- 'false': No box is displayed.
// -- color string: A box is displayed in the given color, e.g., 'white', '#FF00CC'.
// - Box[1] (Optional): Controls the shadow of the box. Possible values:
// -- 'true' (Default): A shadow is displayed in
// darker orange if Box[0] is not a color and in 'white' otherwise.
// -- 'false': No shadow is displayed.
// -- color string: A shadow is displayed in the given color, e.g., 'white', '#FF00CC'.
//
//It is possible to display multiple text strings in a single call.
//In this case, some restrictions apply on the
//parameters String, Row, and Column:
//They can only have either 1 entry or n entries.
//Behavior in the different cases:
// - Multiple text positions are specified, i.e.,
// - |Row| == n, |Column| == n
// - |Row| == n, |Column| == 1
// - |Row| == 1, |Column| == n
// In this case we distinguish:
// - |String| == n: Each element of String is displayed
// at the corresponding position.
// - |String| == 1: String is displayed n times
// at the corresponding positions.
// - Exactly one text position is specified,
// i.e., |Row| == |Column| == 1:
// Each element of String is display in a new textline.
//
//
//Convert the parameters for disp_text.
if ((int)((new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(new HTuple())))) != 0)
{
hv_Color_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP.Dispose();
hv_GenParamName.Dispose();
hv_GenParamValue.Dispose();
return;
}
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP = 12;
}
//
//Convert the parameter Box to generic parameters.
hv_GenParamName.Dispose();
hv_GenParamName = new HTuple();
hv_GenParamValue.Dispose();
hv_GenParamValue = new HTuple();
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
{
//Display no box
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual("true"))) != 0)
{
//Set a color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(0));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
{
//Display no shadow.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual("true"))) != 0)
{
//Set a shadow color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(1));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
//Restore default CoordSystem behavior.
if ((int)(new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual("window"))) != 0)
{
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP = "image";
}
//
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
{
//disp_text does not accept an empty string for Color.
hv_Color_COPY_INP_TMP.Dispose();
hv_Color_COPY_INP_TMP = new HTuple();
}
//
HOperatorSet.DispText(hWindowControl1.HalconWindow, hv_String, hv_CoordSystem_COPY_INP_TMP,
hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP, hv_Color_COPY_INP_TMP, hv_GenParamName,
hv_GenParamValue);
hv_Color_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP.Dispose();
hv_GenParamName.Dispose();
hv_GenParamValue.Dispose();
return;
}
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
public void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
{
// Local iconic variables
// Local control variables
HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
HTuple hv_ResizeFactor = new HTuple(), hv_ImageWidth = new HTuple();
HTuple hv_ImageHeight = new HTuple(), hv_TempWidth = new HTuple();
HTuple hv_TempHeight = new HTuple(), hv_WindowWidth = new HTuple();
HTuple hv_WindowHeight = new HTuple();
// Initialize local and output iconic variables
hv_WindowHandle = new HTuple();
//This procedure opens a new graphics window and adjusts the size
//such that it fits into the limits specified by WidthLimit
//and HeightLimit, but also maintains the correct image aspect ratio.
//
//If it is impossible to match the minimum and maximum extent requirements
//at the same time (f.e. if the image is very long but narrow),
//the maximum value gets a higher priority,
//
//Parse input tuple WidthLimit
if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr(
new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
{
hv_MinWidth.Dispose();
hv_MinWidth = 500;
hv_MaxWidth.Dispose();
hv_MaxWidth = 800;
}
else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
1))) != 0)
{
hv_MinWidth.Dispose();
hv_MinWidth = 0;
hv_MaxWidth.Dispose();
hv_MaxWidth = new HTuple(hv_WidthLimit);
}
else
{
hv_MinWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MinWidth = hv_WidthLimit.TupleSelect(
0);
}
hv_MaxWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MaxWidth = hv_WidthLimit.TupleSelect(
1);
}
}
//Parse input tuple HeightLimit
if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr(
new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
{
hv_MinHeight.Dispose();
hv_MinHeight = 400;
hv_MaxHeight.Dispose();
hv_MaxHeight = 600;
}
else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
1))) != 0)
{
hv_MinHeight.Dispose();
hv_MinHeight = 0;
hv_MaxHeight.Dispose();
hv_MaxHeight = new HTuple(hv_HeightLimit);
}
else
{
hv_MinHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MinHeight = hv_HeightLimit.TupleSelect(
0);
}
hv_MaxHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MaxHeight = hv_HeightLimit.TupleSelect(
1);
}
}
//
//Test, if window size has to be changed.
hv_ResizeFactor.Dispose();
hv_ResizeFactor = 1;
hv_ImageWidth.Dispose(); hv_ImageHeight.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);
//First, expand window to the minimum extents (if necessary).
if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
hv_ImageHeight)))) != 0)
{
hv_ResizeFactor.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_ResizeFactor = (((((hv_MinWidth.TupleReal()
) / hv_ImageWidth)).TupleConcat((hv_MinHeight.TupleReal()) / hv_ImageHeight))).TupleMax();
}
}
hv_TempWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_TempWidth = hv_ImageWidth * hv_ResizeFactor;
}
hv_TempHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_TempHeight = hv_ImageHeight * hv_ResizeFactor;
}
//Then, shrink window to maximum extents (if necessary).
if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
hv_TempHeight)))) != 0)
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()
) / hv_TempWidth)).TupleConcat((hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin()
);
hv_ResizeFactor.Dispose();
hv_ResizeFactor = ExpTmpLocalVar_ResizeFactor;
}
}
}
hv_WindowWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_WindowWidth = hv_ImageWidth * hv_ResizeFactor;
}
hv_WindowHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_WindowHeight = hv_ImageHeight * hv_ResizeFactor;
}
//Resize window
HOperatorSet.SetWindowAttr("background_color", "black");
HOperatorSet.OpenWindow(hv_Row, hv_Column, hv_WindowWidth, hv_WindowHeight, 0, "visible", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
if (HDevWindowStack.IsOpen())
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_ImageHeight - 1, hv_ImageWidth - 1);
}
}
hv_MinWidth.Dispose();
hv_MaxWidth.Dispose();
hv_MinHeight.Dispose();
hv_MaxHeight.Dispose();
hv_ResizeFactor.Dispose();
hv_ImageWidth.Dispose();
hv_ImageHeight.Dispose();
hv_TempWidth.Dispose();
hv_TempHeight.Dispose();
hv_WindowWidth.Dispose();
hv_WindowHeight.Dispose();
return;
}
//private int MV_CC_SetEnumValue_NET(string v1, int v2)//+++设置高电平
//{
// throw new NotImplementedException();
//}
//int nRet = 1;//++初始化读图信号,用来判断停止采图
//《读取图像》///
private void ReadImage_Click(object sender, EventArgs e)
{
//类的实例化
HOperatorSet.GenEmptyObj(out ho_Image);
//外焊缝检测
//读取图片
// 3.实时读图
//Image Acquisition 01: Code generated by Image Acquisition 01
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("MVision", 1, 1, 0, 0, 0, 0, "progressive", 8,
"default", -1, "false", "auto", "GEV:DA0849903 MV-CU013-A0GC", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
//Image Acquisition 03: Code generated by Image Acquisition 03
//hv_AcqHandle.Dispose();
//HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive",
// -1, "default", -1, "false", "default", "34bd202941b3_GEV_MVCU013A0GC", 0,
// -1, out hv_AcqHandle);
// HOperatorSet.OpenFramegrabber("MVision", 0, 0, 0, 0, 0, 0, "progressive",
//-1, "default", -1, "default", "default", "34bd202941b3_GEV_MVCU013A0GC",
//0, -1, out hv_AcqHandle);
// HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
while ((int)(1) != 0)
// while ((int)(nRet) != 0)//++
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 03: Do something
添加代码++
// //1.遍历文件夹
// hv_Files.Dispose();
//HOperatorSet.ListFiles("D:/D程序/外焊缝方案/不锈钢管 - 记号笔圆 - 外/曝光时间-30000",
// "files", out hv_Files);
文件格式筛选
//hv_ImageFiles.Dispose();
//HOperatorSet.TupleRegexpSelect(hv_Files, ".*", out hv_ImageFiles);
依次读取图片
//for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_ImageFiles.TupleLength())) - 1); hv_I = (int)hv_I + 1)
//{
//using (HDevDisposeHelper dh = new HDevDisposeHelper())
//{
// //ho_Image.Dispose();
// HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_I));
//}
hv_Width.Dispose(); hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);//获取图像的宽高
//显示焊缝所在区域
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height, hv_Width);//完整显示图像++
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
判断一个按钮是否按下
//if(Detectionbutton1State)
//{
// //调用《检测焊缝按钮》++
// object se = new object();//++
// EventArgs args = new EventArgs();//++
// Detectionbutton1_Click(se, args);//++
//}
//判断一个选择框是否选择
//判断是否有图片
//if (hv_Width > 0)
//{
//调用《检测焊缝按钮》++
object se = new object();//++
EventArgs args = new EventArgs();//++
//判断打开焊缝识别选择框是否选择
if (checkBox1.CheckState == CheckState.Checked)
{
object se = new object();//++
EventArgs args = new EventArgs();//++
checkBox2_CheckedstateChanged(se, args);
}
else if (checkBox1.CheckState == CheckState.Unchecked)
{
HOperatorSet.WaitSeconds(2);//延时显示++
MessageBox.Show("焊缝识别未打开\n" + checkBox1.Text);
}
HOperatorSet.WaitSeconds(1);//延时显示++
disp_continue_message(hWindowControl1.HalconWindow, "black", "true");
//判断保存图像选择框是否选择
if(checkBox2.CheckState == CheckState.Checked)
{
object se =new object();//++
EventArgs args =new EventArgs();
checkBox2_CheckedChanged(se, args);
}
else if(checkBox2.CheckState == CheckState.Unchecked)
{
MessageBox.Show("存图未打开\n" + checkBox1.Text);
}
}
实时读图检测
//HOperatorSet.CloseFramegrabber(hv_AcqHandle);
}
//
private void checkBox2_CheckedstateChanged(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_ImageR);
HOperatorSet.GenEmptyObj(out ho_ImageG);
HOperatorSet.GenEmptyObj(out ho_ImageB);
HOperatorSet.GenEmptyObj(out ho_GrayImage);
HOperatorSet.GenEmptyObj(out ho_EdgeAmplitude);
HOperatorSet.GenEmptyObj(out ho_ImageResult1);
HOperatorSet.GenEmptyObj(out ho_ImageResult2);
HOperatorSet.GenEmptyObj(out ho_ImageResult3);
HOperatorSet.GenEmptyObj(out ho_ImageScaled1);
HOperatorSet.GenEmptyObj(out ho_ROI_0);
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
HOperatorSet.GenEmptyObj(out ho_ImageMean);
HOperatorSet.GenEmptyObj(out ho_RegionDynThresh);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
HOperatorSet.GenEmptyObj(out ho_RegionOpening);
HOperatorSet.GenEmptyObj(out ho_RegionUnion);
HOperatorSet.GenEmptyObj(out ho_RegionClosing);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions3);
HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
HOperatorSet.GenEmptyObj(out ho_Region);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
HOperatorSet.GenEmptyObj(out ho_RegionDilation);
HOperatorSet.GenEmptyObj(out ho_RegionIntersection1);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
HOperatorSet.GenEmptyObj(out ho_RegionTrans1);
HOperatorSet.GenEmptyObj(out ho_ROI_0);
//防止对象为空
if (hv_Width > 0)
{
hv_WindowHandle.Dispose();
//ho_ImageR.Dispose(); ho_ImageG.Dispose(); ho_ImageB.Dispose();
HOperatorSet.Decompose3(ho_Image, out ho_ImageR, out ho_ImageG, out ho_ImageB
);
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
ho_EdgeAmplitude.Dispose();
HOperatorSet.SobelAmp(ho_GrayImage, out ho_EdgeAmplitude, "sum_abs", 3);
ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose();
HOperatorSet.TransFromRgb(ho_ImageR, ho_ImageG, ho_ImageB, out ho_ImageResult1,
out ho_ImageResult2, out ho_ImageResult3, "hsv");
ho_ImageScaled1.Dispose();
HOperatorSet.ScaleImage(ho_GrayImage, out ho_ImageScaled1, 1, 0);
//手动画记号笔位置
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.GenRectangle2(out ho_ROI_0, 526.5, 284.5, (new HTuple(0.962864)).TupleRad()
, 238.034, 118.017 + 10);
}
ho_ImageReduced.Dispose();
HOperatorSet.ReduceDomain(ho_ImageScaled1, ho_ROI_0, out ho_ImageReduced);
//显示焊缝所在区域
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_ROI_0, hWindowControl1.HalconWindow);
//分割焊缝区域方法1 动态阈值
ho_ImageMean.Dispose();
HOperatorSet.MeanImage(ho_ImageReduced, out ho_ImageMean, 9, 9);
ho_RegionDynThresh.Dispose();
HOperatorSet.DynThreshold(ho_ImageReduced, ho_ImageMean, out ho_RegionDynThresh,
10, "dark");
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_RegionDynThresh, out ho_ConnectedRegions);
ho_RegionFillUp.Dispose();
HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp);
//去除记号笔外的干扰
ho_RegionOpening.Dispose();
HOperatorSet.OpeningCircle(ho_RegionFillUp, out ho_RegionOpening, 1.5);
ho_RegionUnion.Dispose();
HOperatorSet.Union1(ho_RegionOpening, out ho_RegionUnion);
ho_RegionClosing.Dispose();
HOperatorSet.ClosingRectangle1(ho_RegionUnion, out ho_RegionClosing, 1.5, 1);
//去除大黑点的干扰
ho_ConnectedRegions2.Dispose();
HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions2);
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions, "rb",
"and", 1, 10);
ho_SelectedRegions3.Dispose();
HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions3, "area",
"and", 150, 99999);
ho_RegionUnion1.Dispose();
HOperatorSet.Union1(ho_SelectedRegions3, out ho_RegionUnion1);
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
//HOperatorSet.DispObj(ho_GrayImage, hWindowControl1.HalconWindow);
//HOperatorSet.DispObj(ho_RegionUnion1, hWindowControl1.HalconWindow);
// stop(...); only in hdevelop
//提取焊缝方法2
//去除不锈钢管表面黑色部分的干扰
ho_ImageReduced1.Dispose();
HOperatorSet.ReduceDomain(ho_GrayImage, ho_ROI_0, out ho_ImageReduced1);
//binary_threshold (ImageReduced1, Region1, 'max_separability', 'dark', UsedThreshold)
hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
HOperatorSet.MinMaxGray(ho_RegionUnion1, ho_ImageReduced1, 6, out hv_Min, out hv_Max,
out hv_Range);
// stop(...); only in hdevelop
// 判断分割焊缝方案1得出的区域RegionUnion1是否为空
hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose();
HOperatorSet.AreaCenter(ho_RegionUnion1, out hv_Area, out hv_Row, out hv_Column);
if ((int)(new HTuple(hv_Area.TupleGreater(0))) != 0)
{//***
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
ho_Region.Dispose();
HOperatorSet.Threshold(ho_ImageReduced1, out ho_Region, 0, (new HTuple(75)).TupleMin2(
hv_Min));
}
closing_rectangle1 (Region, RegionClosing1, 10-9, 1)//补偿太亮时分割区域过小,太大干扰多
ho_ConnectedRegions3.Dispose();
HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions3);
ho_SelectedRegions2.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions3, out ho_SelectedRegions2, "area",
"and", 150, 99999);
ho_RegionDilation.Dispose();
HOperatorSet.DilationRectangle1(ho_SelectedRegions2, out ho_RegionDilation,
350, 10);
//binary_threshold (ImageReduced1, Region1, 'max_separability', 'dark', UsedThreshold)
ho_RegionIntersection1.Dispose();
HOperatorSet.Intersection(ho_RegionUnion1, ho_RegionDilation, out ho_RegionIntersection1
);
ho_ConnectedRegions1.Dispose();
HOperatorSet.Connection(ho_RegionIntersection1, out ho_ConnectedRegions1);
ho_SelectedRegions1.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
"and", 200, 99999);
//去除大黑点的干扰
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions, "rb",
"and", 1, 10);
ho_RegionUnion2.Dispose();
HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion2);
//读取检测区域
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "red"); //+++
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
HOperatorSet.DispObj(ho_GrayImage, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_RegionUnion2, hWindowControl1.HalconWindow);
// stop(...); only in hdevelop
ho_RegionTrans1.Dispose();
HOperatorSet.ShapeTrans(ho_RegionUnion2, out ho_RegionTrans1, "rectangle2");
//判断焊缝是否在检测区域
hv_WeldArea.Dispose(); hv_WeldRow.Dispose(); hv_WeldColumn.Dispose();
HOperatorSet.AreaCenter(ho_RegionTrans1, out hv_WeldArea, out hv_WeldRow, out hv_WeldColumn);
if ((int)(new HTuple(hv_WeldArea.TupleGreater(0))) != 0)
{
hv_text.Dispose();
hv_text = "存在焊缝NG";
hv_color.Dispose();
hv_color = "red";
//HOperatorSet.CloseFramegrabber(hv_AcqHandle);//+++存在焊缝停止读图
//停止读图
//int nRet = 1;//++
// nRet = 0;
/// nRet = MV_CC_SetEnumValue_NET("TriggerActivation", 2);//0:Rising Edge 1:Falling Edge 2:LevelHigh 3:LevelLow+++
// nRet = m_MyCamera.MV_CC_SetBoolValue_NET("LineInverter", true);//电平反转,也可用作IO输出,连续两次翻转可以输出一个IO信号
}
else
{
hv_text.Dispose();
hv_text = "没有焊缝OK";
hv_color.Dispose();
hv_color = "green";
}
//显示
HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "red"); //+++
HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_RegionTrans1, hWindowControl1.HalconWindow);
disp_message(hWindowControl1.HalconWindow, hv_text, "window", 30, 30, hv_color, "true");
}
else
{
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
disp_continue_message(hv_WindowHandle, "black", "true");
disp_continue_message(hWindowControl1.HalconWindow, "black", "true");
//stop ()
//* endfor
//ho_Image.Dispose();
ho_ImageR.Dispose();
ho_ImageG.Dispose();
ho_ImageB.Dispose();
ho_GrayImage.Dispose();
ho_EdgeAmplitude.Dispose();
ho_ImageResult1.Dispose();
ho_ImageResult2.Dispose();
ho_ImageResult3.Dispose();
ho_ImageScaled1.Dispose();
ho_ROI_0.Dispose();
ho_ImageReduced.Dispose();
ho_ImageMean.Dispose();
ho_RegionDynThresh.Dispose();
ho_ConnectedRegions.Dispose();
ho_RegionFillUp.Dispose();
ho_RegionOpening.Dispose();
ho_RegionUnion.Dispose();
ho_RegionClosing.Dispose();
ho_ConnectedRegions2.Dispose();
ho_SelectedRegions.Dispose();
ho_SelectedRegions3.Dispose();
ho_RegionUnion1.Dispose();
ho_ImageReduced1.Dispose();
ho_Region.Dispose();
ho_ConnectedRegions3.Dispose();
ho_SelectedRegions2.Dispose();
ho_RegionDilation.Dispose();
ho_RegionIntersection1.Dispose();
ho_ConnectedRegions1.Dispose();
ho_SelectedRegions1.Dispose();
ho_RegionUnion2.Dispose();
ho_RegionTrans1.Dispose();
hv_Selection.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
hv_WindowHandle.Dispose();
hv_Min.Dispose();
hv_Max.Dispose();
hv_Range.Dispose();
hv_WeldArea.Dispose();
hv_WeldRow.Dispose();
hv_WeldColumn.Dispose();
hv_text.Dispose();
hv_color.Dispose();
}
//stop ()
//**
}
}
//开启焊缝识别《开启焊缝识别》///
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
//《保存图像》
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
//** 存图
hv_MSecond.Dispose(); hv_Second.Dispose(); hv_Minute.Dispose(); hv_Hour.Dispose(); hv_Day.Dispose(); hv_YDay.Dispose(); hv_Month.Dispose(); hv_Year.Dispose();
HOperatorSet.GetSystemTime(out hv_MSecond, out hv_Second, out hv_Minute, out hv_Hour,
out hv_Day, out hv_YDay, out hv_Month, out hv_Year);
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.WriteImage(ho_Image, "bmp", 0, (((("D:/D程序/外焊缝方案/保存图像/" + hv_Month) + hv_Day) + hv_Hour) + hv_Minute) + hv_Second);
}
}
// stop(...); only in hdevelop
// stop(...); only in hdevelop
private void close(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
}
}