public int Run(ADisplay aDisplay) { // 2016.03.22 m_nFindCount = 0; SetBlobParam(); if (m_cogBlobTool != null) { m_cogBlobTool.InputImage = aDisplay.GetImage8Grey(); if (m_cogimgMask != null) { m_cogBlobTool.RunParams.InputImageMask = m_cogimgMask; } try { // 2015.04.08 m_bRan = false; m_cogBlobTool.Run(); // 2015.04.08 WaitRanEvent(); if (m_cogBlobTool.Results != null) { // 2011.05.24 m_nFindCount = m_cogBlobTool.Results.GetBlobs().Count; } } catch { MessageBox.Show("Run Blob Error"); } } return(m_nFindCount); }
public bool Run(CogImage8Grey _SrcImage, CogRectangle _InspRegion, CogEllipseAlgo _CogEllipseAlgo, ref CogEllipseResult _CogEllipseResult, double _OffsetX = 0, double _OffsetY = 0, int _NgNumber = 0) { bool _Result = true; #region Caliper Center XY 구하기 -> Blob으로 Center 위치 Search #region GetAutoThresholdValue CogHistogramTool _HistoTool = new CogHistogramTool(); _HistoTool.InputImage = _SrcImage; _HistoTool.Region = _InspRegion; _HistoTool.Run(); int[] _HistoValue = _HistoTool.Result.GetHistogram(); double _TotSize = _InspRegion.Width * _InspRegion.Height; double _ThresholdSum = 0; int _ThresholdValue = 0; for (int iLoopCount = 0; iLoopCount < 256; ++iLoopCount) { _ThresholdSum += iLoopCount * _HistoValue[iLoopCount]; } double _ThresholdSum2 = 0; double _WeightBack = 0, _WeightFore = 0, _VarMax = 0; for (int iLoopCount = 0; iLoopCount < 256; ++iLoopCount) { _WeightBack += _HistoValue[iLoopCount]; if (0 == _WeightBack) { continue; } _WeightFore = _TotSize - _WeightBack; if (0 == _WeightFore) { break; } _ThresholdSum2 += (double)(iLoopCount * _HistoValue[iLoopCount]); double _MeanBack = _ThresholdSum2 / _WeightBack; double _MeanFore = (_ThresholdSum - _ThresholdSum2) / _WeightFore; double _VarBetween = _WeightBack * _WeightFore * Math.Pow((_MeanBack - _MeanFore), 2); if (_VarBetween > _VarMax) { _VarMax = _VarBetween; _ThresholdValue = iLoopCount; } } #endregion #region Blob Search CogBlobTool _BlobTool = new CogBlobTool(); _BlobTool.InputImage = _SrcImage; _BlobTool.Region = _InspRegion; _BlobTool.RunParams.SegmentationParams.Mode = CogBlobSegmentationModeConstants.HardFixedThreshold; _BlobTool.RunParams.SegmentationParams.Polarity = CogBlobSegmentationPolarityConstants.LightBlobs; _BlobTool.RunParams.ConnectivityMode = CogBlobConnectivityModeConstants.GreyScale; _BlobTool.RunParams.ConnectivityCleanup = CogBlobConnectivityCleanupConstants.Fill; _BlobTool.RunParams.SegmentationParams.HardFixedThreshold = _ThresholdValue; _BlobTool.RunParams.ConnectivityMinPixels = 10000; _BlobTool.Run(); CogBlobResults _BlobResults = _BlobTool.Results; double _MaxSize = 0; double _CaliperCenterX = 0; double _CaliperCenterY = 0; if (_BlobResults.GetBlobs().Count > 0) { for (int iLoopCount = 0; iLoopCount < _BlobResults.GetBlobs().Count; ++iLoopCount) { CogBlobResult _BlobResult = _BlobResults.GetBlobByID(iLoopCount); if (_BlobResult.Area > _MaxSize) { _MaxSize = _BlobResult.Area; _CaliperCenterX = _BlobResult.CenterOfMassX; _CaliperCenterY = _BlobResult.CenterOfMassY; } } } else { _CaliperCenterX = _CogEllipseAlgo.ArcCenterX - _OffsetX; _CaliperCenterY = _CogEllipseAlgo.ArcCenterY - _OffsetY; } //CogSerializer.SaveObjectToFile(_BlobTool, string.Format(@"D:\CircleBlob.vpp")); #endregion #endregion SetCaliperDirection(_CogEllipseAlgo.CaliperSearchDirection, _CogEllipseAlgo.CaliperPolarity); SetCaliper(_CogEllipseAlgo.CaliperNumber, _CogEllipseAlgo.CaliperSearchLength, _CogEllipseAlgo.CaliperProjectionLength, _CogEllipseAlgo.CaliperIgnoreNumber); //LJH 2019.05.23 Caliper Center 기준점 변경 //SetEllipticalArc(_CogEllipseAlgo.ArcCenterX - _OffsetX, _CogEllipseAlgo.ArcCenterY - _OffsetY, _CogEllipseAlgo.ArcRadiusX, _CogEllipseAlgo.ArcRadiusY, _CogEllipseAlgo.ArcAngleSpan); SetEllipticalArc(_CaliperCenterX, _CaliperCenterY, _CogEllipseAlgo.ArcRadiusX, _CogEllipseAlgo.ArcRadiusY, _CogEllipseAlgo.ArcAngleSpan); if (true == Inspection(_SrcImage)) { GetResult(); } if (FindEllipseResults != null && FindEllipseResults.Count > 0) { _CogEllipseResult.IsGood = true; } else { _CogEllipseResult.IsGood = false; } if (!_CogEllipseResult.IsGood) { CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, " - Ellipse Find Fail!!", CLogManager.LOG_LEVEL.MID); _CogEllipseResult.CenterX = _CogEllipseAlgo.ArcCenterX; _CogEllipseResult.CenterY = _CogEllipseAlgo.ArcCenterY; _CogEllipseResult.RadiusX = _CogEllipseAlgo.ArcRadiusX; _CogEllipseResult.RadiusY = _CogEllipseAlgo.ArcRadiusY; _CogEllipseResult.OriginX = 0; _CogEllipseResult.OriginY = 0; _CogEllipseResult.Rotation = 0; _CogEllipseResult.NgNumber = _NgNumber; } else { if (FindEllipseResults.GetEllipse() != null) { CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, " - Ellipse Complete", CLogManager.LOG_LEVEL.MID); _CogEllipseResult.PointFoundCount = FindEllipseResults.NumPointsFound; _CogEllipseResult.CenterX = FindEllipseResults.GetEllipse().CenterX; _CogEllipseResult.CenterY = FindEllipseResults.GetEllipse().CenterY; _CogEllipseResult.RadiusX = FindEllipseResults.GetEllipse().RadiusX; _CogEllipseResult.RadiusY = FindEllipseResults.GetEllipse().RadiusY; _CogEllipseResult.OriginX = FindEllipseResults.GetEllipse().CenterX; _CogEllipseResult.OriginY = FindEllipseResults.GetEllipse().CenterY; _CogEllipseResult.Rotation = FindEllipseResults.GetEllipse().Rotation; _CogEllipseResult.NgNumber = _NgNumber; _CogEllipseResult.PointPosXInfo = new double[FindEllipseResults.Count]; _CogEllipseResult.PointPosYInfo = new double[FindEllipseResults.Count]; _CogEllipseResult.PointStatusInfo = new bool[FindEllipseResults.Count]; for (int iLoopCount = 0; iLoopCount < FindEllipseResults.Count; ++iLoopCount) { if (true == FindEllipseResults[iLoopCount].Found) { _CogEllipseResult.PointPosXInfo[iLoopCount] = FindEllipseResults[iLoopCount].X; _CogEllipseResult.PointPosYInfo[iLoopCount] = FindEllipseResults[iLoopCount].Y; } _CogEllipseResult.PointStatusInfo[iLoopCount] = FindEllipseResults[iLoopCount].Used; } _CogEllipseResult.DiameterMinAlgo = _CogEllipseAlgo.DiameterSize - _CogEllipseAlgo.DiameterMinus; _CogEllipseResult.DiameterMaxAlgo = _CogEllipseAlgo.DiameterSize + _CogEllipseAlgo.DiameterPlus; CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, String.Format(" - Center X : {0}, Y : {1}", _CogEllipseResult.CenterX.ToString("F2"), _CogEllipseResult.CenterY.ToString("F2")), CLogManager.LOG_LEVEL.MID); CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, String.Format(" - Radius X : {0}, Y : {1}", _CogEllipseResult.RadiusX.ToString("F2"), _CogEllipseResult.RadiusY.ToString("F2")), CLogManager.LOG_LEVEL.MID); } else { CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, " - Ellipse Find Fail!!", CLogManager.LOG_LEVEL.MID); _CogEllipseResult.CenterX = _CogEllipseAlgo.ArcCenterX; _CogEllipseResult.CenterY = _CogEllipseAlgo.ArcCenterY; _CogEllipseResult.RadiusX = _CogEllipseAlgo.ArcRadiusX; _CogEllipseResult.RadiusY = _CogEllipseAlgo.ArcRadiusY; _CogEllipseResult.OriginX = 0; _CogEllipseResult.OriginY = 0; _CogEllipseResult.NgNumber = _NgNumber; _CogEllipseResult.IsGood = false; } } CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, " - Result : " + _CogEllipseResult.IsGood.ToString(), CLogManager.LOG_LEVEL.MID); return(_Result); }
public void MainTaskThread() { int count = 0; double constX = 0; double constY = 0; while (flag) { CogImage8Grey image = new CogImage8Grey(); image = camControl.CamGrab(); if (image == null) { continue; } blob.InputImage = image; blob.Run(); double xxx = 0; double yyy = 0; if (blob.Results.GetBlobs().Count != 0) { xxx = blob.Results.GetBlobs()[0].CenterOfMassX; yyy = blob.Results.GetBlobs()[0].CenterOfMassY; } CogRecord record = new CogRecord(); record.SubRecords.Add(blob.CreateLastRunRecord().SubRecords[0]); if (count < 10) { constX += xxx; constY += yyy; Dispatcher.Invoke(new Action(() => { MinX_textBox.Text = xxx.ToString(); MaxX_textBox.Text = xxx.ToString(); MinY_textBox.Text = yyy.ToString(); MaxY_textBox.Text = yyy.ToString(); })); } else if (count == 10) { constX = constX / 10; constY = constY / 10; Dispatcher.Invoke(new Action(() => { StdX_textBox.Text = constX.ToString(); StdY_textBox.Text = constY.ToString(); MinX_textBox.Text = xxx.ToString(); MaxX_textBox.Text = xxx.ToString(); MinY_textBox.Text = yyy.ToString(); MaxY_textBox.Text = yyy.ToString(); })); } else if (count % 60 == 0) { var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply(image.ToBitmap()); bmp8bpp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\SavedImages\Routine_SavedImage-" + DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss") + ".bmp", ImageFormat.Bmp); Dispatcher.Invoke(new Action(() => { double minx = double.Parse(MinX_textBox.Text); double miny = double.Parse(MinY_textBox.Text); double maxx = double.Parse(MaxX_textBox.Text); double maxy = double.Parse(MaxY_textBox.Text); if (xxx < minx) { MinX_textBox.Text = xxx.ToString(); } if (yyy < miny) { MinY_textBox.Text = yyy.ToString(); } if (xxx > maxx) { MaxX_textBox.Text = xxx.ToString(); } if (yyy > maxy) { MaxY_textBox.Text = yyy.ToString(); } })); } else { Dispatcher.Invoke(new Action(() => { if (Math.Abs(constX - xxx) >= 0.5 || Math.Abs(constY - yyy) >= 0.5) { int a = (int.Parse(AbnormalCount_textBox.Text) + 1); AbnormalCount_textBox.Text = a.ToString(); } var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply(image.ToBitmap()); bmp8bpp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\SavedImages\NG_SavedImage-" + DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss") + ".bmp", ImageFormat.Bmp); double minx = double.Parse(MinX_textBox.Text); double miny = double.Parse(MinY_textBox.Text); double maxx = double.Parse(MaxX_textBox.Text); double maxy = double.Parse(MaxY_textBox.Text); if (xxx < minx) { MinX_textBox.Text = xxx.ToString(); } if (yyy < miny) { MinY_textBox.Text = yyy.ToString(); } if (xxx > maxx) { MaxX_textBox.Text = xxx.ToString(); } if (yyy > maxy) { MaxY_textBox.Text = yyy.ToString(); } })); } count++; Dispatcher.Invoke(new Action(() => { TotalCount_textBox.Text = count.ToString(); display.Image = image; display.Record = record; display.Fit(); })); TextWriter tw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SavedLog.csv", true); tw.WriteLine(count + "," + xxx + "," + yyy + "," + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")); tw.Close(); Thread.Sleep(900); } }