Esempio n. 1
0
        public override IObservable <Contours> Process(IObservable <IplImage> source)
        {
            return(Observable.Defer(() =>
            {
                IplImage temp = null;
                return source.Select(input =>
                {
                    Seq currentContour;
                    temp = IplImageHelper.EnsureImageFormat(temp, input.Size, IplDepth.U8, 1);
                    CV.Copy(input, temp);

                    var storage = new MemStorage();
                    var scanner = CV.StartFindContours(temp, storage, Contour.HeaderSize, Mode, Method, Offset);
                    while ((currentContour = scanner.FindNextContour()) != null)
                    {
                        if (MinArea.HasValue || MaxArea.HasValue)
                        {
                            var contourArea = CV.ContourArea(currentContour, SeqSlice.WholeSeq);
                            if (contourArea < MinArea || contourArea > MaxArea)
                            {
                                scanner.SubstituteContour(null);
                            }
                        }
                    }

                    return new Contours(scanner.EndFindContours(), input.Size);
                });
            }));
        }
Esempio n. 2
0
        public override IObservable <IplImage> Process(IObservable <IplImage> source)
        {
            return(source.Select(input =>
            {
                var output = new IplImage(input.Size, IplDepth.U8, 1);
                distance = IplImageHelper.EnsureImageFormat(distance, input.Size, IplDepth.F32, 1);
                laplacian = IplImageHelper.EnsureImageFormat(laplacian, input.Size, IplDepth.F32, 1);

                CV.DistTransform(input, distance, DistanceType);
                CV.Laplace(distance, laplacian, LaplacianAperture);
                CV.Threshold(laplacian, output, RidgeThreshold, 255, ThresholdTypes.BinaryInv);
                return output;
            }));
        }
Esempio n. 3
0
        public IObservable <KeyPointCollection> Process(IObservable <Tuple <IplImage, IplImage> > source)
        {
            return(Observable.Defer(() =>
            {
                IplImage temp = null;
                IplImage eigen = null;
                Point2f[] corners = null;
                return source.Select(input =>
                {
                    var image = input.Item1;
                    var mask = input.Item2;
                    var result = new KeyPointCollection(image);
                    var rect = RegionOfInterest;
                    if (rect.Width > 0 && rect.Height > 0)
                    {
                        image = image.GetSubRect(rect);
                        mask = mask != null ? mask.GetSubRect(rect) : mask;
                    }
                    else
                    {
                        rect.X = rect.Y = 0;
                    }

                    temp = IplImageHelper.EnsureImageFormat(temp, image.Size, IplDepth.F32, 1);
                    eigen = IplImageHelper.EnsureImageFormat(eigen, image.Size, IplDepth.F32, 1);
                    if (corners == null || corners.Length != MaxFeatures)
                    {
                        corners = new Point2f[MaxFeatures];
                    }

                    int cornerCount = corners.Length;
                    CV.GoodFeaturesToTrack(image, eigen, temp, corners, out cornerCount, QualityLevel, MinDistance, mask);
                    for (int i = 0; i < cornerCount; i++)
                    {
                        corners[i].X += rect.X;
                        corners[i].Y += rect.Y;
                        result.Add(corners[i]);
                    }

                    return result;
                });
            }));
        }
Esempio n. 4
0
        public override IObservable <IplImage> Process(IObservable <IplImage> source)
        {
            return(Observable.Defer(() =>
            {
                IplImage channel1 = null;
                IplImage channel2 = null;
                IplImage channel3 = null;
                IplImage channel4 = null;
                return source.Select(input =>
                {
                    channel1 = IplImageHelper.EnsureImageFormat(channel1, input.Size, IplDepth.U8, 1);
                    channel2 = input.Channels > 1 ? IplImageHelper.EnsureImageFormat(channel2, input.Size, IplDepth.U8, 1) : null;
                    channel3 = input.Channels > 2 ? IplImageHelper.EnsureImageFormat(channel3, input.Size, IplDepth.U8, 1) : null;
                    channel4 = input.Channels > 3 ? IplImageHelper.EnsureImageFormat(channel4, input.Size, IplDepth.U8, 1) : null;

                    var output = new IplImage(input.Size, input.Depth, input.Channels);
                    CV.Split(input, channel1, channel2, channel3, channel4);

                    if (channel1 != null)
                    {
                        CV.ConvertScale(channel1, channel1, Scale.Val0, 0);
                    }
                    if (channel2 != null)
                    {
                        CV.ConvertScale(channel2, channel2, Scale.Val1, 0);
                    }
                    if (channel3 != null)
                    {
                        CV.ConvertScale(channel3, channel3, Scale.Val2, 0);
                    }
                    if (channel4 != null)
                    {
                        CV.ConvertScale(channel4, channel4, Scale.Val3, 0);
                    }
                    CV.Merge(channel1, channel2, channel3, channel4, output);
                    return output;
                });
            }));
        }
