Beispiel #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();
        }
Beispiel #2
0
 /// <summary>
 /// framesの画像群は全て同サイズ、同チャネル、同Depthであることが前提
 /// </summary>
 /// <param name="frame"></param>
 /// <param name="mat"></param>
 public static void MatToRNdMatrix(Mat[] frames, out RNdMatrix mat)
 {
     // mat[b,c,w,h] = b * () + c * () + w * () + hとなるように並び替え
     if (frames.Count() > 0)
     {
         int batchcount = frames.Count();
         int ch         = frames[0].Channels();
         mat = new Components.RNdMatrix(batchcount, ch, frames[0].Width, frames[0].Height);
         int size  = (int)frames[0].Total();
         int total = size * frames[0].Channels();
         unsafe
         {
             byte[] get = new byte[mat.Data.Length];
             for (int i = 0; i < batchcount; i++)
             {
                 var cframe = frames[i].Split();
                 for (int c = 0; c < ch; c++)
                 {
                     Marshal.Copy(cframe[c].Data, get, i * total + c * size, size);
                 }
             }
             mat.Data = get.Select(x => (Real)((double)x / byte.MaxValue)).ToArray();
         }
     }
     else
     {
         mat = null;
     }
 }
Beispiel #3
0
        public static void Show(RNdMatrix mat, string title = "window", int locx = -1, int locy = -1)
        {
            Mat[] frame;
            Converter.RNdMatrixToMat(mat, out frame);

            Cv2.ImShow(title, frame[0]);
            if (locx >= 0 && locy >= 0)
            {
                Cv2.MoveWindow(title, locx, locy);
            }
            Cv2.WaitKey(1);
        }
Beispiel #4
0
        public static void RNdMatrixToVMat(RNdMatrix mat, out Mat frames)
        {
            int batchsize = mat.BatchSize;
            int ch = mat.Channels;
            int w = mat.Width, h = mat.Height;
            int size  = w * h;
            int total = size * ch;

            frames = new Mat(new Size(w, h * ch), MatType.CV_8UC3, new Scalar(byte.MinValue));

            var framelist = new List <Mat>();

            byte[] get_p, get_m;
            var    s_p = mat.Data.Select(x => ((x > 0 ? x : 0))).ToArray();
            var    s_m = mat.Data.Select(x => ((x < 0 ? -x : 0))).ToArray();

            if (w == 1)
            {
            }
            var max_p = s_p.Max();
            var max_m = s_m.Max();
            var min_p = s_p.Min(x => x == 0 ? byte.MaxValue : x);
            var min_m = s_m.Min(x => x == 0 ? byte.MaxValue : x);
            var max   = Math.Max(max_p, max_m);
            //var min = Math.Min(min_p, min_m);
            var min = 0;
            var dlt = max - min;

            if (dlt > 0)
            {
                get_p = s_p.Select(x => (byte)(((x - min) / dlt) * byte.MaxValue)).ToArray();
                get_m = s_m.Select(x => (byte)(((x - min) / dlt) * byte.MaxValue)).ToArray();
            }
            else
            {
                get_p = s_p.Select(x => (byte)(x * byte.MaxValue)).ToArray();
                get_m = s_m.Select(x => (byte)(x * byte.MaxValue)).ToArray();;
            }
            //if (max_p > 1 || max_m > 1)
            //{
            //    var max = Math.Max(max_p, max_m);
            //    get_p = s_p.Select(x => (byte)((x / max) * byte.MaxValue)).ToArray();
            //    get_m = s_m.Select(x => (byte)((x / max) * byte.MaxValue)).ToArray();
            //}
            //else
            //{
            //    get_p = s_p.Select(x => (byte)(x * byte.MaxValue)).ToArray();
            //    get_m = s_m.Select(x => (byte)(x * byte.MaxValue)).ToArray();
            //}

            for (int c = 0; c < ch; c++)
            {
                var cframe_t = new Mat(mat.Height, mat.Width, MatType.CV_8UC1, new Scalar(byte.MinValue));
                var cframe_r = cframe_t.Clone();
                var cframe_g = cframe_t.Clone();
                var cframe_b = cframe_t.Clone();

                Marshal.Copy(get_p, c * size, cframe_r.Data, size);
                Marshal.Copy(get_m, c * size, cframe_b.Data, size);

                var cframe = new Mat(mat.Height, mat.Width, MatType.CV_8UC3);
                Cv2.Merge(new Mat[] { cframe_b, cframe_g, cframe_r }, cframe);
                frames[new Rect(new Point(0, c * mat.Height), cframe.Size())] = cframe;
            }
        }
