Пример #1
0
        public async Task <int> AcquisitionAndInspectAsync(int surfaceTypeIndex)
        {
            // Acquisition
            Task.Run(() => _acquisitionStartedEvent.OnNext(surfaceTypeIndex));
            var imageInfo = await _camera.AcquisitionAsync();

            switch (surfaceTypeIndex)
            {
            case 0:
                Machine.Inspection_SurfaceFront_GrabFinishedPcEventDevice.WriteTrue();
                break;

            case 1:
                Machine.Inspection_SurfaceBack_GrabFinishedPcEventDevice.WriteTrue();
                break;
            }

            Task.Run(() =>
            {
                _acquisitionCompletedEvent.OnNext(imageInfo.ToEntity());
                _calibrationStartedEvent.OnNext(surfaceTypeIndex);
            });

            // Calibration

            HImage calibImage;

            if (MachineConfig.MV_SimulationCalibrationEnabled)
            {
                calibImage = imageInfo.To8BppHImage();
            }
            else
            {
                var    swOriToHImageInfo = new NotifyStopwatch("imageInfo.To8BppHImage()");
                HImage oriImage          = imageInfo.To8BppHImage();
                swOriToHImageInfo.Dispose();

                //                HImage reducedImage = oriImage.Rectangle1Domain(2000, 2000, 5000, 5000);

                var calib   = new HalconImageCalibrator();
                var swCalib = new NotifyStopwatch("AcquisitionAndInspectAsync.CalibrateImage(imageInfo)");
                calibImage = calib.CalibrateImage(oriImage, MachineConfig.MV_CalibrationInterpolation);
                swCalib.Dispose();

                oriImage.Dispose();
            }

            var swCalibToImageInfo = new NotifyStopwatch("calibImage.ToImageInfo()");
            var calibImageInfo     = calibImage.ToImageInfo();

            swCalibToImageInfo.Dispose();

            calibImageInfo.SurfaceTypeIndex = surfaceTypeIndex;

            Debug.WriteLine("SurfaceType " + surfaceTypeIndex + ": StartGrabAsync() Finished");

            Task.Run(() =>
            {
                _calibrationCompletedEvent.OnNext(calibImageInfo.ToEntity());
                _inspectionStartedEvent.OnNext(surfaceTypeIndex);
            });

            Task.Run(
                () =>
            {
                InspectInfo inspectionInfo;
                if (MachineConfigProvider.MachineConfig.MV_SimulationInspectorEnabled)
                {
                    inspectionInfo = new InspectInfo();
                    calibImage.Dispose();
                }
                else
                {
                    InspectionResult inspectionResult;

                    try
                    {
                        Debug.WriteLine("_inspectionController.Inspect() start");
                        var sw3 = new NotifyStopwatch("_inspectionController.Inspect()");
                        _inspectionController
                        .SetInspectionSchema(_inspectionSchema.DeepClone())
                        .SetImage(calibImage)
                        .CreateCoordinate()
                        .Inspect();
                        Debug.WriteLine("_inspectionController.Inspect() OK");
                        sw3.Dispose();
                        calibImage.Dispose();
                        inspectionResult = _inspectionController.InspectionResult;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("_inspectionController error.\n\n" + e.Message + "\n\n" +
                                        e.InnerException.Message);
                        Debug.WriteLine("_inspectionController error.\n\n" + e.Message + "\n\n" +
                                        e.InnerException.Message);

                        inspectionResult = new InspectionResult();
                    }

                    Debug.WriteLine("_inspectionController.GetInspectionInfo() Start");
                    inspectionInfo = inspectionResult.GetInspectionInfo(_inspectionController.Coordinate);
                    Debug.WriteLine("_inspectionController.GetInspectionInfo() OK");
                }

                var surfaceInspectInfo = new SurfaceInspectInfo()
                {
                    SurfaceTypeIndex = surfaceTypeIndex,
                    ImageInfo        = calibImageInfo.ToEntity(),
                    InspectInfo      = inspectionInfo,
                    WorkpieceIndex   =
                        _inspectCounter /
                        MachineConfigProvider.MachineConfig
                        .MV_AcquisitionCountPerWorkpiece
                };

                Task.Run(() => _inspectionCompletedEvent.OnNext(surfaceInspectInfo));

                Debug.WriteLine("AcquisitionAndInspectAsync.HandleInspect() above");
                HandleInspect(surfaceInspectInfo);
            });

            return(calibImageInfo.Index);
        }