Esempio n. 5
0
        public override IObservable <IplImage> Process(IObservable <IplImage> source)
        {
            var propertyChanged = Observable.FromEventPattern <EventArgs>(
                handler => PropertyChanged += new EventHandler(handler),
                handler => PropertyChanged -= new EventHandler(handler));

            return(Observable.Defer(() =>
            {
                IplImage temp = null;
                IplConvKernel strel = null;
                bool updateStrel = false;
                var update = propertyChanged.Subscribe(xs => updateStrel = true);
                return source.Select(input =>
                {
                    if (strel == null || updateStrel)
                    {
                        var size = Size;
                        var anchor = Anchor;
                        updateStrel = false;
                        if (strel != null)
                        {
                            strel.Close();
                        }
                        strel = new IplConvKernel(
                            size.Width,
                            size.Height,
                            anchor.X < 0 ? size.Width / 2 : anchor.X,
                            anchor.Y < 0 ? size.Height / 2 : anchor.Y,
                            Shape);
                    }

                    var output = new IplImage(input.Size, input.Depth, input.Channels);
                    temp = IplImageHelper.EnsureImageFormat(temp, input.Size, input.Depth, input.Channels);
                    CV.MorphologyEx(input, output, temp, strel, Operation, Iterations);
                    return output;
                }).Finally(update.Dispose);
            }));
        }
Esempio n. 6
0
        public override IObservable <RegionActivityCollection> Process(IObservable <IplImage> source)
        {
            return(Observable.Defer(() =>
            {
                var roi = default(IplImage);
                var mask = default(IplImage);
                var currentRegions = default(Point[][]);
                var boundingRegions = default(Rect[]);
                return source.Select(input =>
                {
                    var operation = Operation;
                    var output = new RegionActivityCollection();
                    mask = IplImageHelper.EnsureImageFormat(mask, input.Size, IplDepth.U8, 1);
                    if (operation != ReduceOperation.Sum)
                    {
                        roi = null;
                    }
                    else
                    {
                        roi = IplImageHelper.EnsureImageFormat(roi, input.Size, input.Depth, input.Channels);
                    }
                    if (Regions != currentRegions)
                    {
                        currentRegions = Regions;
                        if (currentRegions != null)
                        {
                            mask.SetZero();
                            CV.FillPoly(mask, currentRegions, Scalar.All(255));
                            boundingRegions = currentRegions.Select(polygon =>
                            {
                                var points = polygon.SelectMany(point => new[] { point.X, point.Y }).ToArray();
                                using (var mat = new Mat(1, polygon.Length, Depth.S32, 2))
                                {
                                    Marshal.Copy(points, 0, mat.Data, points.Length);
                                    return CV.BoundingRect(mat);
                                }
                            }).ToArray();
                        }
                    }

                    if (currentRegions != null)
                    {
                        var activeMask = mask;
                        if (roi != null)
                        {
                            roi.SetZero();
                            CV.Copy(input, roi, mask);
                            activeMask = roi;
                        }

                        var activation = ActivationFunction(operation);
                        for (int i = 0; i < boundingRegions.Length; i++)
                        {
                            var rect = boundingRegions[i];
                            var polygon = currentRegions[i];
                            using (var region = input.GetSubRect(rect))
                                using (var regionMask = activeMask.GetSubRect(rect))
                                {
                                    output.Add(new RegionActivity
                                    {
                                        Roi = polygon,
                                        Rect = rect,
                                        Activity = activation(region, regionMask)
                                    });
                                }
                        }
                    }

                    return output;
                });
            }));
        }