private void AssignToolResult(string parent, CogBlobResults blobResults) { CogBlobResultCollection blobResultColl = blobResults.GetBlobs(false); int count = blobResultColl.Count; AddResult(string.Format("{0}.Count", parent), blobResultColl.Count); double areaX = 0.0; double areaY = 0.0; double totalArea = 0.0; for (int i = 0; i < count; i++) { CogBlobResult blobResult = blobResultColl[i]; double x = blobResult.CenterOfMassX; double y = blobResult.CenterOfMassY; double area = blobResult.Area; totalArea += area; areaX += x * area; areaY += y * area; AddResult(string.Format("{0}[{1}].Area", parent, i), area); AddResult(string.Format("{0}[{1}].CenterOfMassX", parent, i), x); AddResult(string.Format("{0}[{1}].CenterOfMassY", parent, i), y); AddResult(string.Format("{0}[{1}].Angle", parent, i), blobResult.Angle); } AddResult(string.Format("{0}[+].Area", parent), totalArea); AddResult(string.Format("{0}[A].CenterOfMassX", parent), totalArea > 0 ? areaX / totalArea : 0.0); AddResult(string.Format("{0}[A].CenterOfMassY", parent), totalArea > 0 ? areaY / totalArea : 0.0); }
//toolNum和Pattern两个参数决定结果的来源 //Blob工具的结果获取函数 private bool getBlobToolResult(out double[] item_x, out double[] item_y, out int item_number, int toolNum) { CogBlobTool blobTool = null; item_x = null; item_y = null; item_number = 0; switch (toolNum) { case 1: blobTool = blobTool_1; break; case 2: blobTool = blobTool_2; break; case 3: blobTool = blobTool_3; break; default: return(false); } if (blobTool.Results == null) { return(false); } CogBlobResultCollection blobResult = blobTool.Results.GetBlobs(); if (blobResult == null) { return(false); } item_number = blobResult.Count; if (item_number <= 0) { item_x = new double[0]; item_y = new double[0]; return(false); } item_x = new double[item_number]; item_y = new double[item_number]; for (int i = 0; i < item_number; i++) { item_x[i] = blobResult[i].CenterOfMassX; item_y[i] = blobResult[i].CenterOfMassY; } return(true); }
/// <summary> /// Do a run on current displayed Image /// </summary> public bool RunToolBlock(int cameraModule) { bool runStatus = true; String colResult; CogToolResultConstants toolColResult; int i; if (this.cogToolRun[cameraModule] != null) { this.cogToolRun[cameraModule].Inputs["InputImage"].Value = grabImage[cameraModule] as CogImage8Grey; this.cogToolRun[cameraModule].Run(); ICogRunStatus cogStatus = this.cogToolRun[cameraModule].RunStatus; if (cogStatus.Result == CogToolResultConstants.Error) { runStatus = false; // error default all to invalid for (i = 0; i < MAX_RESULT; i++) { this.inspectResult[cameraModule, i] = HGAStatus.Unknown; } } else { double[] blob = { 0, 0, 0, 0, 0, 0 }; double[] centerofmass_X = { 0, 0, 0, 0, 0, 0 }; CommonCogToolBlock = (CogBlobTool)this.cogToolRun[cameraModule].Tools["blobtool"]; CogBlobResultCollection resultcollection = CommonCogToolBlock.Results.GetBlobs(); for (int x = 0; x < resultcollection.Count; x++) { blob[x] = resultcollection[x].Area; centerofmass_X[x] = resultcollection[x].CenterOfMassX; } } } else { runStatus = false; } return(runStatus); }
//#region "when the tool group is run" // The GroupRun function is called when the tool group is run. The default // implementation provided here is equivalent to the normal behavior of the // tool group. Modifying this function will allow you to change the behavior // when the tool group is run. public override bool GroupRun(ref string message, ref CogToolResultConstants result) { // To let the execution stop in this script when a debugger is attached, uncomment the following lines. #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } #endif histCntr = 0; CogGraphicLabel myBaslerLabel = new CogGraphicLabel(); CogGraphicLabel myFlirLabel = new CogGraphicLabel(); CogPointMarker baslerMarker; //= new CogPointMarker(); double blobAngle = 0; double blobAngleRadians = 0; //Get references to the tools CogBlobTool blobBaslerTool = (CogBlobTool)toolGroup.Tools["FindWrappersInBasler"]; CogBlobTool blobFlirTool = (CogBlobTool)toolGroup.Tools["PopsicleBlobFinder"]; CogHistogramTool popsicleHistTool = (CogHistogramTool)toolGroup.Tools["PopsicleHistogramTool"]; //Define the regions CogRectangleAffine popsicleRegion = blobFlirTool.Region as CogRectangleAffine; CogRectangleAffine histogramRegion = blobFlirTool.Region as CogRectangleAffine; //Define the fonts System.Drawing.Font myBaslerFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); System.Drawing.Font myFlirFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); //Reset stats angleFailures = 0; thermalFailures = 0; //Reset any labels and rectangles from previous runs BaslerLabels.Clear(); FlirLabels.Clear(); FlirRectangles.Clear(); //***** Run the tools to perform the search in the basler image ***** //Update status strings for Visual Studio. Do not popup a message box for errors, this causes confusion with the operators. toolGroup.SetScriptTerminalData("BaslerStatus", "OK"); toolGroup.SetScriptTerminalData("FlirStatus", "OK"); bool acquireException = false; try { //Aquire an image from the Basler, send Exception to Visual Studio toolGroup.RunTool(toolGroup.Tools["BaslerAcqFifo"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Check power connections and communication cables."); acquireException = true; } try { //Aquire an image from the Flir, send Exception to Visual Studio toolGroup.RunTool(toolGroup.Tools["FlirAcqFifo"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Check power connections and communication cables."); acquireException = true; } if (acquireException) { return(false); } try { // Scale the Flir image to zoom in to smaller temperature range toolGroup.RunTool(toolGroup.Tools["ScaleFlirImagePmap"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: ScaleFlirImagePmap"); return(false); } try { // Transform the images to calibrated space toolGroup.RunTool(toolGroup.Tools["CalBasler"], ref message, ref result); toolGroup.RunTool(toolGroup.Tools["CalFlir"], ref message, ref result); } catch (Exception ex) { //MessageBox.Show("Calibration tool error: " + ex.Message, "Script Exception"); toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: CalBasler"); toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: CalFlir"); return(false); } try { toolGroup.RunTool(toolGroup.Tools["CogPixelMapBasler"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: CogPixelMapBasler"); return(false); } try { toolGroup.RunTool(toolGroup.Tools["CogIPOneImageTool1"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: CogIPOneImageTool1"); return(false); } try { // Run the Flir hist tool toolGroup.RunTool(toolGroup.Tools["PopsicleHistogramTool"], ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: PopsicleHistogramTool"); return(false); } try { // Run the blob tool and get a reference to the results. toolGroup.RunTool(blobBaslerTool, ref message, ref result); } catch (Exception ex) { toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: Basler blobBaslerTool"); return(false); } CogBlobResultCollection blobResults = blobBaslerTool.Results.GetBlobs(); // Clear list before starting loop malBlobPoses.Clear(); Crosshairs.Clear(); // Get group input terminal data try { disableHist = (bool)toolGroup.GetScriptTerminalData("DisableHistogramInspection"); minPickAngle = (double)toolGroup.GetScriptTerminalData("MinPickAngle"); maxPickAngle = (double)toolGroup.GetScriptTerminalData("MaxPickAngle"); minPopsicleHistCount = (double)toolGroup.GetScriptTerminalData("MinPopsicleHistCount"); VisStudio_Running = (bool)toolGroup.GetScriptTerminalData("VS_Running"); showArea = (bool)toolGroup.GetScriptTerminalData("ShowArea"); showHist = (bool)toolGroup.GetScriptTerminalData("ShowHistCount"); visSideXLength = (double)toolGroup.GetScriptTerminalData("VisSideXLength"); visSideYLength = (double)toolGroup.GetScriptTerminalData("VisSideYLength"); visFlirRegionXadj = (double)toolGroup.GetScriptTerminalData("VisFlirRegionXadj"); visFlirRegionYadj = (double)toolGroup.GetScriptTerminalData("VisFlirRegionYadj"); } catch (Exception ex) { //MessageBox.Show("Getting terminal data exception: ", ex.Message); toolGroup.SetScriptTerminalData("BaslerStatus", "Getting script data: " + ex.Message); } // Set run variables for manual triggering if (!VisStudio_Running) { minPopsicleHistCount = 0; minPickAngle = -20; maxPickAngle = 20; showArea = true; showHist = true; visSideXLength = 200; visSideYLength = 70; visFlirRegionXadj = 0; visFlirRegionYadj = 0; } // *************************************** // ******** Process the blobs ********* // *************************************** try { foreach (CogBlobResult blob in blobResults) { // Set the transform for collections CogTransform2DLinear l2d = new CogTransform2DLinear(); l2d.TranslationX = blob.GetBoundingBoxAtAngle(blob.Angle).CenterX; l2d.TranslationY = blob.GetBoundingBoxAtAngle(blob.Angle).CenterY; l2d.Rotation = blob.Angle; blobAngleRadians = blob.Angle; // Crosshair setup for the Basler baslerMarker = new CogPointMarker(); baslerMarker.X = l2d.TranslationX; baslerMarker.Y = l2d.TranslationY; baslerMarker.Color = CogColorConstants.Green; baslerMarker.GraphicType = CogPointMarkerGraphicTypeConstants.Crosshair; // Flir region CogRectangleAffine myFlirRegion = new CogRectangleAffine(); myFlirRegion.CenterX = l2d.TranslationX + visFlirRegionXadj; myFlirRegion.CenterY = l2d.TranslationY + visFlirRegionYadj; myFlirRegion.Rotation = l2d.Rotation; myFlirRegion.SideXLength = visSideXLength; myFlirRegion.SideYLength = visSideYLength; blobFlirTool.Region = myFlirRegion; toolGroup.RunTool(blobFlirTool, ref message, ref result); popsicleHistTool.Region = myFlirRegion; toolGroup.RunTool(popsicleHistTool, ref message, ref result); // Get the histogram results from the bin binHist = popsicleHistTool.Result.GetHistogram(); // Count total pixels histCntr = 1; for (int i = 0; i < blobFlirTool.RunParams.SegmentationParams.HardFixedThreshold; i++) { histCntr = histCntr + binHist[i]; } myBaslerLabel = new CogGraphicLabel(); myBaslerLabel.SetXYText(0, 0, ""); myFlirLabel = new CogGraphicLabel(); myBaslerLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); myFlirLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); myFlirRegion.Visible = true; myBaslerLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter; myFlirLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter; // Decide to add the popsicle blob to the collection list if ((histCntr < minPopsicleHistCount) && (!disableHist)) { myBaslerLabel.Color = CogColorConstants.Red; myFlirRegion.Color = CogColorConstants.Red; // Show the hist count in the Flir image myFlirLabel.Color = CogColorConstants.Red; if (showHist) { myFlirLabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, "Size: " + histCntr.ToString()); } thermalFailures++; } else { myBaslerLabel.Color = CogColorConstants.Green; // If histogram check is disabled, draw rectangle in yellow, else green if (disableHist) { myFlirLabel.Color = CogColorConstants.Red; myFlirRegion.Color = CogColorConstants.Yellow; } else { myFlirLabel.Color = CogColorConstants.Green; myFlirRegion.Color = CogColorConstants.Green; } if (showHist) { myFlirLabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, "Size: " + histCntr.ToString()); } // Convert blob angle to degrees blobAngle = blob.Angle * 180 / Math.PI; if ((blobAngle > (double)minPickAngle) && (blobAngle < (double)maxPickAngle)) { malBlobPoses.Add(l2d); if (showArea) { myBaslerLabel.Color = CogColorConstants.Green; myBaslerLabel.SetXYText(l2d.TranslationX, l2d.TranslationY - 15, "Size: " + blob.Area.ToString("0")); } } else { myBaslerLabel.Color = CogColorConstants.Red; myBaslerLabel.SetXYText(l2d.TranslationX, l2d.TranslationY, "Angle: " + blobAngle.ToString("0")); myFlirLabel.Color = CogColorConstants.Red; myFlirRegion.Color = CogColorConstants.Red; angleFailures++; } } myBaslerLabel.Rotation = blobAngleRadians; BaslerLabels.Add(myBaslerLabel); FlirLabels.Add(myFlirLabel); FlirRectangles.Add(myFlirRegion); Crosshairs.Add(baslerMarker); // Update group output terminals toolGroup.SetScriptTerminalData("AngleFailures", angleFailures); toolGroup.SetScriptTerminalData("ThermalFailures", thermalFailures); toolGroup.SetScriptTerminalData("BlobCollection", malBlobPoses); } } catch (Exception ex) { toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error during blob processing: " + ex.Message); malBlobPoses.Clear(); // Clear positional data for this frame } // Returning False indicates we ran the tools in script, and they should not be // run by VisionPro return(false); }
private bool getBlobBoundary(out double[] pos_x, out double[] pos_y, out int pos_number, int toolNum, int index) { CogBlobTool blobTool = null; pos_x = null; pos_y = null; pos_number = 0; switch (toolNum) { case 1: blobTool = blobTool_1; break; case 2: blobTool = blobTool_2; break; case 3: blobTool = blobTool_3; break; default: return(false); } if (blobTool.Results == null) { return(false); } CogBlobResultCollection blobResult = blobTool.Results.GetBlobs(); if (blobResult == null) { return(false); } int number = blobResult.Count; if (index >= number || index < 0) { return(false); } CogPolygon blobBoundary = blobResult[index].GetBoundary(); int verticesNum = blobBoundary.NumVertices; pos_x = new double[verticesNum]; pos_y = new double[verticesNum]; for (int i = 0; i < verticesNum; i++) { pos_x[i] = blobBoundary.GetVertexX(i); pos_y[i] = blobBoundary.GetVertexY(i); } pos_number = verticesNum; return(true); }