Exemple #1
0
        public static void RNdMatrixToMat(RNdMatrix mat, out Mat[] frames)
        {
            // MatToRNdMatrixの逆変換
            var baseframe = new Mat(mat.Height, mat.Width, MatType.MakeType(MatType.CV_8U, mat.Channels), new Scalar(byte.MinValue));
            var framelist = new List <Mat>();

            int batchsize = mat.BatchSize;
            int ch        = mat.Channels;
            int size      = mat.Width * mat.Height;
            int total     = size * ch;

            var get = mat.Data.Select(x => (byte)(Math.Max(0, Math.Min(1, x)) * byte.MaxValue)).ToArray();

            for (int i = 0; i < batchsize; i++)
            {
                Mat[] cframe = new Mat[ch];
                for (int c = 0; c < ch; c++)
                {
                    cframe[c] = new Mat(mat.Height, mat.Width, MatType.CV_8UC1, new Scalar(byte.MinValue));
                    Marshal.Copy(get, i * total + c * size, cframe[c].Data, size);
                }

                var frame = new Mat();
                Cv2.Merge(cframe.ToArray(), frame);
                framelist.Add(frame);
            }

            frames = framelist.ToArray();
        }
        public Mat Generate(
            [InputPin(PropertyMode = PropertyMode.Default)] int width,
            [InputPin(PropertyMode = PropertyMode.Default)] int height,
            [InputPin(PropertyMode = PropertyMode.Default)] int channels = 1)
        {
            var image = new Mat(height, width, MatType.MakeType(MatType.CV_8U, channels));

            if (channels == 1)
            {
                image.Randu(0, 256);
            }
            else if (channels == 3)
            {
                image.Randu(new Scalar(0, 0, 0), new Scalar(256, 256, 256));
            }
            else if (channels == 4)
            {
                image.Randu(new Scalar(0, 0, 0, 0), new Scalar(256, 256, 256, 256));
            }
            else
            {
                throw new Exception("Not supported channel-count. Expected count: 1, 3 or 4");
            }

            return(image);
        }
Exemple #3
0
        public PixelFormat GetPixelFormat(Mat image)
        {
            var ranges = new Range <double> [image.Channels()];

            var matType = image.Type();

            if (matType.Equals(MatType.MakeType(MatType.CV_8U, 1)) ||
                matType.Equals(MatType.MakeType(MatType.CV_8U, 3)) ||
                matType.Equals(MatType.MakeType(MatType.CV_8U, 4))
                )
            {
                for (int i = 0; i < image.Channels(); ++i)
                {
                    ranges[i] = new Range <double>(byte.MinValue, byte.MaxValue);
                }

                return(new PixelFormat((PixelType)((int)ChannelType.U8 | image.Channels()), image.Channels() == 1 ? PixelChannels.Gray : PixelChannels.Unknown, typeof(byte), ranges, ColorSpace.Unknown));
            }
            else if (matType.Equals(MatType.MakeType(MatType.CV_32F, 1)) ||
                     matType.Equals(MatType.MakeType(MatType.CV_32F, 3)) ||
                     matType.Equals(MatType.MakeType(MatType.CV_32F, 4))
                     )
            {
                for (int i = 0; i < image.Channels(); ++i)
                {
                    ranges[i] = new Range <double>(float.MinValue, float.MaxValue);
                }

                return(new PixelFormat((PixelType)((int)ChannelType.F32 | image.Channels()), image.Channels() == 1 ? PixelChannels.Gray : PixelChannels.Unknown, typeof(float), ranges, ColorSpace.Unknown));
            }

            throw new Exception("PixelFormat is not supported.");
        }
        public Mat Convert(
            [InputPin(PropertyMode = PropertyMode.Never)] Mat image,
            [InputPin(PropertyMode = PropertyMode.Default)] MatDepth depth,
            [InputPin(PropertyMode = PropertyMode.Default)] int channels)
        {
            MatType type   = MatType.MakeType((int)depth, channels);
            var     result = new Mat(image.Size(), type);

            image.ConvertTo(result, type);
            return(result);
        }
