// Main procedure private void action() { // Local iconic variables HObject ho_image; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_image); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_image.Dispose(); HOperatorSet.ReadImage(out ho_image, "G:/blazarlin/calibrationdata/others/2020点胶机/旋转法/Images/mark/image mark0.bmp"); HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_Width / 1.2, hv_Height / 1.22, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive()); } ho_image.Dispose(); }
private static void dev_close_window() { if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } }
/// <summary> /// Test the neighborhood. /// </summary> private void NeighborhoodTest() { HImage image = new HImage("byte", 512, 512); HRegion regionLine = new HRegion(); HRegion regionComplement = new HRegion(); HRegion regionConnected = new HRegion(); HTuple hv_DefWindow = new HTuple(); HWindow HalconWindow1 = HalconWindowControl1.HalconWindow; HWindow HalconWindow2 = HalconWindowControl2.HalconWindow; try { HalconWindow1.SetColored(6); HalconWindow2.SetColored(6); int numObjects; image.GenImageGrayRamp(0, 0, 128, 256, 256, 512, 512); // Display code image.DispImage(HalconWindow1); image.DispImage(HalconWindow2); regionLine.GenRegionLine(100, -1, 150, 512); regionComplement = regionLine.Complement(); HOperatorSet.SetSystem("neighborhood", 4); regionConnected = regionComplement.Connection(); image.DispImage(HalconWindow1); regionConnected.DispRegion(HalconWindow1); numObjects = regionConnected.CountObj(); label1.Text = "Number of objects 4-connected:" + numObjects.ToString(); regionConnected.Dispose(); HOperatorSet.SetSystem("neighborhood", 8); regionConnected = regionComplement.Connection(); image.DispImage(HalconWindow2); regionConnected.DispRegion(HalconWindow2); numObjects = regionConnected.CountObj(); label2.Text = "Number of objects 8-connected:" + numObjects.ToString(); HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } catch (Exception) { throw; } finally { image.Dispose(); regionLine.Dispose(); regionConnected.Dispose(); regionComplement.Dispose(); } }
private void TemplateMatch_Load(object sender, EventArgs e) { if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("border_width", 0); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, 1024, 768, TMImage.Handle, "visible", "", out MhvWindowHandle); HOperatorSet.SetPart(MhvWindowHandle, 0, 0, 767, 1023); HDevWindowStack.Push(MhvWindowHandle); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image9 = null, ho_ImageScaled = null; // Local control variables HTuple hv_ModelID, hv_Index, hv_WindowHandle = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Score = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image9); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.ReadShapeModel("C:/Users/GPAP/Desktop/1.shm", out hv_ModelID); for (hv_Index = 4; (int)hv_Index <= 9; hv_Index = (int)hv_Index + 1) { ho_Image9.Dispose(); HOperatorSet.ReadImage(out ho_Image9, ("C:/Users/GPAP/Desktop/" + hv_Index) + ".jpg"); ho_ImageScaled.Dispose(); scale_image_range(ho_Image9, out ho_ImageScaled, 30, 70); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } dev_open_window_fit_image(ho_Image9, 0, 0, -1, -1, out hv_WindowHandle); HOperatorSet.FindShapeModel(ho_ImageScaled, hv_ModelID, -0.39, 0.78, 0.5, 1, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image9, HDevWindowStack.GetActive()); } HOperatorSet.DispCross(hv_WindowHandle, hv_Row, hv_Column, 36, hv_Angle); HDevelopStop(); } HOperatorSet.ClearShapeModel(hv_ModelID); ho_Image9.Dispose(); ho_ImageScaled.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image9, ho_ImageScaled, ho_Rectangle; HObject ho_ImageReduced; // Local control variables HTuple hv_WindowHandle, hv_Row1, hv_Column1; HTuple hv_Row2, hv_Column2, hv_ModelID; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image9); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); ho_Image9.Dispose(); HOperatorSet.ReadImage(out ho_Image9, "C:/Users/GPAP/Desktop/9.jpg"); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } dev_open_window_fit_image(ho_Image9, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image9, HDevWindowStack.GetActive()); } HOperatorSet.DrawRectangle1(hv_WindowHandle, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); ho_ImageScaled.Dispose(); scale_image_range(ho_Image9, out ho_ImageScaled, 30, 70); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageScaled, ho_Rectangle, out ho_ImageReduced); HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID); HOperatorSet.WriteShapeModel(hv_ModelID, "C:/Users/GPAP/Desktop/1.shm"); ho_Image9.Dispose(); ho_ImageScaled.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); }
// Short Description: close zoom window and reset part public void close_zoom_window(HTuple hv_WindowHandleZoom, HTuple hv_FormerWidth, HTuple hv_FormerHeight) { // Initialize local and output iconic variables HDevWindowStack.SetActive(hv_WindowHandleZoom); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_FormerHeight - 1, hv_FormerWidth - 1); } return; }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ResultContour = null, ho_ContCircle = null; HObject ho_Contour = null, ho_Cross = null; // Local control variables HTuple hv_WindowHandle = null, hv_Width = null; HTuple hv_Height = null, hv_MetrologyHandle = null, hv_LineRow1 = null; HTuple hv_LineColumn1 = null, hv_LineRow2 = null, hv_LineColumn2 = null; HTuple hv_Tolerance = null, hv_Index1 = null, hv_Rows = null; HTuple hv_Columns = null, hv_I = null, hv_LineParameter = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_IsOverlapping1 = new HTuple(); HTuple hv_Orientation1 = new HTuple(), hv_Orientation2 = new HTuple(); HTuple hv_MRow = new HTuple(), hv_MColumn = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ResultContour); HOperatorSet.GenEmptyObj(out ho_ContCircle); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_Cross); try { //This program shows how to detect the edges of a diamond //with subpixel accuracy and calculate the angle between them. // //In contrast to the example measure_diamond.hdev, //this example uses a metrology model to measure the edges. // //First, the top of the diamond is roughly segmented //to align the metrology objects. //Then, the metrology model is applied and returns the //parameters of the fitted lines. //Finally, the angle between the two lines is computed. // //Display initializations dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_01"); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 16, "mono", "true", "false"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); // //Create the metrology model data structure HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); //The image size is set in advance to speed up the //first call of apply_metrology_model. HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //Define the parameters of the metrology line objects hv_LineRow1 = new HTuple(); hv_LineRow1[0] = 155; hv_LineRow1[1] = 155; hv_LineColumn1 = new HTuple(); hv_LineColumn1[0] = 400; hv_LineColumn1[1] = 400; hv_LineRow2 = new HTuple(); hv_LineRow2[0] = 290; hv_LineRow2[1] = 290; hv_LineColumn2 = new HTuple(); hv_LineColumn2[0] = 230; hv_LineColumn2[1] = 570; hv_Tolerance = 20; // //Create two metrology line objects and set parameters HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_LineRow1, hv_LineColumn1, hv_LineRow2, hv_LineColumn2, hv_Tolerance, 10, 1, 20, new HTuple(), new HTuple(), out hv_Index1); //Create region of interest for the alignment ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, (hv_LineRow1.TupleSelect(0)) - 40, (hv_LineColumn1.TupleSelect(0)) - 50, (hv_LineRow1.TupleSelect(0)) + 20, (hv_LineColumn1.TupleSelect( 0)) + 50); //Change the reference coordinate system in which the //metrology model is given to be situated at the top of the diamond ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((((hv_Rows.TupleSelect(0))).TupleConcat(hv_Columns.TupleSelect(0)))).TupleConcat( 0)); // //Main loop // for (hv_I = 1; (int)hv_I <= 5; hv_I = (int)hv_I + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_" + (hv_I.TupleString( "02"))); //Roughly segment the diamond's position ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); //Extract the top of the diamond HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); // //Use the top of the diamond to align the metrology model in //the current image // HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_Rows.TupleSelect( 0), hv_Columns.TupleSelect(0), 0); // // //Perform the measurement for both lines in one call // HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); // //Access results // HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "all_param", out hv_LineParameter); HOperatorSet.AngleLl(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Angle); hv_Angle = hv_Angle.TupleDeg(); // //Display results // //Create line contours ho_ResultContour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5); HOperatorSet.IntersectionLines(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Row, out hv_Column, out hv_IsOverlapping1); //Calculate the orientation of the two lines HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), out hv_Orientation1); if ((int)(new HTuple(hv_Orientation1.TupleGreater(0))) != 0) { hv_Orientation1 = hv_Orientation1 - ((new HTuple(180)).TupleRad()); } HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect( 5), hv_LineParameter.TupleSelect(6), hv_LineParameter.TupleSelect(7), out hv_Orientation2); // //Visualize the angle between the lines ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Column, 100, hv_Orientation1, hv_Orientation2, "positive", 1); //Get the used measure regions and the measured points //for visualization ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_MRow, out hv_MColumn); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_MRow, hv_MColumn, 6, (new HTuple(45)).TupleRad() ); //Display everything if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "yellow"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Contour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Cross, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ResultContour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ContCircle, HDevWindowStack.GetActive()); } disp_message(hv_WindowHandle, ("Angle = " + (hv_Angle.TupleString(".5"))) + "бу", "window", 12, 12, "black", "true"); if ((int)(new HTuple(hv_I.TupleLess(5))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); } // stop(); only in hdevelop } //Clean up memory HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Image = null, ho_SymbolXLDs = null; HObject ho_SymbolRegions = null, ho_ObjectSelected = null; // Local control variables HTuple hv_BarWidth = new HTuple(), hv_BarHeight = new HTuple(); HTuple hv_DataCodeHandle = new HTuple(), hv_BarCodeHandle = new HTuple(); HTuple hv_WindowHandle = new HTuple(), hv_AcqHandle = new HTuple(); HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple(); HTuple hv_someitem = new HTuple(), hv_Area = new HTuple(); HTuple hv_BarIndex = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolXLDs); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); try { //*** //** INIT //* INIT CONST hv_BarWidth.Dispose(); hv_BarWidth = 800; hv_BarHeight.Dispose(); hv_BarHeight = 100; //* INIT IMAGE //* INIT DATACODE hv_DataCodeHandle.Dispose(); HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", new HTuple(), new HTuple(), out hv_DataCodeHandle); //* INIT BARCODE hv_BarCodeHandle.Dispose(); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", "true"); //* INIT LOC //* Info: //read_shape_model ('C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliInfo.shm', InfoModel) //get_shape_model_contours (InfoModelContours, InfoModel, 1) //* Sign //read_shape_model ('C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliSign.shm', SignModel) //get_shape_model_contours (SignModelContours, SignModel, 1) //*** //** DISPLAY //* DISPLAY INIT hDevelo.dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "black"); //HOperatorSet.OpenWindow(30, 0, 800, 1500, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_ExpDefaultWinHandle); //*** //** LOOP //Image Acquisition 01: Code generated by Image Acquisition 01 hv_AcqHandle.Dispose(); hv_AcqHandle = acqHandle; while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); { HObject ExpTmpOutVar_0; hDevelo.image_cali_map(ho_Image, out ExpTmpOutVar_0, new HTuple(), new HTuple()); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; HOperatorSet.DispObj(ho_Image, hv_ExpImageRawWinHandle); } try { //** PRE //find_shape_model (Image, SignModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.6, InfoRow, InfoColumn, InfoAngle, InfoScore) //rotate_image (Image, Image, deg(-InfoAngle), 'constant') if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //** RECOGNITION //* DataCode ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, "stop_after_result_num", 3, out hv_ResultHandles, out hv_DecodedDataStrings); hDevelo.image_display_datacode(ho_SymbolXLDs, hv_ResultHandles, hv_WindowHandle, hv_DecodedDataStrings, hv_DataCodeHandle); //* BARCODE ho_SymbolRegions.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hDevelo.image_get_bar(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, out hv_DecodedDataStrings, out hv_someitem); //* LOC //* Info: //find_shape_model (Image, InfoModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.7, InfoRow, InfoColumn, InfoAngle, InfoScore) //* HaedSign //find_shape_model (Image, SignModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.7, SignRow, SignColumn, SignAngle, SignScore) //* Ocr //gen_rectangle2 (ROI_OCR_01_0, InfoRow + 70, InfoColumn - 700, InfoAngle, 100, 30) //region_ocr_num_svm (Image, ROI_OCR_01_0, [], [], SymbolNames_OCR_01_0, Ocr_Split) //area_center (ROI_OCR_01_0, Area, IDRow, IDColumn) //smallest_rectangle1 (ROI_OCR_01_0, IDRow1, IDColumn1, IDRow2, IDColumn2) //height_width_ratio (ROI_OCR_01_0, IDHeight, IDWidth, IDRatio) //* Sign //HeadSignScale := 1 //HeadSignRow := SignRow //HeadSignCol := SignColumn //HeadPhi := SignAngle //region_judge_sign (Image, EDGE, HeadSignScale, HeadSignRow, HeadSignCol, HeadPhi, WindowHandle, sign) //** DISPLAY //* DISPLAY BARCODE hDevelo.set_display_font(hv_WindowHandle, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "forest green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SymbolRegions, HDevWindowStack.GetActive()); } for (hv_BarIndex = 1; (int)hv_BarIndex <= (int)(new HTuple(hv_DecodedDataStrings.TupleLength() )); hv_BarIndex = (int)hv_BarIndex + 1) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SymbolRegions, out ho_ObjectSelected, hv_BarIndex); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.SetTposition(hv_WindowHandle, hv_Row - hv_BarHeight, hv_Column - (0.25 * hv_BarWidth)); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.WriteString(hv_WindowHandle, hv_DecodedDataStrings.TupleSelect( hv_BarIndex - 1)); } } //* DISPLAY LOC if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } //* Info: //hom_mat2d_identity (InfoHomMat2D) //hom_mat2d_rotate (InfoHomMat2D, InfoAngle, 0, 0, InfoHomMat2D) //hom_mat2d_translate (InfoHomMat2D, InfoRow, InfoColumn, InfoHomMat2D) //affine_trans_contour_xld (InfoModelContours, InfoTransContours, InfoHomMat2D) if (HDevWindowStack.IsOpen()) { //dev_set_color ('green') } if (HDevWindowStack.IsOpen()) { //dev_display (InfoTransContours) } //* Ocr if (HDevWindowStack.IsOpen()) { //dev_disp_text (Ocr_Split, 'image', IDRow1 + IDHeight, IDColumn1, 'blue', [], []) } //* HeadSign //hom_mat2d_identity (SignHomMat2D) //hom_mat2d_rotate (SignHomMat2D, SignAngle, 0, 0, SignHomMat2D) //hom_mat2d_translate (SignHomMat2D, SignRow, SignColumn, SignHomMat2D) //affine_trans_contour_xld (SignModelContours, SignTransContours, SignHomMat2D) if (HDevWindowStack.IsOpen()) { //dev_set_color ('green') } if (HDevWindowStack.IsOpen()) { //dev_display (SignTransContours) } //* Sign if (HDevWindowStack.IsOpen()) { //dev_set_colored (12) } if (HDevWindowStack.IsOpen()) { //dev_display (EDGE) } //dump_window_image (ImageResult, WindowHandle) //stop () } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_SymbolXLDs.Dispose(); ho_SymbolRegions.Dispose(); ho_ObjectSelected.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_DataCodeHandle.Dispose(); hv_BarCodeHandle.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_Area.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_SymbolXLDs.Dispose(); ho_SymbolRegions.Dispose(); ho_ObjectSelected.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_DataCodeHandle.Dispose(); hv_BarCodeHandle.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_Area.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Exception.Dispose(); }
//Thread ProMulPicThread = new Thread(ProMulPic); /*=======================定义变量=================================*/ private void Form1_Load(object sender, EventArgs e) { timer1.Enabled = true; procethread = new Thread(new ThreadStart(ProMulPic)); procethread.Name = "picProThread"; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_showroi); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_psf); HOperatorSet.GenEmptyObj(out ho_noisefiltered); HOperatorSet.GenEmptyObj(out ho_RestoredImage); HOperatorSet.GenEmptyObj(out ho_RestoredImage1); HOperatorSet.GenEmptyObj(out ho_R); HOperatorSet.GenEmptyObj(out ho_G); HOperatorSet.GenEmptyObj(out ho_B); HOperatorSet.GenEmptyObj(out ho_GrayImage1); HOperatorSet.GenEmptyObj(out ho_H); HOperatorSet.GenEmptyObj(out ho_S); HOperatorSet.GenEmptyObj(out ho_V); HOperatorSet.GenEmptyObj(out ho_MultiChannelImage); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ImagePart); HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_Regions2); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SortedRegions); dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } //使用变形后的数据训练的网络 //read_ocr_class_mlp ('ocrmlp_change.omc', OCRHandle) //使用没变形的数据训练的网络 hv_fileadd = "F:/毕设用/AA毕设/halcon模糊矿车图片/"; /****************************最右边窗口**************************/ HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } set_display_font(hv_WindowHandle, 15, "mono", "true", "false"); /****************************最左边窗口**************************/ HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hWindowControl2.Width, hWindowControl2.Height, hWindowControl2.HalconWindow, "", "", out hv_WindowHandle1); HDevWindowStack.Push(hv_WindowHandle1); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } /***************************中间窗口**************************/ HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hWindowControl3.Width, hWindowControl3.Height, hWindowControl3.HalconWindow, "", "", out hv_WindowHandle2); HDevWindowStack.Push(hv_WindowHandle2); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } HOperatorSet.ReadOcrClassMlp("ocrmlp.omc", out hv_OCRHandle); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_ImageGray, ho_ImageMean; HObject ho_Region, ho_ImageRotate1, ho_Rectangle, ho_ImageReduced; HObject ho_ImagePart; // Local control variables HTuple hv_WindowHandle = new HTuple(), hv_AbsoluteHisto = new HTuple(); HTuple hv_RelativeHisto = new HTuple(), hv_MinThresh = new HTuple(); HTuple hv_MaxThresh = new HTuple(), hv_Area = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Phi3 = new HTuple(), hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_Row2 = new HTuple(); HTuple hv_Column2 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageGray); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ImageRotate1); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ImagePart); dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } //read_image (Image, 'D:/Work/LCD/LCD Pictures/昆山OK相片/1S20JES0YF07R90PMAL8_white.jpg') ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "D:/Work/LCD/LCD Pictures/昆山OK相片/lineNG_white.jpg"); hv_WindowHandle.Dispose(); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } ho_ImageGray.Dispose(); HOperatorSet.Rgb3ToGray(ho_Image, ho_Image, ho_Image, out ho_ImageGray); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageGray, out ho_ImageMean, 9, 9); hv_AbsoluteHisto.Dispose(); hv_RelativeHisto.Dispose(); HOperatorSet.GrayHisto(ho_ImageMean, ho_ImageMean, out hv_AbsoluteHisto, out hv_RelativeHisto); hv_MinThresh.Dispose(); hv_MaxThresh.Dispose(); HOperatorSet.HistoToThresh(hv_RelativeHisto, 20, out hv_MinThresh, out hv_MaxThresh); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageMean, out ho_Region, hv_MaxThresh.TupleSelect( 0), hv_MaxThresh.TupleSelect(1)); } hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row, out hv_Column); hv_Phi3.Dispose(); HOperatorSet.OrientationRegion(ho_Region, out hv_Phi3); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_ImageRotate1.Dispose(); HOperatorSet.RotateImage(ho_ImageMean, out ho_ImageRotate1, -(hv_Phi3.TupleDeg() ), "constant"); } //count_obj (Region, Number) if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { //dev_display (Region) } //dev_clear_obj (Region) using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageRotate1, out ho_Region, hv_MaxThresh.TupleSelect( 0), hv_MaxThresh.TupleSelect(1)); } hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Row2.Dispose(); hv_Column2.Dispose(); HOperatorSet.InnerRectangle1(ho_Region, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageRotate1, ho_Rectangle, out ho_ImageReduced); ho_ImagePart.Dispose(); HOperatorSet.CropDomain(ho_ImageReduced, out ho_ImagePart); HOperatorSet.WriteImage(ho_ImagePart, "bmp", 0, "C:/Users/D19040019/Desktop/test.bmp"); ho_Image.Dispose(); ho_ImageGray.Dispose(); ho_ImageMean.Dispose(); ho_Region.Dispose(); ho_ImageRotate1.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_ImagePart.Dispose(); hv_WindowHandle.Dispose(); hv_AbsoluteHisto.Dispose(); hv_RelativeHisto.Dispose(); hv_MinThresh.Dispose(); hv_MaxThresh.Dispose(); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Phi3.Dispose(); hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Row2.Dispose(); hv_Column2.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image1, ho_Rectangle, ho_ImageReduced; HObject ho_Regions, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_SelectedRegions1, ho_SortedRegions, ho_RegionTrans; HObject ho_Rectangle1 = null; // Local control variables HTuple hv_WindowHandle, hv_Row4, hv_Column4; HTuple hv_Area, hv_Row, hv_Column, hv_Phi, hv_Row3, hv_Column3; HTuple hv_Phi1, hv_Length1, hv_Length2, hv_PhiSum, hv_Index1; HTuple hv_L2Mean, hv_L1Mean, hv_PhiMean, hv_DeltaRow, hv_DeltaCol; HTuple hv_NewRow, hv_NewCol, hv_Index; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_Rectangle1); ho_Image1.Dispose(); HOperatorSet.ReadImage(out ho_Image1, "C:/Users/GPAP/Desktop/2.jpg"); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } dev_open_window_fit_image(ho_Image1, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image1, HDevWindowStack.GetActive()); } //draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) HOperatorSet.DrawPointMod(hv_WindowHandle, 100, 100, out hv_Row4, out hv_Column4); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row4 - 134, hv_Column4 - 1349, hv_Row4 + 62, hv_Column4 - 379); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image1, ho_Rectangle, out ho_ImageReduced); ho_Regions.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 0, 20); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "fill"); } ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 2201.39, 4500); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions1, "height", "and", 20, 79.94); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions1, out ho_SortedRegions, "character", "true", "row"); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_SelectedRegions1, out ho_RegionTrans, "rectangle2"); HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.OrientationRegion(ho_RegionTrans, out hv_Phi); HOperatorSet.SmallestRectangle2(ho_RegionTrans, out hv_Row3, out hv_Column3, out hv_Phi1, out hv_Length1, out hv_Length2); hv_PhiSum = 0; for (hv_Index1 = 1; (int)hv_Index1 <= (int)(new HTuple(hv_Phi1.TupleLength())); hv_Index1 = (int)hv_Index1 + 1) { if ((int)(new HTuple(((hv_Phi1.TupleSelect(hv_Index1 - 1))).TupleLess(0))) != 0) { hv_Phi1[hv_Index1 - 1] = (hv_Phi1.TupleSelect(hv_Index1 - 1)) + ((new HTuple(90)).TupleRad() ); } hv_PhiSum = hv_PhiSum + (hv_Phi1.TupleSelect(hv_Index1 - 1)); } HOperatorSet.TupleMean(hv_Length2, out hv_L2Mean); HOperatorSet.TupleMean(hv_Length1, out hv_L1Mean); hv_PhiMean = hv_PhiSum / (new HTuple(hv_Phi1.TupleLength())); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image1, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { //dev_display (RegionTrans) } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } //disp_cross (WindowHandle, Row, Column, 86, 0) //disp_line (WindowHandle, Row[2], Column[2], Row[0], Column[0]) hv_DeltaRow = ((hv_Row.TupleSelect(2)) - (hv_Row.TupleSelect(0))) / 4; hv_DeltaCol = ((hv_Column.TupleSelect(2)) - (hv_Column.TupleSelect(0))) / 4; hv_NewRow = (hv_Row.TupleSelect(2)) + hv_DeltaRow; hv_NewCol = (hv_Column.TupleSelect(2)) + hv_DeltaCol; for (hv_Index = 1; (int)hv_Index <= 4; hv_Index = (int)hv_Index + 1) { if (HDevWindowStack.IsOpen()) { //dev_set_draw ('fill') } //gen_rectangle2 (Rectangle1, NewRow-2*(Index-1)*DeltaRow, NewCol-2*(Index-1)*DeltaCol, PhiMean, L1Mean, 700) if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_NewRow - ((2 * (hv_Index - 1)) * hv_DeltaRow), hv_NewCol - ((2 * (hv_Index - 1)) * hv_DeltaCol), hv_PhiMean, hv_L1Mean - 60, 1000); } ho_Image1.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegions.Dispose(); ho_RegionTrans.Dispose(); ho_Rectangle1.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_SymbolRegions = null, ho_CandidateRegions = null; HObject ho_ScanlinesAll = null, ho_ScanlinesValid = null; // Local control variables HTuple hv_UserDefinedMessages, hv_StatusID = new HTuple(); HTuple hv_Status = new HTuple(), hv_ImagePrefix, hv_ImageSuffix; HTuple hv_WindowHandle, hv_BarCodeHandle, hv_CodeType; HTuple hv_Count, hv_IndexExposure, hv_DecodedDataStrings = new HTuple(); HTuple hv_NumInterestingCand = new HTuple(), hv_IndexCandidate = new HTuple(); HTuple hv_String = new HTuple(), hv_StatusIDSplit = new HTuple(); HTuple hv_CenterRows = new HTuple(), hv_CenterColumns = new HTuple(); HTuple hv_Box = new HTuple(), hv_IndexStatus = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_CandidateRegions); HOperatorSet.GenEmptyObj(out ho_ScanlinesAll); HOperatorSet.GenEmptyObj(out ho_ScanlinesValid); try { //This example presents the bar code parameters 'status' //and 'status_id' that can be used within get_bar_code_result //to get additional information about possibly occuring //errors when reading a bar code. // //Remark: UserDefinedMessages set to false disables additional //diagnostic messages (overexposure) in this example program hv_UserDefinedMessages = 1; if ((int)(new HTuple(hv_UserDefinedMessages.TupleEqual(0))) != 0) { hv_StatusID = "-1"; } if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } // dev_close_inspect_ctrl(...); only in hdevelop // dev_update_pc(...); only in hdevelop // dev_update_window(...); only in hdevelop hv_ImagePrefix = "barcode/25interleaved/25interleaved_exposure_"; hv_ImageSuffix = ".png"; ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImagePrefix + "01") + hv_ImageSuffix); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } // //If get_bar_code_result is called with the parameter 'status', //'persistence' must be set for the bar code model HOperatorSet.CreateBarCodeModel("persistence", 1, out hv_BarCodeHandle); // //For the visualization, the number of scanlines is reduced //(default is 10) HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "num_scanlines", 5); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "check_char", "present"); // // hv_CodeType = "2/5 Interleaved"; hv_Count = 5; for (hv_IndexExposure = 1; hv_IndexExposure.Continue(hv_Count, 1); hv_IndexExposure = hv_IndexExposure.TupleAdd(1)) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImagePrefix + (hv_IndexExposure.TupleString( "02"))) + hv_ImageSuffix); // //Try decoding the bar code in the input image ho_SymbolRegions.Dispose(); HOperatorSet.FindBarCode(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, hv_CodeType, out hv_DecodedDataStrings); ho_CandidateRegions.Dispose(); HOperatorSet.GetBarCodeObject(out ho_CandidateRegions, hv_BarCodeHandle, "all", "candidate_regions"); if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleGreater( 0))) != 0) { //If a bar code was found, display only scanlines of //successfully decoded regions (which are the first //regions of all candidate regions) HOperatorSet.CountObj(ho_SymbolRegions, out hv_NumInterestingCand); } else { //If no bar code could be found, display the error //information for all candidate regions HOperatorSet.CountObj(ho_CandidateRegions, out hv_NumInterestingCand); } // //Iterate over all symbol regions or candidate regions //if no symbol could be decoded for (hv_IndexCandidate = 0; hv_IndexCandidate.Continue(hv_NumInterestingCand - 1, 1); hv_IndexCandidate = hv_IndexCandidate.TupleAdd(1)) { // //Display valid scanlines in green, invalid //scanlines in red ho_ScanlinesAll.Dispose(); HOperatorSet.GetBarCodeObject(out ho_ScanlinesAll, hv_BarCodeHandle, hv_IndexCandidate, "scanlines_all"); ho_ScanlinesValid.Dispose(); HOperatorSet.GetBarCodeObject(out ho_ScanlinesValid, hv_BarCodeHandle, hv_IndexCandidate, "scanlines_valid"); if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesAll, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesValid, HDevWindowStack.GetActive()); } // //Display the index number of the current candidate disp_message(hv_WindowHandle, (("Candidate " + (hv_IndexCandidate + 1)) + " of ") + hv_NumInterestingCand, "image", 72, 12, "forest green", "true"); // if ((int)(hv_UserDefinedMessages) != 0) { // //Get a tuple of strings containing multiple status //identifiers, separated by a semicolon HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, hv_IndexCandidate, "status_id", out hv_StatusID); // //This user-defined procedure can be modified to //translate status identifiers to customized status //messages translate_bar_code_status_id(hv_StatusID, out hv_Status); } else { // //Get tuple of strings containing status messages HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, hv_IndexCandidate, "status", out hv_Status); } // //Show decoded bar code data if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleGreater( 0))) != 0) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesValid, HDevWindowStack.GetActive() ); } hv_String = "Decoded data strings: " + hv_DecodedDataStrings; // //Combinations of status IDs can be combined to //give further hints for the image acquisition, // e.g. about a possible overexposure HOperatorSet.TupleNumber(hv_StatusID.TupleSplit(";"), out hv_StatusIDSplit); if ((int)((new HTuple(((hv_StatusIDSplit.TupleFind(1000))).TupleNotEqual( -1))).TupleAnd(new HTuple(((hv_StatusIDSplit.TupleFind(1004))).TupleNotEqual( -1)))) != 0) { hv_String = hv_String.TupleConcat("Possible overexposure detected."); } disp_message(hv_WindowHandle, hv_String, "image", 12, 12, "forest green", "true"); } else { disp_message(hv_WindowHandle, "Could not decode bar code", "image", 12, 12, "red", "true"); } // //Display the index number of each scanline at its center scanline_centers(ho_ScanlinesAll, out hv_CenterRows, out hv_CenterColumns); // //If the scanlines lie to close to each other, omit the //background boxes if ((int)(new HTuple(((((((hv_CenterRows.TupleSelect((new HTuple(hv_CenterRows.TupleLength() )) - 2)) - (hv_CenterRows.TupleSelect((new HTuple(hv_CenterRows.TupleLength() )) - 1)))).TuplePow(2)) + ((((hv_CenterColumns.TupleSelect((new HTuple(hv_CenterColumns.TupleLength() )) - 2)) - (hv_CenterColumns.TupleSelect((new HTuple(hv_CenterColumns.TupleLength() )) - 1)))).TuplePow(2)))).TupleGreater(200 * 200))) != 0) { hv_Box = "true"; } else { hv_Box = "false"; } for (hv_IndexStatus = 0; (int)hv_IndexStatus <= (int)((new HTuple(hv_Status.TupleLength() )) - 1); hv_IndexStatus = (int)hv_IndexStatus + 1) { disp_message(hv_WindowHandle, "" + hv_IndexStatus, "image", hv_CenterRows.TupleSelect( hv_IndexStatus), hv_CenterColumns.TupleSelect(hv_IndexStatus), "green", hv_Box); } // dev_inspect_ctrl(...); only in hdevelop if ((int)((new HTuple(hv_IndexCandidate.TupleLess(hv_NumInterestingCand - 1))).TupleOr( new HTuple(hv_IndexExposure.TupleLess(hv_Count)))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); HDevelopStop(); } } } // //Free all memory of the bar code model HOperatorSet.ClearBarCodeModel(hv_BarCodeHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_CandidateRegions.Dispose(); ho_ScanlinesAll.Dispose(); ho_ScanlinesValid.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_CandidateRegions.Dispose(); ho_ScanlinesAll.Dispose(); ho_ScanlinesValid.Dispose(); }
// Main procedure public void action() { // Local iconic variables HObject ho_Image, ho_ImageMean, ho_DarkPixels; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionUnion; HObject ho_RegionDilation, ho_Skeleton, ho_Errors, ho_Scratches; HObject ho_Dots; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowID = null; HTuple hv_Message = null, hv_WindowHandleZoom = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_DarkPixels); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_Skeleton); HOperatorSet.GenEmptyObj(out ho_Errors); HOperatorSet.GenEmptyObj(out ho_Scratches); HOperatorSet.GenEmptyObj(out ho_Dots); try { //This programm shows the extraction of surface scratches via //local thresholding and morphological post-processing // dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } // //Step 1: Acquire image // ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "surface_scratch"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); dev_open_window_fit_image(ho_Image, 0, 0, hv_Width, hv_Width, out hv_WindowID); set_display_font(hv_WindowID, 16, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 4); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } hv_Message = "This program shows the extraction of"; if (hv_Message == null) { hv_Message = new HTuple(); } hv_Message[1] = "surface scratches via local thresholding"; if (hv_Message == null) { hv_Message = new HTuple(); } hv_Message[2] = "and morphological post-processing"; disp_message(hv_WindowID, hv_Message, "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowID, "black", "true"); // stop(...); only in hdevelop // //Step 2: Segment image // //Using a local threshold ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, 7, 7); ho_DarkPixels.Dispose(); HOperatorSet.DynThreshold(ho_Image, ho_ImageMean, out ho_DarkPixels, 5, "dark"); // //Extract connected components ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_DarkPixels, out ho_ConnectedRegions); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ConnectedRegions, HDevWindowStack.GetActive()); } hv_Message = "Connected components after image segmentation"; if (hv_Message == null) { hv_Message = new HTuple(); } hv_Message[1] = "using a local threshold."; disp_message(hv_WindowID, hv_Message, "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowID, "black", "true"); // stop(...); only in hdevelop // //Step 3: Process regions // //Select large regions ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 10, 1000); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive()); } disp_message(hv_WindowID, "Large Regions", "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowID, "black", "true"); // stop(...); only in hdevelop // //Visualize fractioned scratch open_zoom_window(0, ((hv_Width / 2)).TupleRound(), 2, 303, 137, 496, 3, out hv_WindowHandleZoom); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive()); } set_display_font(hv_WindowHandleZoom, 16, "mono", "true", "false"); disp_message(hv_WindowHandleZoom, "Fractioned scratches", "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowHandleZoom, "black", "true"); // stop(...); only in hdevelop // //Merge fractioned scratches via morphology ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_RegionUnion, out ho_RegionDilation, 3.5); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_RegionDilation, HDevWindowStack.GetActive()); } hv_Message = "Region of the scratches after dilation"; disp_message(hv_WindowHandleZoom, hv_Message, "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowHandleZoom, "black", "true"); // stop(...); only in hdevelop ho_Skeleton.Dispose(); HOperatorSet.Skeleton(ho_RegionDilation, out ho_Skeleton); ho_Errors.Dispose(); HOperatorSet.Connection(ho_Skeleton, out ho_Errors); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Errors, HDevWindowStack.GetActive()); } hv_Message = "Fractioned scratches merged via morphology"; disp_message(hv_WindowHandleZoom, hv_Message, "window", 12, 12, "black", "true"); disp_continue_message(hv_WindowHandleZoom, "black", "true"); // stop(...); only in hdevelop // //Distinguish small and large scratches close_zoom_window(hv_WindowHandleZoom, hv_Width, hv_Height); ho_Scratches.Dispose(); HOperatorSet.SelectShape(ho_Errors, out ho_Scratches, "area", "and", 50, 10000); ho_Dots.Dispose(); HOperatorSet.SelectShape(ho_Errors, out ho_Dots, "area", "and", 1, 50); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Scratches, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Dots, HDevWindowStack.GetActive()); } hv_Message = "Extracted surface scratches"; if (hv_Message == null) { hv_Message = new HTuple(); } hv_Message[1] = "Not categorized as scratches"; disp_message(hv_WindowID, hv_Message, "window", 440, 310, (new HTuple("red")).TupleConcat( "blue"), "true"); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_ImageMean.Dispose(); ho_DarkPixels.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionDilation.Dispose(); ho_Skeleton.Dispose(); ho_Errors.Dispose(); ho_Scratches.Dispose(); ho_Dots.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_ImageMean.Dispose(); ho_DarkPixels.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionDilation.Dispose(); ho_Skeleton.Dispose(); ho_Errors.Dispose(); ho_Scratches.Dispose(); ho_Dots.Dispose(); }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_SignModelContours, ho_Image = null; HObject ho_SymbolRegions = null, ho_ROI_OCR_01_0 = null, ho_EDGE = null; HObject ho_ObjectSelected = null, ho_SignTransContours = null; // Local control variables HTuple hv_BarWidth = new HTuple(), hv_BarHeight = new HTuple(); HTuple hv_BarCodeHandle = new HTuple(), hv_SignModel = new HTuple(); HTuple hv_AcqHandle = new HTuple(); HTuple hv_InfoRow = new HTuple(), hv_InfoColumn = new HTuple(); HTuple hv_InfoAngle = new HTuple(), hv_InfoScore = new HTuple(); HTuple hv_DecodedDataStrings = new HTuple(), hv_someitem = new HTuple(); HTuple hv_SignRow = new HTuple(), hv_SignColumn = new HTuple(); HTuple hv_SignAngle = new HTuple(), hv_SignScore = new HTuple(); HTuple hv_SymbolNames_OCR_01_0 = new HTuple(), hv_Ocr_Split = new HTuple(); HTuple hv_Area = new HTuple(), hv_IDRow = new HTuple(); HTuple hv_IDColumn = new HTuple(), hv_IDRow1 = new HTuple(); HTuple hv_IDColumn1 = new HTuple(), hv_IDRow2 = new HTuple(); HTuple hv_IDColumn2 = new HTuple(), hv_IDHeight = new HTuple(); HTuple hv_IDWidth = new HTuple(), hv_IDRatio = new HTuple(); HTuple hv_HeadSignScale = new HTuple(), hv_HeadSignRow = new HTuple(); HTuple hv_HeadSignCol = new HTuple(), hv_HeadPhi = new HTuple(); HTuple hv_sign = new HTuple(), hv_BarIndex = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_SignHomMat2D = new HTuple(), hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_SignModelContours); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_ROI_OCR_01_0); HOperatorSet.GenEmptyObj(out ho_EDGE); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_SignTransContours); try { //*** //** INIT //* INIT CONST hv_BarWidth.Dispose(); hv_BarWidth = 800; hv_BarHeight.Dispose(); hv_BarHeight = 100; //* INIT IMAGE //* INIT BARCODE hv_BarCodeHandle.Dispose(); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", "true"); //* INIT LOC //* Info: //read_shape_model ('C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliInfo.shm', InfoModel) //get_shape_model_contours (InfoModelContours, InfoModel, 1) //* Sign hv_SignModel.Dispose(); HOperatorSet.ReadShapeModel("C:/Users/zhang-sh/source/repos/repo/Model/file/InvV1CaliSign.shm", out hv_SignModel); ho_SignModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_SignModelContours, hv_SignModel, 1); //*** //** DISPLAY //* DISPLAY INIT hDevelo.dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "black"); //HOperatorSet.OpenWindow(30, 0, 800, 1500, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_ExpDefaultWinHandle); //*** //** LOOP //Image Acquisition 01: Code generated by Image Acquisition 01 hv_AcqHandle.Dispose(); hv_AcqHandle = acqHandle; while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); { HObject ExpTmpOutVar_0; hDevelo.image_cali_map(ho_Image, out ExpTmpOutVar_0, new HTuple(), new HTuple()); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; HOperatorSet.DispObj(ho_Image, hv_ExpImageRawWinHandle); } try { //** PRE using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_SignModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.6, out hv_InfoRow, out hv_InfoColumn, out hv_InfoAngle, out hv_InfoScore); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HObject ExpTmpOutVar_0; HOperatorSet.RotateImage(ho_Image, out ExpTmpOutVar_0, ((-hv_InfoAngle)).TupleDeg() , "constant"); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //** RECOGNITION //* BARCODE ho_SymbolRegions.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hDevelo.image_get_bar(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, out hv_DecodedDataStrings, out hv_someitem); //* LOC //* Info: //find_shape_model (Image, InfoModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.7, InfoRow, InfoColumn, InfoAngle, InfoScore) //* HaedSign using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_SignModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.7, out hv_SignRow, out hv_SignColumn, out hv_SignAngle, out hv_SignScore); } //* Ocr using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_ROI_OCR_01_0.Dispose(); HOperatorSet.GenRectangle2(out ho_ROI_OCR_01_0, hv_InfoRow + 70, hv_InfoColumn - 700, hv_InfoAngle, 100, 30); } hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hDevelo.region_ocr_num_svm(ho_Image, ho_ROI_OCR_01_0, new HTuple(), new HTuple(), out hv_SymbolNames_OCR_01_0, out hv_Ocr_Split); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); HOperatorSet.AreaCenter(ho_ROI_OCR_01_0, out hv_Area, out hv_IDRow, out hv_IDColumn); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); HOperatorSet.SmallestRectangle1(ho_ROI_OCR_01_0, out hv_IDRow1, out hv_IDColumn1, out hv_IDRow2, out hv_IDColumn2); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); HOperatorSet.HeightWidthRatio(ho_ROI_OCR_01_0, out hv_IDHeight, out hv_IDWidth, out hv_IDRatio); //* Sign hv_HeadSignScale.Dispose(); hv_HeadSignScale = 1; hv_HeadSignRow.Dispose(); hv_HeadSignRow = new HTuple(hv_SignRow); hv_HeadSignCol.Dispose(); hv_HeadSignCol = new HTuple(hv_SignColumn); hv_HeadPhi.Dispose(); hv_HeadPhi = new HTuple(hv_SignAngle); ho_EDGE.Dispose(); hv_sign.Dispose(); hDevelo.region_judge_sign(ho_Image, out ho_EDGE, hv_HeadSignScale, hv_HeadSignRow, hv_HeadSignCol, hv_HeadPhi, hv_ExpDefaultWinHandle, out hv_sign); //** DISPLAY //* DISPLAY BARCODE 显示条形码 hDevelo.set_display_font(hv_ExpDefaultWinHandle, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "forest green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SymbolRegions, HDevWindowStack.GetActive()); } for (hv_BarIndex = 1; (int)hv_BarIndex <= (int)(new HTuple(hv_DecodedDataStrings.TupleLength() )); hv_BarIndex = (int)hv_BarIndex + 1) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SymbolRegions, out ho_ObjectSelected, hv_BarIndex); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_Row - hv_BarHeight, hv_Column - (0.25 * hv_BarWidth)); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect( hv_BarIndex - 1)); } } //* DISPLAY LOC if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } //* Info: //hom_mat2d_identity (InfoHomMat2D) //hom_mat2d_rotate (InfoHomMat2D, InfoAngle, 0, 0, InfoHomMat2D) //hom_mat2d_translate (InfoHomMat2D, InfoRow, InfoColumn, InfoHomMat2D) //affine_trans_contour_xld (InfoModelContours, InfoTransContours, InfoHomMat2D) if (HDevWindowStack.IsOpen()) { //dev_set_color ('green') } if (HDevWindowStack.IsOpen()) { //dev_display (InfoTransContours) } //* Ocr if (HDevWindowStack.IsOpen()) { //dev_disp_text (Ocr_Split, 'image', IDRow1 + IDHeight, IDColumn1, 'blue', [], []) } //* HeadSign hv_SignHomMat2D.Dispose(); HOperatorSet.HomMat2dIdentity(out hv_SignHomMat2D); { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dRotate(hv_SignHomMat2D, hv_SignAngle, 0, 0, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dTranslate(hv_SignHomMat2D, hv_SignRow, hv_SignColumn, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } ho_SignTransContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_SignModelContours, out ho_SignTransContours, hv_SignHomMat2D); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SignTransContours, HDevWindowStack.GetActive() ); } //* Sign if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_EDGE, HDevWindowStack.GetActive()); } //dump_window_image (ImageResult, WindowHandle) //stop () } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_EDGE.Dispose(); ho_ObjectSelected.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_SignModel.Dispose(); hv_ExpImageRawWinHandle.Dispose(); hv_ExpDefaultWinHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_HeadSignScale.Dispose(); hv_HeadSignRow.Dispose(); hv_HeadSignCol.Dispose(); hv_HeadPhi.Dispose(); hv_sign.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_EDGE.Dispose(); ho_ObjectSelected.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_SignModel.Dispose(); hv_ExpImageRawWinHandle.Dispose(); hv_ExpDefaultWinHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_HeadSignScale.Dispose(); hv_HeadSignRow.Dispose(); hv_HeadSignCol.Dispose(); hv_HeadPhi.Dispose(); hv_sign.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_DarkRegion = null, ho_CharRegion = null; HObject ho_CharConnected = null, ho_CharOrig = null, ho_CharSel = null; HObject ho_Characters = null, ho_CharRectangle = null, ho_CharROI = null; HObject ho_ImageReduced = null, ho_ImageScaled = null, ho_CharConvex = null; HObject ho_SingleChar = null; // Local control variables HTuple hv_OCRFontName, hv_FontName, hv_FontImages; HTuple hv_NumImages, hv_Width, hv_Height, hv_WindowID; HTuple hv_OCRHandle, hv_WidthCharacter, hv_HeightCharacter; HTuple hv_Interpolation, hv_Features, hv_Char, hv_NumHidden; HTuple hv_Preprocessing, hv_NumComponents, hv_NumChar; HTuple hv_NumSamples, hv_i, hv_Min = new HTuple(), hv_Max = new HTuple(); HTuple hv_Range = new HTuple(), hv_WindowChar = new HTuple(); HTuple hv_DoLoop = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Button = new HTuple(), hv_Index = new HTuple(), hv_RecChar = new HTuple(); HTuple hv_Confidence = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_DarkRegion); HOperatorSet.GenEmptyObj(out ho_CharRegion); HOperatorSet.GenEmptyObj(out ho_CharConnected); HOperatorSet.GenEmptyObj(out ho_CharOrig); HOperatorSet.GenEmptyObj(out ho_CharSel); HOperatorSet.GenEmptyObj(out ho_Characters); HOperatorSet.GenEmptyObj(out ho_CharRectangle); HOperatorSet.GenEmptyObj(out ho_CharROI); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_CharConvex); HOperatorSet.GenEmptyObj(out ho_SingleChar); try { // //OCR // // dev_update_window(...); only in hdevelop //Specify the name of the font to use for reading the date on the bottle. //It is easiest to use the pre-trained font Document. If you have run the //program fontt.hdev in this directory, you can activate the second line //to use the font trained with this program. hv_OCRFontName = "Document"; //OCRFontName := 'arial' hv_FontName = "arial"; hv_FontImages = new HTuple(); hv_FontImages[0] = "a1"; hv_FontImages[1] = "a2"; hv_FontImages[2] = "a3"; hv_FontImages[3] = "a4"; hv_FontImages[4] = "a5"; hv_FontImages[5] = "a6"; hv_FontImages[6] = "a7"; hv_FontImages[7] = "a8"; hv_FontImages[8] = "a9"; hv_NumImages = new HTuple(hv_FontImages.TupleLength()); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect( 0))); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_Width / 2, hv_Height / 2, 0, "", "", out hv_WindowID); HDevWindowStack.Push(hv_WindowID); set_display_font(hv_WindowID, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } disp_continue_message(hv_WindowID, "black", "true"); //stop () //---------- read OCR classifier ---------- HOperatorSet.ReadOcrClassMlp(hv_OCRFontName, out hv_OCRHandle); HOperatorSet.GetParamsOcrClassMlp(hv_OCRHandle, out hv_WidthCharacter, out hv_HeightCharacter, out hv_Interpolation, out hv_Features, out hv_Char, out hv_NumHidden, out hv_Preprocessing, out hv_NumComponents); hv_NumChar = new HTuple(hv_Char.TupleLength()); disp_continue_message(hv_WindowID, "black", "true"); HDevelopStop(); //---------- segmentation of characters ---------- if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetShape(HDevWindowStack.GetActive(), "rectangle1"); } hv_NumSamples = 0; for (hv_i = 1; hv_i.Continue(hv_NumImages, 1); hv_i = hv_i.TupleAdd(1)) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect( hv_i - 1))); ho_DarkRegion.Dispose(); HOperatorSet.BinThreshold(ho_Image, out ho_DarkRegion); ho_CharRegion.Dispose(); HOperatorSet.ClosingCircle(ho_DarkRegion, out ho_CharRegion, 8.5); ho_CharConnected.Dispose(); HOperatorSet.Connection(ho_CharRegion, out ho_CharConnected); ho_CharOrig.Dispose(); HOperatorSet.Intersection(ho_CharConnected, ho_DarkRegion, out ho_CharOrig ); ho_CharSel.Dispose(); HOperatorSet.SelectShape(ho_CharOrig, out ho_CharSel, "area", "and", 50, 99999); ho_Characters.Dispose(); HOperatorSet.SortRegion(ho_CharSel, out ho_Characters, "character", "true", "row"); //adapt to different brigthness ho_CharRectangle.Dispose(); HOperatorSet.ShapeTrans(ho_CharRegion, out ho_CharRectangle, "rectangle1"); ho_CharROI.Dispose(); HOperatorSet.DilationRectangle1(ho_CharRectangle, out ho_CharROI, 10, 10); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_CharROI, out ho_ImageReduced); HOperatorSet.MinMaxGray(ho_CharROI, ho_Image, 5, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageReduced, out ho_ImageScaled, 255 / hv_Range, ((-hv_Min) * 255) / hv_Range); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Characters, HDevWindowStack.GetActive()); } disp_continue_message(hv_WindowID, "black", "true"); HDevelopStop(); //interactive OCR HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, (hv_Width / 2) + 50, 128, 128, 0, "", "", out hv_WindowChar); HDevWindowStack.Push(hv_WindowChar); set_display_font(hv_WindowChar, 27, "mono", "true", "false"); HDevWindowStack.SetActive(hv_WindowID); ho_CharConvex.Dispose(); HOperatorSet.ShapeTrans(ho_Characters, out ho_CharConvex, "rectangle1"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } hv_DoLoop = 1; while ((int)(new HTuple(hv_DoLoop.TupleEqual(1))) != 0) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } disp_message(hv_WindowID, "Left-click on a character to read; right mouse button to proceed to next image", "image", 8, 10, "blue", "false"); HOperatorSet.GetMbutton(hv_WindowID, out hv_Row, out hv_Column, out hv_Button); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HOperatorSet.GetRegionIndex(ho_CharConvex, hv_Row, hv_Column, out hv_Index); if ((int)(new HTuple(hv_Index.TupleGreater(0))) != 0) { ho_SingleChar.Dispose(); HOperatorSet.SelectObj(ho_Characters, out ho_SingleChar, hv_Index); HOperatorSet.DoOcrSingleClassMlp(ho_SingleChar, ho_ImageScaled, hv_OCRHandle, 1, out hv_RecChar, out hv_Confidence); HDevWindowStack.SetActive(hv_WindowChar); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, 127, 127); } if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } disp_message(hv_WindowChar, hv_RecChar.TupleSelect(0), "image", 40, 53, "green", "false"); HDevWindowStack.SetActive(hv_WindowID); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1, hv_Width - 1); } } if ((int)(new HTuple(hv_Button.TupleEqual(4))) != 0) { hv_DoLoop = 0; HDevWindowStack.SetActive(hv_WindowChar); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HDevWindowStack.SetActive(hv_WindowID); } } } HOperatorSet.ClearOcrClassMlp(hv_OCRHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetShape(HDevWindowStack.GetActive(), "original"); } // dev_update_window(...); only in hdevelop } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_DarkRegion.Dispose(); ho_CharRegion.Dispose(); ho_CharConnected.Dispose(); ho_CharOrig.Dispose(); ho_CharSel.Dispose(); ho_Characters.Dispose(); ho_CharRectangle.Dispose(); ho_CharROI.Dispose(); ho_ImageReduced.Dispose(); ho_ImageScaled.Dispose(); ho_CharConvex.Dispose(); ho_SingleChar.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_DarkRegion.Dispose(); ho_CharRegion.Dispose(); ho_CharConnected.Dispose(); ho_CharOrig.Dispose(); ho_CharSel.Dispose(); ho_Characters.Dispose(); ho_CharRectangle.Dispose(); ho_CharROI.Dispose(); ho_ImageReduced.Dispose(); ho_ImageScaled.Dispose(); ho_CharConvex.Dispose(); ho_SingleChar.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Bright, ho_RectPCB, ho_ImagePCB; HObject ho_Regions, ho_RectRegions, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_region = null, ho_fileRegions; // Local control variables HTuple hv_ImagePath, hv_width, hv_height, hv_WindowHandle; HTuple hv_RegionCount, hv_i, hv_FileRegionCount; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Bright); HOperatorSet.GenEmptyObj(out ho_RectPCB); HOperatorSet.GenEmptyObj(out ho_ImagePCB); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_RectRegions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_region); HOperatorSet.GenEmptyObj(out ho_fileRegions); //Code generated by Image Acquisition 01 //open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'gray', -1, 'false', 'default', 'CGimagetech USB Camera', 0, -1, AcqHandle) //grab_image_start (AcqHandle, -1) //grab_image_async (Image, AcqHandle, -1) //close_framegrabber (AcqHandle) //获得模板图像 hv_ImagePath = "H:/PCBDetect/晟朗/电路板检测/Image/正常 - 副本.bmp"; ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImagePath); //显示模板图像 if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.GetImageSize(ho_Image, out hv_width, out hv_height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_width, hv_height, 0, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); HDevWindowStack.SetActive(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HDevelopStop(); //PCB ROI ho_Bright.Dispose(); HOperatorSet.Threshold(ho_Image, out ho_Bright, 100, 255); ho_RectPCB.Dispose(); HOperatorSet.ShapeTrans(ho_Bright, out ho_RectPCB, "rectangle2"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_RectPCB, HDevWindowStack.GetActive()); } HDevelopStop(); //建立芯片模板图像 ho_ImagePCB.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RectPCB, out ho_ImagePCB); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "fill"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ImagePCB, HDevWindowStack.GetActive()); } HDevelopStop(); ho_Regions.Dispose(); HOperatorSet.Threshold(ho_ImagePCB, out ho_Regions, 220, 255); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Regions, HDevWindowStack.GetActive()); } HDevelopStop(); ho_RectRegions.Dispose(); HOperatorSet.OpeningRectangle1(ho_Regions, out ho_RectRegions, 10, 10); if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_RectRegions, HDevWindowStack.GetActive()); } HDevelopStop(); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RectRegions, out ho_ConnectedRegions); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ConnectedRegions, HDevWindowStack.GetActive()); } HDevelopStop(); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "rectangle1", 90); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive()); } HDevelopStop(); HOperatorSet.CountObj(ho_SelectedRegions, out hv_RegionCount); for (hv_i = 1; hv_i.Continue(hv_RegionCount, 1); hv_i = hv_i.TupleAdd(1)) { if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } ho_region.Dispose(); HOperatorSet.SelectObj(ho_SelectedRegions, out ho_region, hv_i); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_region, HDevWindowStack.GetActive()); } HDevelopStop(); } //将芯片模板存入文件 HOperatorSet.WriteRegion(ho_SelectedRegions, "H:/PCBDetect/电路板检测Halcon程序/1.reg"); //使用模板文件 ho_fileRegions.Dispose(); HOperatorSet.ReadRegion(out ho_fileRegions, "H:/PCBDetect/电路板检测Halcon程序/1.reg"); HOperatorSet.CountObj(ho_fileRegions, out hv_FileRegionCount); if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } for (hv_i = 1; hv_i.Continue(hv_FileRegionCount, 1); hv_i = hv_i.TupleAdd(1)) { if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } ho_region.Dispose(); HOperatorSet.SelectObj(ho_fileRegions, out ho_region, hv_i); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_region, HDevWindowStack.GetActive()); } HDevelopStop(); } ho_Image.Dispose(); ho_Bright.Dispose(); ho_RectPCB.Dispose(); ho_ImagePCB.Dispose(); ho_Regions.Dispose(); ho_RectRegions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_region.Dispose(); ho_fileRegions.Dispose(); }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Image = null, ho_ImageMean, ho_RegionDynThresh; HObject ho_ConnectedRegions, ho_ErrorRegions; // Local control variables HTuple hv_AcqHandle = new HTuple(), hv_Width = new HTuple(); HTuple hv_Height = new HTuple(), hv_WindowHandle = new HTuple(); HTuple hv_NumErrors = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_RegionDynThresh); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_ErrorRegions); try { //Image Acquisition 02: Code generated by Image Acquisition 02 hv_AcqHandle.Dispose(); 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); //while (true) ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); //Image Acquisition 02: Do something //endwhile HOperatorSet.CloseFramegrabber(hv_AcqHandle); //关闭程序计数器,图形变量更新,窗口图形更新 // dev_update_window(...); only in hdevelop //读取一张图像 //read_image (Image, 'plastic_mesh/plastic_mesh_01') //关闭打开的窗口 if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } { HObject ExpTmpOutVar_0; ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_ConnectedRegions.Dispose(); ho_ErrorRegions.Dispose(); hv_Width.Dispose(); hv_Height.Dispose(); hv_WindowHandle.Dispose(); hv_NumErrors.Dispose(); CheckNetBreak(ho_Image, out ExpTmpOutVar_0, out ho_ImageMean, out ho_RegionDynThresh, out ho_ConnectedRegions, out ho_ErrorRegions, out hv_Width, out hv_Height, out hv_WindowHandle, out hv_NumErrors); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; } // stop(...); only in hdevelop //*endfor } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_ConnectedRegions.Dispose(); ho_ErrorRegions.Dispose(); hv_AcqHandle.Dispose(); hv_Width.Dispose(); hv_Height.Dispose(); hv_WindowHandle.Dispose(); hv_NumErrors.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_ConnectedRegions.Dispose(); ho_ErrorRegions.Dispose(); hv_AcqHandle.Dispose(); hv_Width.Dispose(); hv_Height.Dispose(); hv_WindowHandle.Dispose(); hv_NumErrors.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image11515282Cam0; // Local control variables HTuple hv_WindowHandle, hv_row, hv_col, hv_row1; HTuple hv_col1, hv_RowProj, hv_ColProj, hv_Real, hv_Real1; HTuple hv_k, hv_b, hv_k1, hv_b1, hv_x, hv_y, hv_x0, hv_y0; HTuple hv_TargetRow, hv_TargetCol, hv_k2, hv_b2, hv_kIn; HTuple hv_bIn, hv_LineOutRow, hv_LineOutCol, hv_LineOutRow1; HTuple hv_LineOutCol1, hv_k3, hv_b3, hv_Sqrt; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image11515282Cam0); ho_Image11515282Cam0.Dispose(); HOperatorSet.ReadImage(out ho_Image11515282Cam0, "C:/项目/李博定位台需求/AssemblyBench/JPT TosaTest/bin/Debug/ImageSaved/ImageSaved/11月5日 15时28分2秒_Cam0.jpg"); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } dev_open_window_fit_image(ho_Image11515282Cam0, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image11515282Cam0, HDevWindowStack.GetActive()); } hv_row = 456; hv_col = 789; hv_row1 = 1000; hv_col1 = 1450; HOperatorSet.ProjectionPl(200, 200, hv_row, hv_col, hv_row1, hv_col1, out hv_RowProj, out hv_ColProj); HOperatorSet.DispCross(hv_WindowHandle, 200, 200, 26, 0); HOperatorSet.DispLine(hv_WindowHandle, hv_row, hv_col, hv_row1, hv_col1); HOperatorSet.DispCross(hv_WindowHandle, hv_RowProj, hv_ColProj, 26, 0); HOperatorSet.DispLine(hv_WindowHandle, 200, 200, hv_RowProj, hv_ColProj); HOperatorSet.TupleReal(hv_col1 - hv_col, out hv_Real); HOperatorSet.TupleReal(hv_row1 - hv_row, out hv_Real1); hv_k = hv_Real / hv_Real1; hv_b = hv_col1 - (hv_k * hv_row1); HOperatorSet.DispCross(hv_WindowHandle, 234, (hv_k * 234) + hv_b, 56, 0); //根据点画垂线 hv_k1 = -1 / hv_k; hv_b1 = ((234 * hv_k) + hv_b) - ((-1 / hv_k) * 234); hv_x = 234; hv_y = (234 * hv_k1) + hv_b1; hv_x0 = 567; hv_y0 = (567 * hv_k1) + hv_b1; HOperatorSet.DispCross(hv_WindowHandle, hv_x0, hv_y0, 56, 0); HOperatorSet.DispLine(hv_WindowHandle, hv_x, hv_y, hv_x0, hv_y0); //画指定长度的垂线 GetVerticalFromDistance(hv_x, hv_y, hv_row, hv_col, hv_row1, hv_col1, 60, "row", -1, out hv_TargetRow, out hv_TargetCol, out hv_k2, out hv_b2, out hv_kIn, out hv_bIn); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } HOperatorSet.DispCross(hv_WindowHandle, hv_TargetRow, hv_TargetCol, 56, 0); GetVerticalFromDistance(hv_x, hv_y, hv_row, hv_col, hv_row1, hv_col1, 60, "row", 1, out hv_TargetRow, out hv_TargetCol, out hv_k2, out hv_b2, out hv_kIn, out hv_bIn); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } HOperatorSet.DispCross(hv_WindowHandle, hv_TargetRow, hv_TargetCol, 56, 0); GetParallelLineFromDistance(hv_row, hv_col, hv_row1, hv_col1, 350, "row", -1, out hv_LineOutRow, out hv_LineOutCol, out hv_LineOutRow1, out hv_LineOutCol1, out hv_k3, out hv_b3); HOperatorSet.DispLine(hv_WindowHandle, hv_LineOutRow, hv_LineOutCol, hv_LineOutRow1, hv_LineOutCol1); HOperatorSet.DispCross(hv_WindowHandle, hv_LineOutRow, hv_LineOutCol, 106, 0); HOperatorSet.DispCross(hv_WindowHandle, hv_LineOutRow1, hv_LineOutCol1, 106, 0); HOperatorSet.TupleSqrt(((hv_LineOutRow - hv_row) * (hv_LineOutRow - hv_row)) + ((hv_LineOutCol - hv_col) * (hv_LineOutCol - hv_col)), out hv_Sqrt); ho_Image11515282Cam0.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Region, ho_SmoothedImage, ho_ReducedImage; HObject ho_ModelImages, ho_ModelRegions, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_ModelContours, ho_TestImage = null; // Local control variables HTuple hv_WindowHandle = null, hv_Colors = null; HTuple hv_MinScore = null, hv_NumMatches = null, hv_AcqHandle = null; HTuple hv_Size = null, hv_Coeffs = null, hv_ModelID = null; HTuple hv_NumLevels = null, hv_AngleStart = null, hv_AngleExtent = null; HTuple hv_AngleStep = null, hv_ScaleMin = null, hv_ScaleMax = null; HTuple hv_ScaleStep = null, hv_Metric = null, hv_MinContrast = null; HTuple hv_keepGrabbing = null, hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Button = new HTuple(), hv_Exception = new HTuple(); HTuple hv_ErrorCode = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Score = new HTuple(), hv_ScoreLength = new HTuple(); HTuple hv_BestMatchScore = new HTuple(), hv_location = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_SmoothedImage); HOperatorSet.GenEmptyObj(out ho_ReducedImage); HOperatorSet.GenEmptyObj(out ho_ModelImages); HOperatorSet.GenEmptyObj(out ho_ModelRegions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_TestImage); try { // import(...); only in hdevelop // import(...); only in hdevelop if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "white"); HOperatorSet.OpenWindow(0, 0, 1024, 768, 0, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); // dev_update_time(...); only in hdevelop if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } hv_Colors = new HTuple(); hv_Colors[0] = "red"; hv_Colors[1] = "green"; hv_Colors[2] = "cyan"; hv_MinScore = 0.5; hv_NumMatches = 1; //global object Image HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "00111cf51192_SENSORTECHNOLOGYCOLTD_STCGE83A", 0, -1, out hv_AcqHandle); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTimeRaw", 9000); disp_message(hv_WindowHandle, "Press F5 to begin live grab. Press Left mouse click to exit live acquisition...", "window", -1, -1, "black", "true"); // stop(...); only in hdevelop abshear_async_acquisition(hv_WindowHandle, hv_AcqHandle); disp_message(hv_WindowHandle, "Hold down Left Mouse Button to select region", "window", -1, -1, "black", "true"); ho_Region.Dispose(); HOperatorSet.DrawRegion(out ho_Region, hv_WindowHandle); HOperatorSet.InfoSmooth("deriche2", 0.3, out hv_Size, out hv_Coeffs); ho_SmoothedImage.Dispose(); HOperatorSet.SmoothImage(ExpGetGlobalVar_Image(), out ho_SmoothedImage, "deriche2", 0.3); ho_ReducedImage.Dispose(); HOperatorSet.ReduceDomain(ho_SmoothedImage, ho_Region, out ho_ReducedImage); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); HOperatorSet.InspectShapeModel(ho_ReducedImage, out ho_ModelImages, out ho_ModelRegions, 1, 160); //Since the shape models contain a few extraneous edges, they will be //removed here to give a slightly nicer visualization. ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_ModelRegions, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 20, 100000); ho_ModelRegions.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_ModelRegions); ho_ModelContours.Dispose(); HOperatorSet.GenContoursSkeletonXld(ho_ModelRegions, out ho_ModelContours, 1, "filter"); HOperatorSet.CreateShapeModel(ho_ReducedImage, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID); HOperatorSet.GetShapeModelParams(hv_ModelID, out hv_NumLevels, out hv_AngleStart, out hv_AngleExtent, out hv_AngleStep, out hv_ScaleMin, out hv_ScaleMax, out hv_ScaleStep, out hv_Metric, out hv_MinContrast); abshear_serialize_and_save(ho_SmoothedImage, hv_ModelID); hv_keepGrabbing = 1; while ((int)(hv_keepGrabbing) != 0) { try { HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_ErrorCode = hv_Exception.TupleSelect(0); } if ((int)(new HTuple(hv_Button.TupleEqual(1))) != 0) { hv_keepGrabbing = 0; } //Grab and display an image for the matching ho_TestImage.Dispose(); HOperatorSet.GrabImageAsync(out ho_TestImage, hv_AcqHandle, -1); ho_SmoothedImage.Dispose(); HOperatorSet.SmoothImage(ho_TestImage, out ho_SmoothedImage, "deriche2", 0.3); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_TestImage, HDevWindowStack.GetActive()); } HOperatorSet.FindShapeModel(ho_TestImage, hv_ModelID, -0.39, 0.79, hv_MinScore, hv_NumMatches, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score); HDevWindowStack.SetActive(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } dev_display_shape_matching_results(hv_ModelID, "green", hv_Row, hv_Column, hv_Angle, 1.0, 1.0, 0); HOperatorSet.TupleLength(hv_Score, out hv_ScoreLength); if ((int)(new HTuple(hv_ScoreLength.TupleGreater(0))) != 0) { HOperatorSet.TupleFirstN(hv_Score, 0, out hv_BestMatchScore); disp_message(hv_WindowHandle, "BestMatch: " + hv_BestMatchScore, "window", -1, -1, "black", "true"); hv_location = (("Location: " + hv_Row) + new HTuple(", ")) + hv_Column; disp_message(hv_WindowHandle, hv_location, "window", 40, -1, "black", "true"); } else { disp_message(hv_WindowHandle, "No match found...", "window", -1, -1, "black", "true"); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Region.Dispose(); ho_SmoothedImage.Dispose(); ho_ReducedImage.Dispose(); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_ModelContours.Dispose(); ho_TestImage.Dispose(); throw HDevExpDefaultException; } ho_Region.Dispose(); ho_SmoothedImage.Dispose(); ho_ReducedImage.Dispose(); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_ModelContours.Dispose(); ho_TestImage.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Image1, ho_Image2, ho_Image3; HObject ho_ImageResult1, ho_ImageResult2, ho_ImageResult3; HObject ho_ClassRegionsNotRejected, ho_ObjectSelected, ho_RegionOpening2; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionUnion; HObject ho_RegionOpening, ho_RegionTrans, ho_ImageReduced; HObject ho_ObjectSelected1, ho_RegionOpening1, ho_RegionFillUp; HObject ho_RegionClosing, ho_ConnectedRegions1, ho_SelectedRegions1; HObject ho_Rectangle; // Local control variables HTuple hv_WindowHandle, hv_MLPHandle, hv_MLPHandle1; HTuple hv_Row1, hv_Column1, hv_Phi, hv_Length1, hv_Length2; HTuple hv_Area, hv_Row, hv_Column; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image2); HOperatorSet.GenEmptyObj(out ho_Image3); HOperatorSet.GenEmptyObj(out ho_ImageResult1); HOperatorSet.GenEmptyObj(out ho_ImageResult2); HOperatorSet.GenEmptyObj(out ho_ImageResult3); HOperatorSet.GenEmptyObj(out ho_ClassRegionsNotRejected); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_RegionOpening2); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Rectangle); try { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, new HTuple(new HTuple("C:/Code/Halcon/╪Л╡Бспнч/м╪ф╛1/") + 1) + ".jpg"); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HOperatorSet.ReadClassMlp("mlp_1.gmc", out hv_MLPHandle); HOperatorSet.ReadClassMlp("mlp_2.gmc", out hv_MLPHandle1); HOperatorSet.SetTposition(hv_WindowHandle, 10, 10); set_display_font(hv_WindowHandle, 50, "mono", "true", "false"); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); HOperatorSet.Decompose3(ho_Image, out ho_Image1, out ho_Image2, out ho_Image3 ); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); HOperatorSet.TransFromRgb(ho_Image1, ho_Image2, ho_Image3, out ho_ImageResult1, out ho_ImageResult2, out ho_ImageResult3, "hsv"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image3, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_Image, out ho_ClassRegionsNotRejected, hv_MLPHandle, 0.5); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected, 1); ho_RegionOpening2.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected, out ho_RegionOpening2, 5); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionOpening2, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 6000.2, 159205); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionUnion, out ho_RegionOpening, 3.5); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_RegionOpening, out ho_RegionTrans, "circle"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_RegionTrans, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "fill"); } ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionTrans, out ho_ImageReduced); ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_ImageReduced, out ho_ClassRegionsNotRejected, hv_MLPHandle1, 0.5); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected1, 4); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected1, out ho_RegionOpening1, 3.5); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_RegionOpening1, out ho_RegionFillUp); ho_RegionClosing.Dispose(); HOperatorSet.ClosingCircle(ho_RegionFillUp, out ho_RegionClosing, 10); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegions1, out hv_Row1, out hv_Column1, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1, hv_Column1, hv_Phi, hv_Length1, hv_Length2); HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area, out hv_Row, out hv_Column); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if ((int)((new HTuple((new HTuple((new HTuple(hv_Length1.TupleLess(200))).TupleAnd( new HTuple(hv_Length2.TupleLess(200))))).TupleAnd(new HTuple(hv_Length1.TupleGreater( 50))))).TupleAnd(new HTuple(hv_Length2.TupleGreater(50)))) != 0) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Rectangle, HDevWindowStack.GetActive()); } HOperatorSet.WriteString(hv_WindowHandle, "OK"); //set_framegrabber_param (AcqHandle, 'UserOutputValue', 0) } else { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } HOperatorSet.WriteString(hv_WindowHandle, "NG"); //set_framegrabber_param (AcqHandle, 'UserOutputValue', 1) } } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); }
private void action(string ImgPath) { HObject ho_Image1, ho_Image = null, ho_Caltab = null; HTuple hv_ImgPath = null, hv_Pointer = null; HTuple hv_Type = null, hv_Width = null, hv_Height = null; HTuple hv_WindowHandle1 = null, hv_StartCamPar = null; HTuple hv_TmpCtrl_FindCalObjParNames = null, hv_TmpCtrl_FindCalObjParValues = null; HTuple hv_CalibDataID = null, hv_NumImages = null, hv_I = null; HTuple hv_RCoord = new HTuple(), hv_CCoord = new HTuple(); HTuple hv_Index = null, hv_StartPose = new HTuple(), hv_CaltabName = null; HTuple hv_Error = null, hv_CamParam = null, hv_WindowHandle = null; HTuple hv_Exception = null, hv_Row = null, hv_Column = null; HTuple hv_Pose = null, hv_X = null, hv_Y = null, hv_Z = null; HTuple hv_Image_X1 = null, hv_Image_Y1 = null, hv_Distance_XY = null; HTuple hv_Image_X2 = null, hv_Image_Y2 = null, hv_World_X1 = null; HTuple hv_World_Y1 = null, hv_World_X2 = null, hv_World_Y2 = null; HTuple hv_DistanceWorld = null, hv_DistanceImage = null; HTuple hv_DistanceOneMilimeter = null, hv_DistanceOnePixel = null; HTuple hv_OffSetX = null, hv_OffSetY = null, hv_FinalPose = null; HTuple hv_Erros = null, hv_X1 = null, hv_Y1 = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Caltab); try { //相机内部标定(需要很多位置的标定板图像) 黑三角为第三象限 //图片文件夹 hv_ImgPath = ImgPath; if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image1.Dispose(); HOperatorSet.ReadImage(out ho_Image1, hv_ImgPath + "calib_01"); HOperatorSet.GetImagePointer1(ho_Image1, out hv_Pointer, out hv_Type, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_Width, hv_Height, 0, "visible", "", out hv_WindowHandle1); HDevWindowStack.Push(hv_WindowHandle1); //初始相机参数 hv_StartCamPar = new HTuple(); hv_StartCamPar[0] = 0; hv_StartCamPar[1] = 0; hv_StartCamPar[2] = 1.67e-006; hv_StartCamPar[3] = 1.67e-006; hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Width / 2); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Height / 2); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Width); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Height); hv_TmpCtrl_FindCalObjParNames = new HTuple(); hv_TmpCtrl_FindCalObjParNames[0] = "gap_tolerance"; hv_TmpCtrl_FindCalObjParNames[1] = "alpha"; hv_TmpCtrl_FindCalObjParNames[2] = "skip_find_caltab"; hv_TmpCtrl_FindCalObjParValues = new HTuple(); hv_TmpCtrl_FindCalObjParValues[0] = 1; hv_TmpCtrl_FindCalObjParValues[1] = 1; hv_TmpCtrl_FindCalObjParValues[2] = "false"; //生成一个标定数据模型(制定设置中的相机数量和校准对象数量) HOperatorSet.CreateCalibData("calibration_object", 1, 1, out hv_CalibDataID); //设置相机类型和初始参数(设置相机类型和初始内部相机参数),只有相同类型的相机可以在单个设置中校准 HOperatorSet.SetCalibDataCamParam(hv_CalibDataID, 0, "area_scan_telecentric_division", hv_StartCamPar); //根据标定板规格生成标定板文件 HOperatorSet.GenCaltab(7, 7, 0.000625, 0.5, "caltab.descr", "caltab.ps"); //根据标定板模型创建一个标定对象(描述校准对象) HOperatorSet.SetCalibDataCalibObject(hv_CalibDataID, 0, "caltab.descr"); hv_NumImages = 20; HTuple end_val24 = hv_NumImages; HTuple step_val24 = 1; for (hv_I = 1; hv_I.Continue(end_val24, step_val24); hv_I = hv_I.TupleAdd(step_val24)) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImgPath + "calib_") + (hv_I.TupleString( "02d"))); //找到Halcon标定板,并将数据存入校准数据模型中(收集数据) //即提取校准对象的mark点的图像坐标以及相机粗略估计姿态 if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HOperatorSet.FindCalibObject(ho_Image, hv_CalibDataID, 0, 0, hv_I, hv_TmpCtrl_FindCalObjParNames, hv_TmpCtrl_FindCalObjParValues); //从校准数据模型中获取轮廓数据 ho_Caltab.Dispose(); HOperatorSet.GetCalibDataObservContours(out ho_Caltab, hv_CalibDataID, "caltab", 0, 0, hv_I); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Caltab, HDevWindowStack.GetActive()); } HOperatorSet.GetCalibDataObservPoints(hv_CalibDataID, 0, 0, hv_I, out hv_RCoord, out hv_CCoord, out hv_Index, out hv_StartPose); //find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose) if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } HOperatorSet.DispCircle(hv_WindowHandle1, hv_RCoord, hv_CCoord, HTuple.TupleGenConst( new HTuple(hv_RCoord.TupleLength()), 2.5)); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1, hv_Width - 1); } //set_calib_data_observ_points (CalibDataID, 0, 0, i, RCoord, CCoord, 'all', StartPose) } //执行实际的校准 //校准相机 HOperatorSet.CalibrateCameras(hv_CalibDataID, out hv_Error); //获得相机参数 HOperatorSet.GetCalibData(hv_CalibDataID, "camera", 0, "params", out hv_CamParam); //保存相机参数 HOperatorSet.WriteCamPar(hv_CamParam, "camera_parameters.dat"); HOperatorSet.ClearCalibData(hv_CalibDataID); // stop(...); only in hdevelop //外部标定(只需要一张标定板图像) //即获取相机的姿态 if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, hv_Width + 5, hv_Width, hv_Height, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); //读取初始相机参数 try { HOperatorSet.ReadCamPar("camera_parameters.dat", out hv_CamParam); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); // stop(...); only in hdevelop } //从图像点确定外部摄像机参数和世界坐标 //当校准板直接放在测量平面上时 可以从图像确定外部摄像机参数 ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImgPath + "calib_01"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } hv_CaltabName = "caltab.descr"; hv_TmpCtrl_FindCalObjParNames = new HTuple(); hv_TmpCtrl_FindCalObjParNames[0] = "gap_tolerance"; hv_TmpCtrl_FindCalObjParNames[1] = "alpha"; hv_TmpCtrl_FindCalObjParNames[2] = "skip_find_caltab"; hv_TmpCtrl_FindCalObjParValues = new HTuple(); hv_TmpCtrl_FindCalObjParValues[0] = 1; hv_TmpCtrl_FindCalObjParValues[1] = 1; hv_TmpCtrl_FindCalObjParValues[2] = "false"; //生产一个标定数据模型 HOperatorSet.CreateCalibData("calibration_object", 1, 1, out hv_CalibDataID); //设置相机类型和初始参数 HOperatorSet.SetCalibDataCamParam(hv_CalibDataID, 0, "area_scan_telecentric_division", hv_CamParam); //定义校准对象 HOperatorSet.SetCalibDataCalibObject(hv_CalibDataID, 0, hv_CaltabName); //找到Halcon标定板,并将数据写入校准数据模型中 HOperatorSet.FindCalibObject(ho_Image, hv_CalibDataID, 0, 0, 1, hv_TmpCtrl_FindCalObjParNames, hv_TmpCtrl_FindCalObjParValues); //获得标定区域轮廓 ho_Caltab.Dispose(); HOperatorSet.GetCalibDataObservContours(out ho_Caltab, hv_CalibDataID, "caltab", 0, 0, 1); //获得标定区域mark点的信息 HOperatorSet.GetCalibDataObservPoints(hv_CalibDataID, 0, 0, 1, out hv_Row, out hv_Column, out hv_Index, out hv_Pose); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Caltab, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } //显示标定的结果 HOperatorSet.DispCaltab(hv_WindowHandle, hv_CaltabName, hv_CamParam, hv_Pose, 1); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } //在mark点中心画点 HOperatorSet.DispCircle(hv_WindowHandle, hv_Row, hv_Column, HTuple.TupleGenConst( new HTuple(hv_Row.TupleLength()), 1.5)); // stop(...); only in hdevelop //取得校准文件内的坐标数据 //caltab_points (CaltabName, X, Y, Z) //calibrate_cameras (CalibDataID, Error) //考虑标定板厚度进行姿态矫正 3.5mm厚度 HOperatorSet.SetOriginPose(hv_Pose, 0, 0, 0.0035, out hv_Pose); HOperatorSet.ClearCalibData(hv_CalibDataID); //图像坐标转世界坐标 HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Row, hv_Column, "mm", out hv_X, out hv_Y); hv_Image_X1 = 100; hv_Image_Y1 = 100; hv_Distance_XY = 500; hv_Image_X2 = hv_Image_X1 + hv_Distance_XY; hv_Image_Y2 = hv_Image_Y1 + hv_Distance_XY; HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Image_Y1, hv_Image_X1, "mm", out hv_World_X1, out hv_World_Y1); HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Image_Y2, hv_Image_X2, "mm", out hv_World_X2, out hv_World_Y2); //计算世界坐标距离? HOperatorSet.DistancePp(hv_World_Y1, hv_World_X1, hv_World_Y2, hv_World_X2, out hv_DistanceWorld); //计算像素坐标? HOperatorSet.DistancePp(hv_Image_X1, hv_Image_Y1, hv_Image_X2, hv_Image_Y2, out hv_DistanceImage); //每毫米世界坐标距=距离对应的像素点个数? hv_DistanceOneMilimeter = hv_DistanceImage / hv_DistanceWorld; //每个像素对应的世界坐标距离? hv_DistanceOnePixel = hv_DistanceWorld / hv_DistanceImage; //调整世界坐标至中心偏移量? hv_OffSetX = (hv_Width / 2) * hv_DistanceOnePixel; hv_OffSetY = (hv_Height / 2) * hv_DistanceOnePixel; // hv_X = new HTuple(); hv_X[0] = 0; hv_X[1] = 50; hv_X[2] = 100; hv_X[3] = 80; hv_Y = new HTuple(); hv_Y[0] = 5; hv_Y[1] = 0; hv_Y[2] = 5; hv_Y[3] = 0; hv_Z = new HTuple(); hv_Z[0] = 0; hv_Z[1] = 0; hv_Z[2] = 0; hv_Z[3] = 0; hv_RCoord = new HTuple(); hv_RCoord[0] = 414; hv_RCoord[1] = 227; hv_RCoord[2] = 85; hv_RCoord[3] = 128; hv_CCoord = new HTuple(); hv_CCoord[0] = 119; hv_CCoord[1] = 318; hv_CCoord[2] = 550; hv_CCoord[3] = 448; HOperatorSet.VectorToPose(hv_X, hv_Y, hv_Z, hv_RCoord, hv_CCoord, hv_CamParam, "telecentric_planar_robust", "error", out hv_FinalPose, out hv_Erros); HOperatorSet.WritePose(hv_FinalPose, "campose.dat"); HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_FinalPose, hv_Row, hv_Column, 1, out hv_X1, out hv_Y1); } catch (HalconException HDevExpDefaultException) { ho_Image1.Dispose(); ho_Image.Dispose(); ho_Caltab.Dispose(); throw HDevExpDefaultException; } ho_Image1.Dispose(); ho_Image.Dispose(); ho_Caltab.Dispose(); }