示例#1
0
        /// <summary>
        /// Makes image transpose and creates mask image
        /// </summary>
        /// <param name="inputImage"></param>
        /// <param name="maskImage"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Execute(List <NamedData> data, string name)
        {
            try
            {
                Image <Gray, ushort>[] inputImages = GetEmguUShortImages("RawImages", data);
                int imageCounter = inputImages?.Length ?? 0;

                if (imageCounter == 0)
                {
                    _logger?.InfoLog("No raw image was found dynamicresult!", ClassName);
                    return(false);
                }

                Image <Gray, byte>[] maskImages = new Image <Gray, byte> [imageCounter];

                for (int m = 0; m < imageCounter; m++)
                {
                    CvInvoke.Transpose(inputImages[m], inputImages[m]);

                    // calculate historamm for binarythreshold
                    _hist.Calculate <ushort>(new[] { inputImages[0] }, false, null);

                    float thresh;
                    _thresholdcalculator.Execute(_hist, out thresh);


                    if (_showImages)
                    {
                        GeneralImageHandling.SaveHistogram(name, "PreProcessor", "Histogram", _hist, _logger);
                    }

                    double maskValue = 255.0;
                    if (thresh > 255)
                    {
                        thresh /= 16;
                    }

                    // tempimage for thresholding:
                    _tempimage = inputImages[m].Convert <Gray, byte>();
                    CvInvoke.Threshold(_tempimage, _thresholdedImage, thresh, maskValue, ThresholdType.Binary);
                    _tempimage?.Dispose();

                    CvInvoke.Erode(_thresholdedImage, _dilatedImage, null, new Point(-1, -1), 3, BorderType.Default, new MCvScalar(0));
                    CvInvoke.Dilate(_dilatedImage, _thresholdedImage, null, new Point(-1, -1), 4, BorderType.Default, new MCvScalar(0));

                    maskImages[m] = _thresholdedImage;

                    maskImages[m].Reduce(_reducedMask, ReduceDimension.SingleCol, ReduceType.ReduceAvg);

                    // this section tries to mask out the belts:
                    int    count           = 0;
                    double MagicThreshold1 = 0.4;   // 40%
                    double MagicThreshold2 = 0.18;  // 15%
                    for (int i = 0; i < _reducedMask.Height; i++)
                    {
                        if (_reducedMask[i, 0] > (1 - MagicThreshold1) * maskValue)
                        {
                            count++;
                        }
                    }
                    if (count < maskImages[m].Height * (1 - MagicThreshold2))
                    {
                        Rectangle fullRoi   = new Rectangle(0, 0, maskImages[m].Width, maskImages[m].Height);
                        Rectangle rectLeft  = new Rectangle(0, _beltCoordinates.LeftBeltStart, maskImages[m].Width, _beltCoordinates.LeftBeltEnd - _beltCoordinates.LeftBeltStart);
                        Rectangle rectRight = new Rectangle(0, _beltCoordinates.RightBeltStart, maskImages[m].Width, _beltCoordinates.RightBeltEnd - _beltCoordinates.RightBeltStart);

                        maskImages[m].ROI = rectLeft;
                        maskImages[m].SetValue(0.0);

                        maskImages[m].ROI = rectRight;
                        maskImages[m].SetValue(0.0);

                        maskImages[m].ROI = fullRoi; // new Rectangle(0, 0, maskImage.Width, maskImage.Height);
                    }


                    if (_showImages)
                    {
                        ImageViewer.Show(inputImages[m], "ImagePreProcessor - transposed image");
                        ImageViewer.Show(maskImages[m], "ImagePreProcessor - maskImage");

                        //SaveMaskImage(name, maskImages[m], "MaskImage");
                        GeneralImageHandling.SaveImage(name, "ImagePreProcessor", "MaskImage", maskImages[m], _logger);
                    }
                }

                data.Add(new EmguByteNamedData(maskImages, "Maskimages", "MaskImages"));

                return(true);
            }
            catch (Exception ex)
            {
                _logger?.ErrorLog($"Exception occured: {ex}", ClassName);
                return(false);
            }
        }