Exemple #5
0
        public IImageBuffer MatToImageBuffer(Mat mat)
        {
            var arraySize = mat.Width * mat.Height * mat.Channels();

            var matType = mat.Type();

            if (matType.Equals(MatType.MakeType(MatType.CV_8U, 1)) ||
                matType.Equals(MatType.MakeType(MatType.CV_8U, 3)) ||
                matType.Equals(MatType.MakeType(MatType.CV_8U, 4))
                )
            {
                var dst = new I <byte>(this.GetPixelFormat(mat), mat.Height, mat.Width, mat.Channels());
                if (mat.IsContinuous())
                {
                    Marshal.Copy(mat.Data, dst.Data.Buffer, 0, arraySize);
                }
                else
                {
                    var buffer = dst.Data.Buffer;
                    int stride = dst.Data.Stride[0];
                    for (int i = 0, y = 0; y < mat.Height; ++y, i += stride)
                    {
                        Marshal.Copy(mat.Ptr(y), buffer, i, stride);
                    }
                    return(dst);
                }
                return(dst);
            }
            else if (matType.Equals(MatType.MakeType(MatType.CV_32F, 1)) ||
                     matType.Equals(MatType.MakeType(MatType.CV_32F, 3)) ||
                     matType.Equals(MatType.MakeType(MatType.CV_32F, 4))
                     )
            {
                var dst = new I <float>(this.GetPixelFormat(mat), mat.Height, mat.Width, mat.Channels());
                if (mat.IsContinuous())
                {
                    Marshal.Copy(mat.Data, dst.Data.Buffer, 0, arraySize);
                }
                else
                {
                    var buffer = dst.Data.Buffer;
                    int stride = dst.Data.Stride[0];
                    for (int i = 0, y = 0; y < mat.Height; ++y, i += stride)
                    {
                        Marshal.Copy(mat.Ptr(y), buffer, i, stride);
                    }
                    return(dst);
                }
                return(dst);
            }

            throw new Exception("Unknown Image. This Provider can just convert Mat to ImageBuffer.");
        }