Beispiel #5
0
        public bool LoadCapture(int batchcount, int inchannels, int inw, int inh, int outchannels, int outw, int outh, bool doFlipX, bool doFlipY, double doRot, bool doAffine, out RNdMatrix smat, out RNdMatrix tmat)
        {
            bool ret = true;

            List <Mat> sframes = new List <Mat>();
            List <Mat> tframes = new List <Mat>();

            for (int i = 0; i < batchcount; i++)
            {
                Mat mat = new Mat(), mat1, mat2;
                while (!capture.Read(mat))
                {
                }
                mat1 = mat.Clone();
                mat2 = mat.Clone();

                bool   flipx = false;
                bool   flipy = false;
                int    oflux = 0, ofluy = 0;
                int    ofrdx = 0, ofrdy = 0;
                double scl = 2.5;
                double rotangle = 0;
                Point  lu = new Point(), rd = new Point();

                if (doFlipX)
                {
                    flipx = (State.RandomSource.NextDouble() > 0.5 ? true : false);
                }
                if (doFlipY)
                {
                    flipy = (State.RandomSource.NextDouble() > 0.5 ? true : false);
                }
                rotangle = (State.RandomSource.NextDouble() * 2 - 1) * doRot;
                if (doAffine)
                {
                    if (inw > 1 && outw > 1 && inh > 1 && outh > 1)
                    {
                        oflux = State.RandomSource.Next(Math.Max(0, (int)(Math.Min(inw, outw) / (scl) - 1)));
                        ofluy = State.RandomSource.Next(Math.Max(0, (int)(Math.Min(inh, outh) / (scl) - 1)));
                        ofrdx = State.RandomSource.Next(Math.Max(0, (int)(Math.Min(inw, outw) / (scl) - 1)));
                        ofrdy = State.RandomSource.Next(Math.Max(0, (int)(Math.Min(inh, outh) / (scl) - 1)));
                    }
                }

                lu = new Point(oflux, ofluy);
                rd = new Point(ofrdx, ofrdy);

                var sframe = mat1.Clone();
                if (inchannels != sframe.Channels())
                {
                    if (inchannels == 1)
                    {
                        Cv2.CvtColor(sframe, sframe, ColorConversionCodes.BGR2GRAY);
                    }
                    else if (inchannels == 3)
                    {
                        Cv2.CvtColor(sframe, sframe, ColorConversionCodes.GRAY2BGR);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                sframe = sframe.Resize(new Size(inw, inh), 0, 0, InterpolationFlags.Area);
                sframe = EffectProcess.Offset(sframe, lu, rd, flipx, flipy, rotangle);
                sframes.Add(sframe.Clone());

                var tframe = mat2.Clone();
                if (outchannels != tframe.Channels())
                {
                    if (outchannels == 1)
                    {
                        Cv2.CvtColor(tframe, tframe, ColorConversionCodes.BGR2GRAY);
                    }
                    else if (outchannels == 3)
                    {
                        Cv2.CvtColor(tframe, tframe, ColorConversionCodes.GRAY2BGR);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                tframe = EffectProcess.Effect(tframe.Resize(new Size(outw, outh), 0, 0, InterpolationFlags.Area));
                tframe = EffectProcess.Offset(tframe, lu, rd, flipx, flipy, rotangle);
                tframes.Add(tframe.Clone());
            }

            Converter.MatToRNdMatrix(sframes.ToArray(), out smat);
            Converter.MatToRNdMatrix(tframes.ToArray(), out tmat);
            return(ret);
        }
Beispiel #6
0
        public RNdMatrix GetFrame(RNdMatrix inmat)
        {
            var frames = capture.GetFrames(inmat.BatchSize, inmat.Channels, inmat.Width, inmat.Height);

            return(new RNdMatrix(frames));
        }
Beispiel #7
0
        public bool LoadImage(int batchcount, int inchannels, int inw, int inh, int outchannels, int outw, int outh, bool doFlipX, bool doFlipY, double doRot, bool doAffine, out RNdMatrix smat, out RNdMatrix tmat)
        {
            bool ret = false;

            smat = tmat = null;

            List <Mat> sframes = new List <Mat>();
            List <Mat> tframes = new List <Mat>();

            for (int i = 0; i < batchcount; i++)
            {
                bool check;
                var  index = GetIndex(out check, inw, inh);
                ret |= check;
                var mat1 = (index[0].Container);
                var mat2 = (index[1].Container);

                bool   flipx = false;
                bool   flipy = false;
                int    oflux = 0, ofluy = 0;
                int    ofrdx = 0, ofrdy = 0;
                double scl = 2.5;
                double rotangle = 0;
                Point  lu = new Point(), rd = new Point();

                if (doFlipX)
                {
                    flipx = (State.RandomSource.NextDouble() > 0.5 ? true : false);
                }
                if (doFlipY)
                {
                    flipy = (State.RandomSource.NextDouble() > 0.5 ? true : false);
                }
                rotangle = (State.RandomSource.NextDouble() * 2 - 1) * doRot;
                if (doAffine)
                {
                    var w = Math.Min(mat1.Width, mat2.Width);
                    var h = Math.Min(mat1.Height, mat2.Height);
                    {
                        oflux = State.RandomSource.Next(Math.Max(0, (int)(w / (scl) - 1)));
                        ofluy = State.RandomSource.Next(Math.Max(0, (int)(h / (scl) - 1)));
                        ofrdx = State.RandomSource.Next(Math.Max(0, (int)(w / (scl) - 1)));
                        ofrdy = State.RandomSource.Next(Math.Max(0, (int)(h / (scl) - 1)));
                    }
                }

                lu = new Point(oflux, ofluy);
                rd = new Point(ofrdx, ofrdy);

                var sframe = mat1.Clone();
                if (inchannels != sframe.Channels())
                {
                    if (inchannels == 1)
                    {
                        Cv2.CvtColor(sframe, sframe, ColorConversionCodes.BGR2GRAY);
                    }
                    else if (inchannels == 3)
                    {
                        Cv2.CvtColor(sframe, sframe, ColorConversionCodes.GRAY2BGR);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                sframe = EffectProcess.Offset(sframe, lu, rd, flipx, flipy, rotangle);
                sframe = sframe.Resize(new Size(inw, inh), 0, 0, InterpolationFlags.Area);
                sframes.Add(sframe.Clone());

                var tframe = mat2.Clone();
                if (outchannels != tframe.Channels())
                {
                    if (outchannels == 1)
                    {
                        Cv2.CvtColor(tframe, tframe, ColorConversionCodes.BGR2GRAY);
                    }
                    else if (outchannels == 3)
                    {
                        Cv2.CvtColor(tframe, tframe, ColorConversionCodes.GRAY2BGR);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                tframe = EffectProcess.Effect(tframe);
                tframe = EffectProcess.Offset(tframe, lu, rd, flipx, flipy, rotangle);
                tframe = tframe.Resize(new Size(outw, outh), 0, 0, InterpolationFlags.Area);
                tframes.Add(tframe.Clone());
            }

            Converter.MatToRNdMatrix(sframes.ToArray(), out smat);
            Converter.MatToRNdMatrix(tframes.ToArray(), out tmat);
            return(ret);
        }