public void GetAxesParam(ADisplay aDisplay, bool isShowDistorted) { double dOriginX, dOriginY; ICogTransform2D transform2D; CogTransform2DLinear transform2DLinear = null; ICogGraphicInteractive axes = aDisplay.GetInteractiveGraphics("Calibrated Origin"); if (axes != null) { CogCoordinateAxes coordinateAxes = axes as CogCoordinateAxes; dOriginX = coordinateAxes.OriginX; dOriginY = coordinateAxes.OriginY; TransUncalibrated2CalibratedCoord(dOriginX, dOriginY, out m_dOriginX, out m_dOriginY); if (isShowDistorted == true) { transform2D = m_cogCalibCheckerboardTool.Calibration.OwnedWarpParams.GetOutputImageRootFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(dOriginX, dOriginY).Invert(); m_dRotation = transform2DLinear.MapAngle(m_cogCoordinateAxes.Rotation) + m_dCaibratedRotation; } else { transform2D = m_cogCalibCheckerboardTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY).Invert(); m_dRotation = transform2DLinear.MapAngle(coordinateAxes.Rotation) + m_dCaibratedRotation; } } }
public void ShowCalibratedOrigin(ADisplay aDisplay, bool isSwapHandedness) { double dOriginX, dOriginY; ICogTransform2D transform2D; CogTransform2DLinear transform2DLinear = null; try { if (m_bCalibrated == true) { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibNPointToNPointTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } else { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibNPointToNPointTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); if (isSwapHandedness == false) { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } else { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2 + m_dPI) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } } //m_cogCoordinateAxes.GraphicDOFEnable = (CogCoordinateAxesDOFConstants)(-2147483644);//Position+Rotation m_cogCoordinateAxes.GraphicDOFEnable = CogCoordinateAxesDOFConstants.None; m_cogCoordinateAxes.Interactive = true; m_cogCoordinateAxes.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.XAxisLabel.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.YAxisLabel.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.TipText = "Calibrated Origin"; aDisplay.AddOverlay(m_cogCoordinateAxes as ICogGraphicInteractive, ""); } catch { } }
private void Recognize_Click(object sender, EventArgs e) { this.cogDisplayRecognize.StaticGraphics.Clear(); this.txtResult.Text = string.Empty; this.txtLocationX.Text = string.Empty; this.txtLocationY.Text = string.Empty; this.txtConstrast.Text = string.Empty; if (!_PMAlignTool.Pattern.Trained) { TrainCurrentImage(); } try { _PMAlignTool.RunParams.AcceptThreshold = double.Parse(this.spinAcceptThreshold.Value.ToString()); //_PMAlignTool.RunParams.ContrastThreshold = 10; _PMAlignTool.RunParams.RunAlgorithm = (CogPMAlignRunAlgorithmConstants)Enum.Parse (typeof(CogPMAlignRunAlgorithmConstants), this.cboRunAlgorithm.Text); _PMAlignTool.RunParams.RunMode = (CogPMAlignRunModeConstants)Enum.Parse (typeof(CogPMAlignRunModeConstants), this.cboRunMode.Text); _PMAlignTool.InputImage = new CogImage8Grey(_inputImageRecognize); _PMAlignTool.Run(); if ((CogToolBase.SfCreateLastRunRecord == (int)CogBarcodeLastRunRecordConstants.ResultsBounds) || (CogToolBase.SfRunStatus == 4)) { if (_PMAlignTool.Results == null && _PMAlignTool.RunStatus.Exception != null) { return; } var bestResult = from CogPMAlignResult eachResult in _PMAlignTool.Results orderby(eachResult.GetPose().TranslationX) orderby eachResult.GetPose().TranslationY select eachResult; foreach (CogPMAlignResult eachResult in bestResult) { CogTransform2DLinear resultPosition = eachResult.GetPose(); this.txtResult.Text += eachResult.Score.ToString("0.00") + ";"; this.txtLocationX.Text += resultPosition.TranslationX.ToString("0.00") + ";"; this.txtLocationY.Text += resultPosition.TranslationY.ToString("0.00") + ";"; this.txtConstrast.Text += eachResult.Contrast.ToString("0.00") + ";"; CogCompositeShape shape = eachResult.CreateResultGraphics(CogPMAlignResultGraphicConstants.All); ICogGraphic graphic = shape.CopyBase(CogCopyShapeConstants.BasicGraphics); graphic.Color = CogColorConstants.Green; this.cogDisplayRecognize.StaticGraphics.Add(graphic, "ResultRegion"); } } } catch (Exception ex) { XtraMessageBox.Show(string.Format("Recognize Failed! {0}", ex.Message)); } }
/* * public void GetPointParam(ADisplay aDisplay) * { * double dOriginX, dOriginY; * ICogTransform2D transform2D; * CogTransform2DLinear transform2DLinear = null; * ICogGraphicInteractive axes = aDisplay.GetInteractiveGraphics("Calibrated Origin"); * * if (axes != null) * { * CogCoordinateAxes coordinateAxes = axes as CogCoordinateAxes; * * dOriginX = coordinateAxes.OriginX; * dOriginY = coordinateAxes.OriginY; * TransUncalibrated2CalibratedCoord(dOriginX, dOriginY, out m_dOriginX, out m_dOriginY); * * if (isShowDistorted == true) * { * transform2D = m_cogCalibNPointToNPointTool.Calibration.OwnedWarpParams.GetOutputImageRootFromCalibratedTransform(); * transform2DLinear = transform2D.LinearTransform(dOriginX, dOriginY).Invert(); * m_dRotation = transform2DLinear.MapAngle(m_cogCoordinateAxes.Rotation) + m_dCaibratedRotation; * } * else * { * transform2D = m_cogCalibNPointToNPointTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); * transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY).Invert(); * m_dRotation = transform2DLinear.MapAngle(coordinateAxes.Rotation) + m_dCaibratedRotation; * } * } * } */ public void GetAxesParam(ADisplay aDisplay) { double dOriginX, dOriginY; ICogTransform2D transform2D; CogTransform2DLinear transform2DLinear = null; ICogGraphicInteractive axes = aDisplay.GetInteractiveGraphics("Calibrated Origin"); if (axes != null) { CogCoordinateAxes coordinateAxes = axes as CogCoordinateAxes; dOriginX = coordinateAxes.OriginX; dOriginY = coordinateAxes.OriginY; TransUncalibrated2CalibratedCoord(dOriginX, dOriginY, out m_dOriginX, out m_dOriginY); transform2D = m_cogCalibNPointToNPointTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY).Invert(); m_dRotation = transform2DLinear.MapAngle(coordinateAxes.Rotation) + m_dCaibratedRotation; } }
public void ShowCalibratedOrigin(ADisplay aDisplay, bool isShowDistorted, bool isSwapHandedness) { double dOriginX, dOriginY, dMappedX, dMappedY; ICogTransform2D transform2D; CogTransform2DLinear transform2DLinear = null; try { if (m_bCalibrated == true) { if (m_emComputationMode == CogCalibFixComputationModeConstants.Linear) { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibCheckerboardTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } else if (m_emComputationMode == CogCalibFixComputationModeConstants.PerspectiveAndRadialWarp) { if (isShowDistorted == true) { transform2D = m_cogCalibCheckerboardTool.Calibration.OwnedWarpParams.GetOutputImageRootFromCalibratedTransform(); transform2D.MapPoint(m_dOriginX - m_dCaibratedOriginX, m_dOriginY - m_dCaibratedOriginY, out dMappedX, out dMappedY); m_cogCoordinateAxes.OriginX = dMappedX; m_cogCoordinateAxes.OriginY = dMappedY; transform2DLinear = transform2D.LinearTransform(dMappedX, dMappedY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); m_cogCoordinateAxes.Skew = transform2DLinear.Skew; } else { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibCheckerboardTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } } } else { if (m_emComputationMode == CogCalibFixComputationModeConstants.Linear) { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibCheckerboardTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); if (isSwapHandedness == false) { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } else { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2 + m_dPI) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } } else if (m_emComputationMode == CogCalibFixComputationModeConstants.PerspectiveAndRadialWarp) { if (isShowDistorted == true) { transform2D = m_cogCalibCheckerboardTool.Calibration.OwnedWarpParams.GetOutputImageRootFromCalibratedTransform(); transform2D.MapPoint(m_dOriginX - m_dCaibratedOriginX, m_dOriginY - m_dCaibratedOriginY, out dMappedX, out dMappedY); m_cogCoordinateAxes.OriginX = dMappedX; m_cogCoordinateAxes.OriginY = dMappedY; transform2DLinear = transform2D.LinearTransform(dMappedX, dMappedY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); if (isSwapHandedness == false) { m_cogCoordinateAxes.Skew = transform2DLinear.Skew; } else { m_cogCoordinateAxes.Skew = transform2DLinear.Skew + m_dPI; } } else { TransCalibrated2UncalibratedCoord(m_dOriginX, m_dOriginY, out dOriginX, out dOriginY); m_cogCoordinateAxes.OriginX = dOriginX; m_cogCoordinateAxes.OriginY = dOriginY; transform2D = m_cogCalibCheckerboardTool.Calibration.GetComputedUncalibratedFromCalibratedTransform(); transform2DLinear = transform2D.LinearTransform(m_dOriginX, m_dOriginY); m_cogCoordinateAxes.Rotation = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation); if (isSwapHandedness == false) { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } else { m_cogCoordinateAxes.Skew = transform2DLinear.MapAngle(m_dRotation - m_dCaibratedRotation + m_dPI / 2 + m_dPI) - (m_cogCoordinateAxes.Rotation + m_dPI / 2); } } } } m_cogCoordinateAxes.GraphicDOFEnable = (CogCoordinateAxesDOFConstants)(-2147483644);//Position+Rotation m_cogCoordinateAxes.Interactive = true; m_cogCoordinateAxes.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.XAxisLabel.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.YAxisLabel.Color = CogColorConstants.Cyan; m_cogCoordinateAxes.TipText = "Calibrated Origin"; aDisplay.AddOverlay(m_cogCoordinateAxes as ICogGraphicInteractive, ""); } catch { } }
/// <summary> /// Chạy lần lượt tất cả các Job /// Chuẩn bị Update: Chạy lần lượt Job theo Mode? /// Hiện tại sử dụng đầu vào Camera /// </summary> /// <returns></returns> public string RunJob() { string returnString = ""; CogTransform2DLinear temp = null; if (toolBlockEdit.Subject != null) { toolBlockEdit.Subject.Run(); } if (toolGroupEdit.Subject != null) { toolGroupEdit.Subject.Run(); } if (ImageFileTool != null) { ImageFileTool.Run(); } if (acqFifoTool.Subject != null) { acqFifoTool.Subject.Run(); } if (!calibGribCBTool.Subject.Calibration.Calibrated) { MessageBox.Show("Image Not Calibration!!!"); } else { calibGribCBTool.Subject.Run(); } if (pmAlignTool.Subject != null) { pmAlignTool.Subject.Run(); } if (pmAlignTool.Subject.Results.Count > 0) { temp = pmAlignTool.Subject.Results[0].GetPose(); } if (temp != null) { foreach (var item in pmAlignTool.Subject.Results) { temp = (item as CogPMAlignResult).GetPose(); returnString += $"X : {temp.TranslationX.ToString("0.00")} - Y : {temp.TranslationY.ToString("0.00")} - Angle : {(temp.Rotation * 180 / Math.PI).ToString("0.00")}\r\n"; } //return ($"X : {temp.TranslationX.ToString("0.00")} - Y : {temp.TranslationY.ToString("0.00")} - Angle : {(temp.Rotation * 180 / Math.PI).ToString("0.00")}"); // Input pattern to Array listPatterns = new patternObject[20]; for (int i = 0; i < listPatterns.Length; i++) { listPatterns[i] = new patternObject(); } int index = 0; foreach (var item in PMAlignTool.Subject.Results) { var tempResult = item as CogPMAlignResult; listPatterns[index] = new patternObject { X = tempResult.GetPose().TranslationX, Y = tempResult.GetPose().TranslationY, Angle = tempResult.GetPose().Rotation * 180 / Math.PI }; index += 1; } listPatterns = ToolSupport.SortPatterns(listPatterns); // In ra màn hình list Pattern foreach (var item in listPatterns) { Console.WriteLine($"X: {item.X} Y: {item.Y} Angle: {item.Angle}"); } return(returnString); } return("Fail"); }
private void PatMaxTool_Changed(object sender, Cognex.VisionPro.CogChangedEventArgs e) { //If FunctionalArea And cogFA_Tool_Results Then if ((Cognex.VisionPro.Implementation.CogToolBase.SfCreateLastRunRecord | Cognex.VisionPro.Implementation.CogToolBase.SfRunStatus) > 0) { //Note, Results will be nothing if Run failed. if (PatMaxTool.Results == null) { //txtPatMaxScoreValue.Text = "N/A"; } else if (PatMaxTool.Results.Count > 0) { int nRetrieved = PatMaxTool.Results.Count; for (int nItem = 0; nItem < nRetrieved; nItem++) { if (nItem >= PARAM_NUM_TO_FIND) { break; } int nIndexer = nItem;//listSorted.ElementAt(nItem); try { CogPMAlignResult result = PatMaxTool.Results[nIndexer]; if (result.Score <= PARAM_ACCEPT_RATIO / 100.0) { continue; } //Passing result does not imply Pattern is found, must check count. CogCompositeShape resultGraphics = default(CogCompositeShape); resultGraphics = PatMaxTool.Results[nIndexer].CreateResultGraphics(CogPMAlignResultGraphicConstants.MatchRegion); CogRectangle rect = resultGraphics.EnclosingRectangle(CogCopyShapeConstants.All); RectangleF rcResult = new RectangleF((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height); // Get matced Template Center PointF ptTemplateCenter = CRect.GetCenter(rcResult); // Set Relative Distance PointF ptDistance = CPoint.GetDistancePoint(PARAM_PT_RELATIVE_ORIGIN, ptTemplateCenter); CogTransform2DLinear ll = result.GetPose(); ptrnResult.AddData(rcResult, ptTemplateCenter, ptDistance, result.Score, 0); } catch { } } } else { //txtPatMaxScoreValue.Text = "N/A"; } } }
//#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 void 研究图像缝合ToolStripMenuItem_Click(object sender, EventArgs e) { CogImageFile imagefile_0 = new CogImageFile(); imagefile_0.Open(Application.StartupPath + "/../../../street/street_0.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_0 = new CogImage24PlanarColor(); image_24_0 = (CogImage24PlanarColor)imagefile_0[0]; CogImage8Grey imageGray8_0 = CogImageConvert.GetIntensityImage(image_24_0, 0, 0, image_24_0.Width, image_24_0.Height); CogImageFile imagefile_1 = new CogImageFile(); imagefile_1.Open(Application.StartupPath + "/../../../street/street_1.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_1 = new CogImage24PlanarColor(); image_24_1 = (CogImage24PlanarColor)imagefile_1[0]; CogImage8Grey imageGray8_1 = CogImageConvert.GetIntensityImage(image_24_1, 0, 0, image_24_1.Width, image_24_1.Height); CogImageFile imagefile_2 = new CogImageFile(); imagefile_2.Open(Application.StartupPath + "/../../../street/street_2.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_2 = new CogImage24PlanarColor(); image_24_2 = (CogImage24PlanarColor)imagefile_2[0]; CogImage8Grey imageGray8_2 = CogImageConvert.GetIntensityImage(image_24_2, 0, 0, image_24_2.Width, image_24_2.Height); // 灰度图像 //CogImage8Grey image8 = (CogImage8Grey)m_tb.GetScriptTerminalData("imageInput"); CogImage8Grey image8 = (CogImage8Grey)m_tb.GetScriptTerminalData("OutputImage"); image8 = imageGray8_0.Copy(); CogToolCollection tbTc = m_tb.Tools; //tbTc. if (false) { image8 = (CogImage8Grey)((CogImageFileTool)tbTc["CogImageFileTool1"]).OutputImage; } CogImage8Grey image_002 = new CogImage8Grey(); //image_002 = image8; image_002 = image8.Copy(); CogImage8Grey image_003 = image8.Copy(); // 将各个图像进行缝合组成一个完整的大图 // 灰度图像 //CogIPOneImageTool imageCtrl_02 = (CogIPOneImageTool)mToolBlock.Tools["CogIPOneImageTool2"]; CogRectangle rt = new CogRectangle(); rt.SetCenterWidthHeight(0, 0, image8.Width * 3, image8.Height * 3); CogImage8Grey m_gray = rt.CreateRLE(128, 0).CreateImage8Grey(); //imageCtrl_02.InputImage = m_gray; // CogImage8Grey inputImage = ( CogImage8Grey ) image8; // tb.SetScriptTerminalData("Input", inputImage); CogImageStitch stt = new CogImageStitch(); CogTransform2DLinear rootFromBlending = new CogTransform2DLinear(); //缩放,旋转,平移 rootFromBlending.SetScalingsRotationsTranslation(1, 1, 0, 0, 0, 0); // rootFromBlending.SetScalingsRotationTranslation(); stt.AllocateBlendingBuffer(image8.Width * 3, image8.Height * 3, rootFromBlending); //stt.BlendImageIntoBuffer(image8, m_gray); stt.BlendImageIntoBuffer(imageGray8_0, imageGray8_0, m_gray, 0, 0); stt.BlendImageIntoBuffer(imageGray8_1, imageGray8_1, m_gray, image8.Width, image8.Height); stt.BlendImageIntoBuffer(imageGray8_2, imageGray8_2, m_gray, image8.Width * 2, image8.Height * 2); stt.FillDestinationImageFromBuffer(m_gray); // BlendImageIntoBuffer( CogImage8Grey, CogImage8Grey, CogImage8Grey, Int32, Int32 ) // CogCopyRegionTool ccft = new CogCopyRegionTool(); //switch ( true ) //{ //case : // break; //} cogRecordDisplay1.Image = m_gray; cogRecordDisplay1.Fit(); }