Exemple #6
0
        //イベント
        //public event ZerosMat _zerosMat;

        public CutRect(Mat srcImg)
        {
            //加工画像の情報
            this.srcMat    = new Mat();
            this.srcMat    = srcImg.Clone();
            this.imgWidth  = this.srcMat.Width;
            this.imgHeight = this.srcMat.Height;
            this.channels  = this.srcMat.Channels();
            this.imgDepth  = this.srcMat.Depth();
            //出力用画像
            this.dstMat   = new Mat(this.imgHeight, this.imgWidth, MatType.MakeType(this.imgDepth, this.channels));
            this.countMat = new Mat(this.imgHeight, this.imgWidth, MatType.CV_8UC1);
        }
 public void MakeType()
 {
     Assert.Equal(MatType.CV_8UC(1), MatType.MakeType(MatType.CV_8U, 1));
     Assert.Equal(MatType.CV_8UC(2), MatType.MakeType(MatType.CV_8U, 2));
     Assert.Equal(MatType.CV_8UC(3), MatType.MakeType(MatType.CV_8U, 3));
     Assert.Equal(MatType.CV_8UC(4), MatType.MakeType(MatType.CV_8U, 4));
     Assert.Equal(MatType.CV_8UC(5), MatType.MakeType(MatType.CV_8U, 5));
     Assert.Equal(MatType.CV_8UC(6), MatType.MakeType(MatType.CV_8U, 6));
     Assert.Equal(MatType.CV_8SC(1), MatType.MakeType(MatType.CV_8S, 1));
     Assert.Equal(MatType.CV_8SC(2), MatType.MakeType(MatType.CV_8S, 2));
     Assert.Equal(MatType.CV_8SC(3), MatType.MakeType(MatType.CV_8S, 3));
     Assert.Equal(MatType.CV_8SC(4), MatType.MakeType(MatType.CV_8S, 4));
     Assert.Equal(MatType.CV_8SC(5), MatType.MakeType(MatType.CV_8S, 5));
     Assert.Equal(MatType.CV_8SC(6), MatType.MakeType(MatType.CV_8S, 6));
     Assert.Equal(MatType.CV_16UC(1), MatType.MakeType(MatType.CV_16U, 1));
     Assert.Equal(MatType.CV_16UC(2), MatType.MakeType(MatType.CV_16U, 2));
     Assert.Equal(MatType.CV_16UC(3), MatType.MakeType(MatType.CV_16U, 3));
     Assert.Equal(MatType.CV_16UC(4), MatType.MakeType(MatType.CV_16U, 4));
     Assert.Equal(MatType.CV_16UC(5), MatType.MakeType(MatType.CV_16U, 5));
     Assert.Equal(MatType.CV_16UC(6), MatType.MakeType(MatType.CV_16U, 6));
     Assert.Equal(MatType.CV_16SC(1), MatType.MakeType(MatType.CV_16S, 1));
     Assert.Equal(MatType.CV_16SC(2), MatType.MakeType(MatType.CV_16S, 2));
     Assert.Equal(MatType.CV_16SC(3), MatType.MakeType(MatType.CV_16S, 3));
     Assert.Equal(MatType.CV_16SC(4), MatType.MakeType(MatType.CV_16S, 4));
     Assert.Equal(MatType.CV_16SC(5), MatType.MakeType(MatType.CV_16S, 5));
     Assert.Equal(MatType.CV_16SC(6), MatType.MakeType(MatType.CV_16S, 6));
     Assert.Equal(MatType.CV_32SC(1), MatType.MakeType(MatType.CV_32S, 1));
     Assert.Equal(MatType.CV_32SC(2), MatType.MakeType(MatType.CV_32S, 2));
     Assert.Equal(MatType.CV_32SC(3), MatType.MakeType(MatType.CV_32S, 3));
     Assert.Equal(MatType.CV_32SC(4), MatType.MakeType(MatType.CV_32S, 4));
     Assert.Equal(MatType.CV_32SC(5), MatType.MakeType(MatType.CV_32S, 5));
     Assert.Equal(MatType.CV_32SC(6), MatType.MakeType(MatType.CV_32S, 6));
     Assert.Equal(MatType.CV_32FC(1), MatType.MakeType(MatType.CV_32F, 1));
     Assert.Equal(MatType.CV_32FC(2), MatType.MakeType(MatType.CV_32F, 2));
     Assert.Equal(MatType.CV_32FC(3), MatType.MakeType(MatType.CV_32F, 3));
     Assert.Equal(MatType.CV_32FC(4), MatType.MakeType(MatType.CV_32F, 4));
     Assert.Equal(MatType.CV_32FC(5), MatType.MakeType(MatType.CV_32F, 5));
     Assert.Equal(MatType.CV_32FC(6), MatType.MakeType(MatType.CV_32F, 6));
     Assert.Equal(MatType.CV_64FC(1), MatType.MakeType(MatType.CV_64F, 1));
     Assert.Equal(MatType.CV_64FC(2), MatType.MakeType(MatType.CV_64F, 2));
     Assert.Equal(MatType.CV_64FC(3), MatType.MakeType(MatType.CV_64F, 3));
     Assert.Equal(MatType.CV_64FC(4), MatType.MakeType(MatType.CV_64F, 4));
     Assert.Equal(MatType.CV_64FC(5), MatType.MakeType(MatType.CV_64F, 5));
     Assert.Equal(MatType.CV_64FC(6), MatType.MakeType(MatType.CV_64F, 6));
 }
        public Mat Convert(
            [InputPin(PropertyMode = PropertyMode.Allow)] Array array,
            [InputPin(PropertyMode = PropertyMode.Default)] MatDepth depth
            )
        {
            var arrayType = array.GetType();

            var dimensions = arrayType.GetArrayRank();

            if (dimensions == 1)
            {
                var type = MatType.MakeType((int)depth, 1);
                var m    = new Mat(1, array.Length, type, array.Cast <float>().ToArray());
                return(m);
            }

            throw new NotImplementedException(string.Format("Cannot convert '{0}' to 'Mat'", arrayType));
        }