Пример #2
0
        private void Inspect(InspectionSchema schema)
        {
            //IndicatorViewer.Loaded += (sender, args) => IndicatorViewer.ZoomFit();
            //                        IndicatorViewer.Loaded += (sender, args) => IndicatorViewer.ZoomOut();

            RegionIndicators.Clear();
            LineIndicators.Clear();
            CircleIndicators.Clear();
            DefectIndicators.Clear();
            ObjectIndicators.Clear();

            BitmapSource bs;

            try
            {
                bs = new BitmapImage(new Uri(schema.TestImageFilePath, UriKind.RelativeOrAbsolute));
            }
            catch (FileNotFoundException e)
            {
                throw new HalconInspectorException("Image file not exist", e);
            }

            if (Math.Abs(bs.DpiX - 96) > 0.00001 || Math.Abs(bs.DpiY - 96) > 0.00001)
            {
                var sw1 = new NotifyStopwatch("BitmapSource convert to Dpi96");
                BitmapSourceInfo bsi = bs.ToGray8BppBitmapSourceInfo();
                bsi.DpiX = 96;
                bsi.DpiY = 96;
                var bitmapSourceDpi96 = bsi.GetBitmapSource();
                bs = bitmapSourceDpi96;
                sw1.Stop();
                sw1.Dispose();
            }

            IndicatorViewer.BitmapSource = bs;

            var sw2    = new NotifyStopwatch("BitmapSource.ToHImage()");
            var hImage = bs.ToHImage();

            sw2.Stop();
            sw2.Dispose();


            try
            {
                var sw = new NotifyStopwatch("InspectionController.Inspect()");
                InspectionController
                .SetInspectionSchema()
                .SetImage(hImage)
                .CreateCoordinate()
                .Inspect()
                ;
                sw.Stop();
                sw.Dispose();

                // CoordinateCircles
                Show_CircleSearchingDefinitions(
                    InspectionController.InspectionResult.GetCoordinateCircleSearchingDefinitions());
                Show_CircleSearchingResults(InspectionController.InspectionResult.CoordinateCircles);

                // Circles
                Show_CircleSearchingDefinitions(
                    InspectionController.InspectionResult.GetCircleSearchingDefinitions(), Brushes.Orange);
                Show_CircleSearchingResults(InspectionController.InspectionResult.Circles, Brushes.DodgerBlue);

                // CoordinateEdges
                Show_EdgeSearchingDefinitions(InspectionController.InspectionResult.GetCoordinateEdges());
                Show_EdgeSearchingResults(InspectionController.InspectionResult.CoordinateEdges);

                // Edges
                Show_EdgeSearchingDefinitions(InspectionController.InspectionResult.GetEdgeSearchingDefinitions());
                Show_EdgeSearchingResults(InspectionController.InspectionResult.Edges);

                // DistanceBetweenPoints
                Show_DistanceBetweenPointsResults(InspectionController.InspectionResult.DistanceBetweenPointsResults);

                // Defects
                Show_DefectResults(InspectionController.InspectionResult.RegionDefectResults);

                // Parts
                Show_PartSearchingDefinitions(InspectionController.InspectionResult.GetPartSearchingDefinitions());
                Show_PartSearchingResults(InspectionController.InspectionResult.Parts);

                // RegionTargets
                Show_RegionTargetDefinitions(InspectionController.InspectionResult.GetRegionTargetDefinitions());
                Show_RegionTargetResults(InspectionController.InspectionResult.RegionTargets);
            }
            catch (CreateCoordinateFailedException e)
            {
                Show_CircleSearchingDefinitions(
                    InspectionController.InspectionResult.GetCoordinateCircleSearchingDefinitions());
                Show_CircleSearchingResults(InspectionController.InspectionResult.CoordinateCircles);

                Show_EdgeSearchingDefinitions(InspectionController.InspectionResult.GetCoordinateEdges());
                Show_EdgeSearchingResults(InspectionController.InspectionResult.CoordinateEdges);

                MessageBox.Show(e.Message);
            }
        }
