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); }
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); }