Exemple #9
0
        private void ReadFromCVMat(Mat Frame)
        {
            var cnl = Frame.Channels();

            if (cnl != Channels)
            {
                if (cnl == 1)
                {
                    Cv2.CvtColor(Frame, Frame, ColorConversionCodes.GRAY2BGR);
                }
                else if (cnl == 3)
                {
                    Cv2.CvtColor(Frame, Frame, ColorConversionCodes.BGR2GRAY);
                }
            }

            var size = new Size(Width, Height);

            if (Frame.Size() != size)
            {
                Frame = Frame.Resize(size);
            }
            if (Frame.Type() != MatType.MakeType(MatType.CV_64F, Channels))
            {
                Frame.ConvertTo(Frame, MatType.MakeType(MatType.CV_64F, Channels), 1.0 / byte.MaxValue);
            }

            Mat[]      frames = Frame.Split();
            double[][] vector;
            vector = new double[Channels][];
            for (int c = 0; c < Channels; c++)
            {
                vector[c] = new double[Area];
                Marshal.Copy(frames[c].Data, vector[c], 0, Area);
            }

            FrameToBuffer(vector);
        }
Exemple #10
0
        public IEnumerable <ITypeConverter> GetConverters()
        {
            return(new[]
            {
                // Point
                TypeConverter.Create <Point, Xamla.Types.Int2>(p => new Int2(p.X, p.Y)),
                TypeConverter.Create <Xamla.Types.Int2, Point>(p => new Point(p.X, p.Y)),
                TypeConverter.Create <Xamla.Types.Float2, Point>(p => new Point(p.X, p.Y)),
                TypeConverter.Create <Point, Xamla.Types.Float2>(p => new Float2(p.X, p.Y)),

                // Point2d
                TypeConverter.Create <Xamla.Types.Float2, Point2d>(p => new Point2d(p.X, p.Y)),
                TypeConverter.Create <Point2d, Xamla.Types.Float2>(p => new Float2(p.X, p.Y)),

                // Point2f
                TypeConverter.Create <Xamla.Types.Float2, Point2f>(p => new Point2f((float)p.X, (float)p.Y)),
                TypeConverter.Create <Point2f, Xamla.Types.Float2>(p => new Float2(p.X, p.Y)),

                // Point3i
                TypeConverter.Create <Xamla.Types.Int3, Point3i>(p => new Point3i(p.X, p.Y, p.Z)),
                TypeConverter.Create <Point3i, Xamla.Types.Int3>(p => new Int3(p.X, p.Y, p.Z)),

                // Point3f
                TypeConverter.Create <Xamla.Types.Float3, Point3f>(p => new Point3f((float)p.X, (float)p.Y, (float)p.Z)),
                TypeConverter.Create <Point3f, Xamla.Types.Float3>(p => new Float3(p.X, p.Y, p.Z)),

                // Point3d
                TypeConverter.Create <Xamla.Types.Float3, Point3d>(p => new Point3d(p.X, p.Y, p.Z)),
                TypeConverter.Create <Point3d, Xamla.Types.Float3>(p => new Float3(p.X, p.Y, p.Z)),

                // Size
                TypeConverter.Create <Size, Xamla.Types.Int2>(s => new Int2(s.Width, s.Height)),
                TypeConverter.Create <Xamla.Types.Int2, Size>(i2 => new Size(i2.X, i2.Y)),

                // Size2f
                TypeConverter.Create <Size2f, Xamla.Types.Float2>(s => new Float2(s.Width, s.Height)),
                TypeConverter.Create <Xamla.Types.Float2, Size2f>(p => new Size2f(p.X, p.Y)),

                TypeConverter.Create <IImageBuffer, Mat>(this.ImageBufferToMat),
                TypeConverter.Create <Mat, IImageBuffer>(this.MatToImageBuffer),
                TypeConverter.Create <Mat, InputArray>(InputArray.Create),
                TypeConverter.Create <Mat, InputOutputArray>(x => x),
                TypeConverter.Create <OutputArray, Mat>(x => x.GetMat()),
                TypeConverter.Create <OutputArray, InputArray>(x => InputArray.Create(x.GetMat())),
                TypeConverter.Create <IntRect, Rect>(x => new Rect(x.Left, x.Top, x.Size.X, x.Size.Y)),
                TypeConverter.Create <Rect, IntRect>(x => new IntRect(x.Left, x.Top, x.Right, x.Bottom)),
                TypeConverter.Create <Point2f[], Mat>(points => {
                    var type = MatType.MakeType(MatType.CV_32F, 2);
                    var m = new Mat(1, points.Length, type, points);
                    return m;
                }),
                TypeConverter.Create <Mat, Point2f[]>(mat => {
                    var array = new Point2f[mat.Cols * mat.Rows];
                    mat.GetArray(0, 0, array);
                    return array;
                }),
                TypeConverter.Create <RotatedFloatRect, RotatedRect>(x => new RotatedRect(new Point2f((float)x.Center.X, (float)x.Center.Y), new Size2f((float)x.Size.X, (float)x.Size.Y), (float)x.AngleDegrees)),
                TypeConverter.Create <RotatedRect, RotatedFloatRect>(x => new RotatedFloatRect(x.Center.X, x.Center.Y, x.Size.Width, x.Size.Height, RotatedFloatRect.DegreeToRadian(x.Angle))),
                TypeConverter.Create <KeyPoint, Point2f>(keyPoint => keyPoint.Pt),

                TypeConverter.Create <Point, Point2f>(p => new Point2f(p.X, p.Y)),
                TypeConverter.Create <Point2f, Point>(p => new Point(p.X, p.Y)),
                TypeConverter.Create <Point, Point2d>(p => new Point2d(p.X, p.Y)),
                TypeConverter.Create <Point2d, Point>(p => new Point(p.X, p.Y)),
                TypeConverter.Create <Point2d, Point2f>(p => new Point2f((float)p.X, (float)p.Y)),
                TypeConverter.Create <Point2f, Point2d>(p => new Point2d(p.X, p.Y)),

                TypeConverter.Create <Point3i, Point3f>(p => new Point3f(p.X, p.Y, p.Z)),
                TypeConverter.Create <Point3f, Point3i>(p => new Point3i((int)p.X, (int)p.Y, (int)p.Z)),
                TypeConverter.Create <Point3d, Point3i>(p => new Point3i((int)p.X, (int)p.Y, (int)p.Z)),
                TypeConverter.Create <Point3i, Point3d>(p => new Point3d(p.X, p.Y, p.Z)),
                TypeConverter.Create <Point3f, Point3d>(p => new Point3d(p.X, p.Y, p.Z)),
                TypeConverter.Create <Point3d, Point3f>(p => new Point3f((float)p.X, (float)p.Y, (float)p.Z)),

                TypeConverter.Create <Mat, M>(mat => M.FromArray(this.MatToA(mat))),
                TypeConverter.Create <M, Mat>(m => this.AToMat(m.UnderlyingArray)),
                //TypeConverter.Create<float[], Mat>(f => new Mat(f.Length, 1, MatType.CV_32FC1, f))
            });
        }