Пример #3
0
        public void Run(string imageDir, InspectionSchema schema)
        {
            IList <string> fileNames;

            try
            {
                var strings = Directory.GetFiles(imageDir).ToList();

                fileNames = strings.Where(x =>
                {
                    if (x.EndsWith(".bmp"))
                    {
                        return(true);
                    }

                    if (x.EndsWith(".tif"))
                    {
                        return(true);
                    }

                    if (x.EndsWith(".jpg"))
                    {
                        return(true);
                    }

                    return(false);
                }).ToList();
            }
            catch (Exception)
            {
                MessageBox.Show("Image directory cannot load files! " + imageDir);
                return;
            }


            // Inspect
            IList <InspectionResult> inspectionResults = new List <InspectionResult>();

//            IList<ImageInfo> imageInfos = new List<ImageInfo>();
            IList <HImage> images = new List <HImage>();

            foreach (var fileName in fileNames)
            {
                string name = fileName;

                var fn = (string)name;
                Debug.WriteLine("Task.Started: " + fn);

//                BitmapImage bi = null;
//                try
//                {
//                    Debug.WriteLine("BitmapImage() begin");
//                    bi = new BitmapImage(new Uri(fn, UriKind.RelativeOrAbsolute));
//                    Debug.WriteLine("BitmapImage() end");
//                }
//                catch (Exception e2)
//                {
//                    MessageBox.Show("BitmapImage loading error: " + fn);
//                }
//
//                Debug.WriteLine("ToImageInfoWith8Bpp() begin");
//                var imageInfo = bi.ToImageInfoWith8Bpp();
//                Debug.WriteLine("ToImageInfoWith8Bpp() end");
//
//                imageInfos.Add(imageInfo);

                images.Add(new HImage(name));
            }

            IList <Task> tasks = new List <Task>();

            foreach (var imageInfo in images)
            {
//                string name = fileName;
                var imageInfo2 = imageInfo;
                var task       = new Task(
                    (x) =>
                {
                    HImage imageInfo3 = (HImage)imageInfo2;
//                        Debug.WriteLine("Task.Started: " + fn);
//
//                        BitmapImage bi = null;
//                        try
//                        {
//                            Debug.WriteLine("BitmapImage() begin");
//                            bi = new BitmapImage(new Uri(fn, UriKind.RelativeOrAbsolute));
//                            Debug.WriteLine("BitmapImage() end");
//                        }
//                        catch (Exception e2)
//                        {
//                            MessageBox.Show("BitmapImage loading error: " + fn);
//                        }
//
//                        Debug.WriteLine("ToImageInfoWith8Bpp() begin");
//                        var imageInfo = bi.ToImageInfoWith8Bpp();
//                        Debug.WriteLine("ToImageInfoWith8Bpp() end");

                    using (var inspectionController = new InspectionController())
                    {
                        inspectionController
                        .SetInspectionSchema(schema.DeepClone())
                        .SetImage(imageInfo3)
                        .CreateCoordinate()
                        .Inspect()
                        ;

                        inspectionController.InspectionResult.Comment = "";

                        inspectionResults.Add(inspectionController.InspectionResult);
                    }

                    //                var targetTask = new SearchingTask();
                    //                foreach (var csd in schema.CircleSearchingDefinitions)
                    //                {
                    //                    var relativeVector = new Vector(csd.BaselineX*1000.0/16.0, csd.BaselineY*1000.0/16.0);
                    //                    var originalVector = coord.GetOriginalVector(relativeVector);
                    //                    csd.CenterX = originalVector.X;
                    //                    csd.CenterY = originalVector.Y;
                    //                }
                    //                targetTask.CircleDefinitions.AddRange(schema.CircleSearchingDefinitions);
                    //
                    //
                    //                var targetResult = inspector.Search(imageInfo, targetTask);
                    //
                    //                targetResult.CircleSearchingResults.UpdateRelativeCoordinate(coord);
                }, imageInfo);
                tasks.Add(task);
                task.Start();
            }

            Task.WaitAll(tasks.ToArray());

            var coordinateResultGroups = inspectionResults.Select(x => x.CoordinateCircles).ToList();
            List <CircleSearchingResultCollection> objectsResultGroups = inspectionResults.Select(x => x.Circles).ToList();

            foreach (var task in objectsResultGroups)
            {
                foreach (var t in task)
                {
                    Debug.WriteLine("objectsResultGroups Circle " + t.Index + " X: " + t.Definition.CenterX);
                    Debug.WriteLine("objectsResultGroups Circle " + t.Index + " Y: " + t.Definition.CenterY);
                }
            }

            DateTime dateTime  = DateTime.Now;
            string   reportDir = "_reports" + dateTime.ToString("_yyyy-MM-dd_HH.mm.ss");
            var      exportDir = Path.Combine(imageDir, reportDir);

            Directory.CreateDirectory(exportDir);

            // SaveToCSV
            ReportManager.SaveToCSV(coordinateResultGroups, exportDir, "Coordinate");
            ReportManager.SaveToCSV(objectsResultGroups, exportDir, "Objects");
            Debug.WriteLine("SaveToCSV() end");


            var distGroups = inspectionResults.Select(x => x.DistanceBetweenPointsResults).ToList();

            ReportManager.SaveCsvGroupByEdge(distGroups, exportDir, "Edges");
            Debug.WriteLine("SaveCsvGroupByEdge() end");


            List <RegionDefectResult> defectsGroups = inspectionResults.SelectMany(x => x.RegionDefectResults).ToList();
            var dgs = defectsGroups.Select(x => x.DefectResults).ToList();

            ReportManager.SaveDefectResultsToCsvGroupByWorkpiece(dgs, exportDir, "Defects");
            Debug.WriteLine("SaveDefectResultsToCsvGroupByWorkpiece() end");

            // SaveToXaml
            ReportManager.SaveToXaml(coordinateResultGroups, exportDir, "Coordinate");
            ReportManager.SaveToXaml(objectsResultGroups, exportDir, "Objects");
            ReportManager.SaveToXaml(inspectionResults, exportDir, "All");
            Debug.WriteLine("SaveToXaml() end");


            var cs = CoordinateCircleCalculator.Calculate(inspectionResults);


            //            var allResultGroup = new List<CircleSearchingResultCollection>();
            //            allResultGroup.AddRange(coordinateResultGroups);
            //            allResultGroup.AddRange(objectsResultGroups);
            //            ReportManager.SaveToCSV(allResultGroup, exportDir, "All");
            //            ReportManager.SaveToXaml(allResultGroup, exportDir, "All");
        }