Exemple #11
0
        public Mat AToMat(A a)
        {
            if (a.Rank > 3)
            {
                throw new Exception("Mat does not support Rank > 3");
            }

            int depth;

            if (a.ElementType == typeof(short))
            {
                depth = MatType.CV_16S;
            }
            else if (a.ElementType == typeof(ushort))
            {
                depth = MatType.CV_16U;
            }
            else if (a.ElementType == typeof(float))
            {
                depth = MatType.CV_32F;
            }
            else if (a.ElementType == typeof(int))
            {
                depth = MatType.CV_32S;
            }
            else if (a.ElementType == typeof(double))
            {
                depth = MatType.CV_64F;
            }
            else if (a.ElementType == typeof(sbyte))
            {
                depth = MatType.CV_8S;
            }
            else if (a.ElementType == typeof(byte))
            {
                depth = MatType.CV_8U;
            }
            else
            {
                throw new Exception("ElementType is not supported for Mat.");
            }

            int cols = 1;

            if (a.Rank > 1)
            {
                cols = a.Dimension[1];
            }

            int channels = 1;

            if (a.Rank == 3)
            {
                channels = a.Dimension[2];
            }

            var matType = MatType.MakeType(depth, channels);
            var mat     = new Mat(a.Dimension[0], cols, matType, a.Pin().Pointer);

            return(mat.Clone());
        }