/// <summary>Creates a new vector allocated in OpenCL Image2D object.</summary> /// <param name="Length">Vector length. For convenience some extra memory is allocated but calculations only go up to vector dimensions</param> public CLImgVector(int Length) { //Stores length and computes number of necessary rows n = Length; //nRows = n/2^14 (4096 float4's) + 1 (at least one row) nRows = ((n - 1) >> 14) + 1; //Trick: //y = n >> 12; //y = n / 2^12; //x = n & 0xfff; //x = n mod (2^12-1); if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) { try { CLCalc.InitCL(); } catch { } } //Allocates vector. Width = IMGWIDTH, Height = nRows, Total number of elements = 4*Width*Height VectorData = new float[IMGWIDTH * nRows * 4]; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLVector = new CLCalc.Program.Image2D(VectorData, IMGWIDTH, nRows); } }
/// <summary>Apply filter to an image</summary> /// <param name="id">Filter index, CLFilters[id], to apply</param> /// <param name="bmp">Bitmap to be processes</param> public static Bitmap ApplyFilter(int id, Bitmap bmp) { //if (bmp.Width < 4096) //{ // CLCalc.Program.Image2D CLImgSrc = new CLCalc.Program.Image2D(bmp); // CLCalc.Program.Image2D CLImgDst = new CLCalc.Program.Image2D(bmp); // CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { CLImgSrc, CLImgDst }; // CLFilters[id].FilterKernel.Execute(args, new int[] { bmp.Width - 7, bmp.Height - 7 }); // return CLImgDst.ReadBitmap(); //} //else //{ //Pictures can be too big; it's necessary to split List <Bitmap> bmps = MPOReader.SplitJPS(bmp); CLCalc.Program.Image2D CLImgSrc0 = new CLCalc.Program.Image2D(bmps[0]); CLCalc.Program.Image2D CLImgDst0 = new CLCalc.Program.Image2D(bmps[0]); CLCalc.Program.MemoryObject[] args0 = new CLCalc.Program.MemoryObject[] { CLImgSrc0, CLImgDst0 }; CLFilters[id].FilterKernel.Execute(args0, new int[] { bmps[0].Width - 7, bmps[0].Height - 7 }); CLCalc.Program.Image2D CLImgSrc1 = new CLCalc.Program.Image2D(bmps[1]); CLCalc.Program.Image2D CLImgDst1 = new CLCalc.Program.Image2D(bmps[1]); CLCalc.Program.MemoryObject[] args1 = new CLCalc.Program.MemoryObject[] { CLImgSrc1, CLImgDst1 }; CLFilters[id].FilterKernel.Execute(args1, new int[] { bmps[1].Width - 7, bmps[1].Height - 7 }); Bitmap bmpL = CLImgDst0.ReadBitmap(); Bitmap bmpR = CLImgDst1.ReadBitmap(); return(MPOReader.AssembleJPS(bmpL, bmpR)); //} }
/// <summary>Constructor.</summary> /// <param name="N">NxN dimension of the matrix</param> /// <param name="nonZeroElemsPerRow">Maximum number of non-zero elements per row</param> public CLImgSparseMatrix(int N, int nonZeroElemsPerRow) { elemsPerRow = nonZeroElemsPerRow - 1; elemsPerRow = (elemsPerRow >> 2) + 1; elemsPerRow = elemsPerRow << 2; numRows = N; nElems = numRows * elemsPerRow; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) { try { CLCalc.InitCL(); } catch { } } //OpenCL image allocation int CLImgNumRows = ((nElems - 1) >> 14) + 1; MatrixData = new float[IMGWIDTH * CLImgNumRows * 4]; Columns = new int[IMGWIDTH * CLImgNumRows * 4]; for (int i = 0; i < Columns.Length; i++) { Columns[i] = -1; } if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLMatrixData = new CLCalc.Program.Image2D(MatrixData, IMGWIDTH, CLImgNumRows); CLColumns = new CLCalc.Program.Image2D(Columns, IMGWIDTH, CLImgNumRows); } }
/// <summary>Classifies multiple samples stored in OpenCL memory</summary> /// <param name="Samples">Samples data to classify</param> /// <param name="svm">SVM to use as classifier</param> public static float[] MultiClassify(SVM svm, CLCalc.Program.Image2D Samples) { float[] resp = new float[Samples.Height]; //svm.WriteToDevice(); if ((Samples.Width << 2) != svm.HostVLen[0]) { throw new Exception("Invalid Samples width, should be the same length of training features"); } if (svm.CLKernelValuesMultiClassify == null || svm.CLKernelValuesMultiClassify.OriginalVarLength != svm.alphaList.Count * Samples.Height) { svm.CLKernelValuesMultiClassify = new CLCalc.Program.Variable(new float[svm.alphaList.Count * Samples.Height]); } if (svm.CLAlphas == null || svm.CLAlphas.OriginalVarLength != svm.alphaList.Count) { svm.CLAlphas = new CLCalc.Program.Variable(svm.alphaList.ToArray()); float[] ys = new float[svm.TrainingSet.trainingArray.Count]; for (int i = 0; i < ys.Length; i++) { ys[i] = svm.TrainingSet.trainingArray[i].y; } svm.CLys = new CLCalc.Program.Variable(ys); } if (svm.CLb == null) { svm.CLb = new CLCalc.Program.Variable(new float[] { svm.b }); svm.CLQtdSupVecs = new CLCalc.Program.Variable(new int[] { svm.alphaList.Count }); CLMultiClassifSums = new CLCalc.Program.Variable(new float[Samples.Height]); } if (CLMultiClassifSums.OriginalVarLength != Samples.Height) { CLMultiClassifSums = new CLCalc.Program.Variable(new float[Samples.Height]); } //svm.CLAlphas.WriteToDevice(svm.alphaList.ToArray()); //svm.CLys.WriteToDevice(ys); //svm.CLb.WriteToDevice(new float[] { svm.b }); //svm.CLQtdSupVecs.WriteToDevice(new int[] { svm.alphaList.Count }); CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { svm.CLTrainingFeatures, svm.CLQtdSupVecs, svm.CLXVecLen, Samples, svm.CLKernelValuesMultiClassify, svm.CLLambda }; kernelComputeMultiKernelRBF.Execute(args, new int[] { svm.alphaList.Count, Samples.Height }); CLCalc.Program.Sync(); args = new CLCalc.Program.MemoryObject[] { svm.CLAlphas, svm.CLQtdSupVecs, svm.CLXVecLen, svm.CLys, svm.CLKernelValuesMultiClassify, svm.CLb, CLMultiClassifSums }; kernelSumKernels.Execute(args, Samples.Height); CLMultiClassifSums.ReadFromDeviceTo(resp); return(resp); }
public Kernels(CLCalc.Program.Image2D CLGLRenderImage) { this.CLimg = CLGLRenderImage; dimensions = new int[CLimg.Width * CLimg.Height]; for (int y = 0; y < CLimg.Height; y++) { CLY.VarSize += 100; } for (int x = 0; x < CLimg.Width; x++) { CLX.VarSize += dimensions[CLimg.Height - 1 + CLimg.Height * x]; CLZ.VarSize += dimensions[CLimg.Height * x] = 240; } CLDimensions = new CLCalc.Program.Variable(dimensions); }
/// <summary>Initialize kernels</summary> private void InitKernels() { if (kernelComputeHistograms == null || CLN == null) { CLSrc src = new CLSrc(); CLCalc.Program.Compile(src.src); kernelComputeHistograms = new CLCalc.Program.Kernel("ComputeHistogram"); kernelConsolidateHist = new CLCalc.Program.Kernel("ConsolidateHist"); kernelPerformNormalization = new CLCalc.Program.Kernel("PerformNormalization"); kernelComputeHue = new CLCalc.Program.Kernel("ComputeHue"); CLN = new CLCalc.Program.Variable(new int[] { NLumIntens }); CLWidth = new CLCalc.Program.Variable(new int[] { bmp.Width }); CLHeight = new CLCalc.Program.Variable(new int[] { bmp.Height }); CLbmp = new CLCalc.Program.Image2D(bmp); CLNewBmp = new CLCalc.Program.Image2D(bmp); } }
private List <Bitmap> CLHSL(Bitmap bmp) { if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) { CLCalc.InitCL(); } if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL) { return(null); } InitKernels(); if (CLBmpSaturation == null) { CLBmpSaturation = new CLCalc.Program.Image2D(bmp); CLBmpIntens = new CLCalc.Program.Image2D(bmp); } if (CLbmp == null || CLbmp.Height != bmp.Height || CLbmp.Width != bmp.Width) { CLbmp = new CLCalc.Program.Image2D(bmp); CLNewBmp = new CLCalc.Program.Image2D(bmp); CLBmpSaturation = new CLCalc.Program.Image2D(bmp); CLBmpIntens = new CLCalc.Program.Image2D(bmp); } else { CLbmp.WriteBitmap(bmp); CLN.WriteToDevice(new int[] { NLumIntens }); CLWidth.WriteToDevice(new int[] { bmp.Width }); CLHeight.WriteToDevice(new int[] { bmp.Height }); } kernelComputeHue.Execute(new CLCalc.Program.MemoryObject[] { CLbmp, CLNewBmp, CLBmpSaturation, CLBmpIntens }, new int[] { bmp.Width, bmp.Height }); return(new List <Bitmap>() { CLNewBmp.ReadBitmap(), CLBmpSaturation.ReadBitmap(), CLBmpIntens.ReadBitmap() }); }
/// <summary>Classifies a given set of Samples (image2d of floats) each one in a category. Each row of the image is a sample /// to be classified and the features should be stored in the columns. The number of columns Ncol = Nfeatures/4 since /// each pixel holds 4 floats</summary> /// <param name="Samples">Image2D containing samples to be classified</param> /// <param name="maxVals">Maximum values found</param> /// <returns></returns> public float[] Classify(CLCalc.Program.Image2D Samples, out float[] maxVals) { maxVals = SVM.MultiClassify(SVMs[0], Samples); float[] classif = new float[maxVals.Length]; for (int j = 0; j < maxVals.Length; j++) { classif[j] = Classifications[0]; } for (int i = 1; i < SVMs.Count; i++) { float[] m = SVM.MultiClassify(SVMs[i], Samples); for (int j = 0; j < maxVals.Length; j++) { if (m[j] > maxVals[j]) { maxVals[j] = m[j]; classif[j] = Classifications[i]; } } } return(classif); }
public Kernels(CLCalc.Program.Image2D CLGLRenderImage) { }
/// <summary>Constructor.</summary> /// <param name="N">NxN dimension of the matrix</param> /// <param name="nonZeroElemsPerRow">Maximum number of non-zero elements per row</param> public CLImgSparseMatrix(int N, int nonZeroElemsPerRow) { elemsPerRow = nonZeroElemsPerRow - 1; elemsPerRow = (elemsPerRow >> 2) + 1; elemsPerRow = elemsPerRow << 2; numRows = N; nElems = numRows * elemsPerRow; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) { try { CLCalc.InitCL(); } catch { } } //OpenCL image allocation int CLImgNumRows = ((nElems - 1) >> 14) + 1; MatrixData = new float[IMGWIDTH * CLImgNumRows * 4]; Columns = new int[IMGWIDTH * CLImgNumRows * 4]; for (int i = 0; i < Columns.Length; i++) Columns[i] = -1; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLMatrixData = new CLCalc.Program.Image2D(MatrixData, IMGWIDTH, CLImgNumRows); CLColumns = new CLCalc.Program.Image2D(Columns, IMGWIDTH, CLImgNumRows); } }
public void SetDynamicImage(CLCalc.Program.Image2D image) { this.image = image; this.dynamicVisible = true; }
public void Initialize(BaseCameraApplication capture) { DepthCameraFrame frame = capture.GetDevices()[0].GetDepthImage(); try { StreamReader reader = new StreamReader(new MemoryStream(Perceptual.Foundation.Properties.Resources.AdaptiveTemporalFilter)); string text = reader.ReadToEnd(); CLCalc.Program.Compile(capture.GetPrimaryDevice().GetPreprocessCode() + "\n#define HISTORY_SIZE " + historySize + "\n" + text); reader.Close(); } catch (Exception ex) { System.Console.WriteLine(ex.Message); System.Console.WriteLine("Could not find AdaptiveTemporalFilter.cl"); Environment.Exit(1); } historyIndex = new CLCalc.Program.Value<int>(historySize - 1); updateBuffer = new CLCalc.Program.Kernel("UpdateFilter"); copyToTemporalBuffer = new CLCalc.Program.Kernel("CopyToTemporalBuffer"); depthBuffer = CLCalc.Program.Variable.Create(new ComputeBuffer<float>(CLCalc.Program.Context, ComputeMemoryFlags.ReadWrite, 4 * frame.Width * frame.Height)); depthCopyBuffer = new CLCalc.Program.Variable(new float[4 * frame.Width * frame.Height]); depthTemporalBuffer = CLCalc.Program.Variable.Create(new ComputeBuffer<float>(CLCalc.Program.Context, ComputeMemoryFlags.ReadWrite, historySize * 4 * frame.Width * frame.Height)); depthImage = new CLCalc.Program.Image2D(new float[frame.Height * frame.Width * 4], frame.Width, frame.Height); uvImage = new CLCalc.Program.Image2D(new float[frame.Height * frame.Width * 4], frame.Width, frame.Height); kernelErodeFilter = new CLCalc.Program.Kernel("ErodeFilter"); kernelDilateFilter = new CLCalc.Program.Kernel("DilateFilter"); kernelCopyImage = new CLCalc.Program.Kernel("CopyImage"); kernelCopyBuffer = new CLCalc.Program.Kernel("CopyDepth"); kernelMedianFilter1 = new CLCalc.Program.Kernel("SmallFilter"); kernelMedianFilter2 = new CLCalc.Program.Kernel("LargeFilter"); }
public void SetDynamicImage(CLCalc.Program.Image2D image, int textureId) { this.textureId = textureId; this.image = image; this.dynamicVisible = true; }
/// <summary>Shades a bitmap</summary> /// <param name="bmp">Bitmap to shade</param> /// <param name="bmp">Actually render?</param> /// <returns></returns> public void Shade(Bitmap bmp, PictureBox pb, bool doRender) { Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height); Graphics g = Graphics.FromImage(bmp2); g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height); //draws points onto thresholded bitmap for (int i = 0; i < Colors.Count; i++) { if (Points[i].Count > 1) { g.DrawLines(new Pen(Color.Black, 2), Points[i].ToArray()); } } #region Initializations if (CLbmpSrc == null || CLbmpSrc.Width != bmp.Width || CLbmpSrc.Height != bmp.Height) { CLbmpSrc = new CLCalc.Program.Image2D(bmp2); CLbmpThresh = new CLCalc.Program.Image2D(bmp); CLthreshInfo = new CLCalc.Program.Variable(typeof(byte), bmp.Width * bmp.Height); CLbmpStroke = new CLCalc.Program.Image2D(bmp); CLbmpDists = new CLCalc.Program.Image2D(bmp); CLimgFinal1 = new CLCalc.Program.Image2D(new Bitmap(bmp2.Width, bmp2.Height)); CLimgFinal2 = new CLCalc.Program.Image2D(new Bitmap(bmp2.Width, bmp2.Height)); CLTotalPixWeight = new CLCalc.Program.Variable(typeof(float), bmp.Width * bmp.Height); lastPixWeight = new float[bmp.Width * bmp.Height]; CLWeight = new CLCalc.Program.Variable(typeof(float), bmp.Width * bmp.Height); lastImage = new Bitmap(bmp.Width, bmp.Height); } else { CLbmpSrc.WriteBitmap(bmp2); CLimgFinal1.WriteBitmap(new Bitmap(bmp2.Width, bmp2.Height)); CLimgFinal2.WriteBitmap(new Bitmap(bmp2.Width, bmp2.Height)); kernelinitTotalWeight.Execute(new CLCalc.Program.MemoryObject[] { CLTotalPixWeight }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); } #endregion System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swCL = new System.Diagnostics.Stopwatch(); sw.Start(); //computes threshold kernelThreshold.Execute(new CLCalc.Program.MemoryObject[] { CLcfgVars, CLbmpSrc, CLthreshInfo, CLbmpThresh }, new int[] { bmp.Width, bmp.Height }); if (!doRender) { return; } //Reuse last render? if (ReUseLastRender) { CLTotalPixWeight.WriteToDevice(lastPixWeight); CLimgFinal1.WriteBitmap(lastImage); } else { lastRenderedIdx = 0; } //generates images for points for (int i = lastRenderedIdx; i < Colors.Count; i++) { Bitmap bmpStroke = new Bitmap(bmp.Width, bmp.Height); if (Points[i].Count > 1) { if (DistanceMaps[i] == null) { Graphics g2 = Graphics.FromImage(bmpStroke); g2.DrawLines(new Pen(Colors[i], 2), Points[i].ToArray()); CLbmpStroke.WriteBitmap(bmpStroke); int[] changed = new int[] { 0 }; swCL.Start(); changed[0] = 1; kernelinitWeight.Execute(new CLCalc.Program.MemoryObject[] { CLWeight }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); int n = 0; while (changed[0] > 0 && n < MAXPROPAGITERS) { n++; changed[0] = 0; CLchanged.WriteToDevice(changed); kernelPropagateDist.Execute(new CLCalc.Program.MemoryObject[] { CLchanged, CLbmpStroke, CLthreshInfo, CLWeight, CLbmpDists }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); CLchanged.ReadFromDeviceTo(changed); } swCL.Stop(); DistanceMaps[i] = new float[CLWeight.OriginalVarLength]; CLWeight.ReadFromDeviceTo(DistanceMaps[i]); } else { CLWeight.WriteToDevice(DistanceMaps[i]); } //composes total weight CLcolor.WriteToDevice(new float[] { (float)Colors[i].B, (float)Colors[i].G, (float)Colors[i].R }); kernelAddToTotalWeight.Execute(new CLCalc.Program.MemoryObject[] { CLTotalPixWeight, CLWeight, CLcolor, CLimgFinal1, CLimgFinal2, CLthreshInfo }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); //swaps final images - result is always on CLimgFinal1 CLCalc.Program.Image2D temp = CLimgFinal1; CLimgFinal1 = CLimgFinal2; CLimgFinal2 = temp; if (pb != null) { pb.Image = GetRenderedImage(); pb.Refresh(); } } bmpStroke.Dispose(); } sw.Stop(); lastRenderedIdx = Colors.Count; //saves total pixel weight and final image CLTotalPixWeight.ReadFromDeviceTo(lastPixWeight); if (lastImage != null) { lastImage.Dispose(); } lastImage = CLimgFinal1.ReadBitmap(); }
/// <summary>Specific function when SVM contains only one object, such as faces</summary> /// <param name="bmp">Next frame to process</param> public List <int> FindSingleObj(Bitmap bmp) { //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(), sw2 = new System.Diagnostics.Stopwatch(); //sw.Start(); if (SVM == null) { return(null); } if (imgWidth != bmp.Width || imgHeight != bmp.Height) { imgWidth = bmp.Width; imgHeight = bmp.Height; SubFramePos = 0; List <int> subFrames = new List <int>(); ComputeSubFrames(0, 0, bmp.Width, bmp.Height, subFrames); SubFrames = subFrames.ToArray(); SubFeatures = new float[(SubFrames.Length / 3) * 364]; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLSubFrames = new CLCalc.Program.Variable(SubFrames); CLSubFeatures = new CLCalc.Program.Image2D(SubFeatures, 91, SubFrames.Length / 3); CLBmp = new CLCalc.Program.Image2D(bmp); //CLBmpTemp = new CLCalc.Program.Image2D(bmp); CLBmpPrev = new CLCalc.Program.Image2D(bmp); } } //Swaps current and previous bitmap pointers CLCalc.Program.Image2D temp = CLBmp; CLBmp = CLBmpPrev; CLBmpPrev = temp; //Computes frame difference ComputeFrameDiff(); //Replaces subFrames based on moving regions for (int k = 0; k < MovingRegionBoxes.Count >> 2; k++) { List <int> sframes = new List <int>(); int ind = 4 * k; ComputeSubFrames(MovingRegionBoxes[ind] << 3, MovingRegionBoxes[ind + 2] << 3, MovingRegionBoxes[ind + 1] << 3, MovingRegionBoxes[ind + 3] << 3, sframes); for (int p = 0; p < sframes.Count; p += 3) { SubFrames[SubFramePos] = sframes[p]; SubFrames[SubFramePos + 1] = sframes[p + 1]; SubFrames[SubFramePos + 2] = sframes[p + 2]; SubFramePos += 3; if (SubFramePos > SubFrames.Length - 1) { SubFramePos = 0; } } } CLSubFrames.WriteToDevice(SubFrames); CLBmp.WriteBitmap(bmp); ////Segments skin //kernelSegregateSkin.Execute(new CLCalc.Program.MemoryObject[] { CLBmpTemp, CLBmp }, new int[] { bmp.Width, bmp.Height }); //Extract features using OpenCL CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { CLSubFrames, CLSubFeatures, CLBmp }; kernelExtractFeatures.Execute(args, SubFrames.Length / 3); #region No OpenCL //float[] testSubFeats = new float[364 * (SubFrames.Length / 3)]; //CLSubFeatures.ReadFromDeviceTo(testSubFeats); //Extract features without OpenCL //ExtractFeatures(SubFrames, SubFeatures, bmp); //CLSubFeatures.WriteToDevice(SubFeatures); #endregion //sw2.Start(); float[] maxvals = OpenCLTemplate.MachineLearning.SVM.MultiClassify(SVM.SVMs[0], CLSubFeatures); //SVM.Classify(CLSubFeatures, out maxvals); //sw2.Stop(); List <int> FacesPos = new List <int>(); List <float> MaxVals = new List <float>(); //Goes in decreasing window size order for (int kk = Config.WINDOWSIZES.Length - 1; kk >= 0; kk--) { for (int i = maxvals.Length - 1; i >= 0; i--) { if (SubFrames[3 * i + 2] == Config.WINDOWSIZES[kk] && maxvals[i] > Config.REQCERTAINTY) { //Checks if a face already has been found in that region bool contido = false; int i3 = 3 * i; int kmax = FacesPos.Count / 3; for (int k = 0; k < kmax; k++) { int k3 = 3 * k; if ( (FacesPos[k3] <= SubFrames[i3] && SubFrames[i3] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] && SubFrames[i3 + 1] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] + SubFrames[i3 + 2] && SubFrames[i3] + SubFrames[i3 + 2] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] + SubFrames[i3 + 2] && SubFrames[i3 + 1] + SubFrames[i3 + 2] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] && SubFrames[i3] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] + SubFrames[i3 + 2] && SubFrames[i3 + 1] + SubFrames[i3 + 2] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] + SubFrames[i3 + 2] && SubFrames[i3] + SubFrames[i3 + 2] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] && SubFrames[i3 + 1] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) ) { contido = true; //Replaces if better if (maxvals[i] > MaxVals[k] && SubFrames[3 * i + 2] == FacesPos[3 * k + 2]) { FacesPos[k3] = SubFrames[i3]; FacesPos[k3 + 1] = SubFrames[i3 + 1]; FacesPos[k3 + 2] = SubFrames[i3 + 2]; MaxVals[k] = maxvals[i]; } k = FacesPos.Count; } } if (!contido) { FacesPos.Add(SubFrames[3 * i]); FacesPos.Add(SubFrames[3 * i + 1]); FacesPos.Add(SubFrames[3 * i + 2]); MaxVals.Add(maxvals[i]); } } } } //sw.Stop(); Random rnd = new Random(); //Updates frame search region if (MovingRegionBoxes.Count > 0) { for (int i = 0; i < maxvals.Length; i++) { if (maxvals[i] > Config.REFINEUNCERTAINTY) { int i3 = 3 * i; List <int> sframes = new List <int>(); int cx = SubFrames[i3] + (SubFrames[i3 + 2] >> 1) + rnd.Next(7) - 3; int cy = SubFrames[i3 + 1] + (SubFrames[i3 + 2] >> 1) + rnd.Next(7) - 3; int bigwSize = Config.WINDOWSIZES[Config.WINDOWSIZES.Length - 1]; try { ComputeSubFrames(cx - (bigwSize >> 1), cy - (bigwSize >> 1), cx + (bigwSize >> 1), cy + (bigwSize >> 1), sframes); for (int p = 0; p < sframes.Count; p += 3) { SubFrames[SubFramePos] = sframes[p]; SubFrames[SubFramePos + 1] = sframes[p + 1]; SubFrames[SubFramePos + 2] = sframes[p + 2]; SubFramePos += 3; if (SubFramePos > SubFrames.Length - 1) { SubFramePos = 0; } } } catch { } } } } return(FacesPos); }
public ImageColorMixer(int xpos, int ypos, int width, int height) { this.xpos = xpos; this.ypos = ypos; this.width = width; this.height = height; this.scrollWidth = width / 2 - 40; this.controlXpos = xpos + scrollWidth + 40; hueScroll = new ImageScrollButton("Hue", xpos, ypos, scrollWidth, 20, 0, 1.0f); saturationScroll = new ImageScrollButton("Saturation", xpos, ypos + 30, scrollWidth, 20, 0, 1.0f); valueScroll = new ImageScrollButton("Brightness", xpos, ypos + 60, scrollWidth, 20, 0, 1.0f); hueImage = new CLCalc.Program.Image2D(hueBuffer = new float[scrollWidth * 4], scrollWidth, 1); saturationImage = new CLCalc.Program.Image2D(saturationBuffer = new float[scrollWidth * 4], scrollWidth, 1); valueImage = new CLCalc.Program.Image2D(valueBuffer = new float[scrollWidth * 4], scrollWidth, 1); mixedImage = new CLCalc.Program.Image2D(mixedBuffer = new float[scrollWidth * 4], scrollWidth, 1); controlColors = CLCalc.Program.Variable.Create(new ComputeBuffer<float>(CLCalc.Program.Context, ComputeMemoryFlags.ReadWrite, controlColorsBuffer = new float[4 * NUM_CONTROL_PTS])); controlAmps = CLCalc.Program.Variable.Create(new ComputeBuffer<float>(CLCalc.Program.Context, ComputeMemoryFlags.ReadWrite, controlAmpsBuffer = new float[NUM_CONTROL_PTS])); hueScroll.sliderTrack.SetDynamicImage(hueImage); saturationScroll.sliderTrack.SetDynamicImage(saturationImage); valueScroll.sliderTrack.SetDynamicImage(valueImage); mixedColor = new ImageButton("Mixture", false, controlXpos, ypos + 90, scrollWidth, 20); chosenColor = new ImageButton("Chosen", false, xpos, ypos + 90, scrollWidth, 20); mixedColor.SetDynamicImage(mixedImage); controlPoints = new ImageButton[NUM_CONTROL_PTS]; stems = new ImageButton[NUM_CONTROL_PTS]; controlPointsBorder = new ImageButton[NUM_CONTROL_PTS]; Bitmap bitmap = new Bitmap(20, 20); using (Graphics gfx = Graphics.FromImage(bitmap)) { gfx.Clear(Color.FromArgb(0, 0, 0, 0)); gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; gfx.FillEllipse(new SolidBrush(Color.White), 1, 1, 17, 17); } hueScroll.SetValue(0.1f); valueScroll.SetValue(0.5f); saturationScroll.SetValue(0.5f); float4 currentColor = HSVtoRGB(new float4(hueScroll.GetValue(), saturationScroll.GetValue(), valueScroll.GetValue(), 1.0f)); colorIndex = NUM_CONTROL_PTS / 2; for (int i = 0; i < NUM_CONTROL_PTS; i++) { controlPoints[i] = new ImageButton("control_" + i, bitmap, true, (int)(controlXpos + (i + 0.5f) * (scrollWidth) / (float)NUM_CONTROL_PTS), ypos + ((i == colorIndex) ? 50 : 70), 20, 20); controlPointsBorder[i] = new ImageButton("control_" + i, new Bitmap(20, 20), false, (int)(controlXpos + i * (scrollWidth) / (float)NUM_CONTROL_PTS), ypos + 70, 20, 20); controlPoints[i].SetDragAndDrop(true); controlPoints[i].SetDragBoundingBox(controlXpos - 10, ypos, controlXpos + scrollWidth - 10, ypos + 70); stems[i] = new ImageButton("stem_" + i, false, (int)(controlXpos + i * (scrollWidth) / (float)NUM_CONTROL_PTS), 90, 2, 20); stems[i].SetBackgroundColor(Color4.White); controlPoints[i].SetForegroundColor(HSVtoRGB(new float4(i / (float)(NUM_CONTROL_PTS), saturationScroll.GetValue(), valueScroll.GetValue(), 1.0f))); UpdateControlPoint(i); controlPoints[i].AddObserver(this); } }
/// <summary>Writes Training Set into device memory</summary> private void WriteToDevice() { //Vector length if (HostVLen == null) { HostVLen = new int[1]; HostVLen[0] = (1 + ((TrainingSet.trainingArray[0].xVector.Length - 1) >> 2)) << 2; } //Populates OpenCL buffer if (TrainingFeatures == null) // || TrainingFeatures.Length != TrainingSet.getN * HostVLen[0]) { TrainingFeatures = new float[TrainingSet.getN * HostVLen[0]]; } for (int i = 0; i < TrainingSet.getN; i++) { for (int j = 0; j < TrainingSet.trainingArray[0].xVector.Length; j++) { TrainingFeatures[j + i * HostVLen[0]] = TrainingSet.trainingArray[i].xVector[j]; } } if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL) { return; } lock (CLResource) { //Writes to OpenCL memory //if (CLTrainingFeatures != null) CLTrainingFeatures.Dispose(); if (CLTrainingFeatures == null || CLTrainingFeatures.OriginalVarLength != TrainingFeatures.Length) { if (CLTrainingFeatures != null) { CLTrainingFeatures.Dispose(); } CLTrainingFeatures = new CLCalc.Program.Variable(TrainingFeatures); } else { CLTrainingFeatures.WriteToDevice(TrainingFeatures); } //if (CLXVecLen != null) CLXVecLen.Dispose(); if (CLXVecLen == null) { CLXVecLen = new CLCalc.Program.Variable(HostVLen); } else { CLXVecLen.WriteToDevice(HostVLen); } HostSample = new float[HostVLen[0]]; //if (CLSample != null) CLSample.Dispose(); if (CLSample == null) { CLSample = new CLCalc.Program.Image2D(HostSample, HostVLen[0] >> 2, 1); } else { CLSample.WriteToDevice(HostSample); } //if (CLKernelValues != null) CLKernelValues.Dispose(); if (CLKernelValues == null || CLKernelValues.OriginalVarLength != TrainingSet.getN) { CLKernelValues = new CLCalc.Program.Variable(new float[TrainingSet.getN]); } else { CLKernelValues.WriteToDevice(new float[TrainingSet.getN]); } //if (CLLambda != null) CLLambda.Dispose(); if (CLLambda == null) { CLLambda = new CLCalc.Program.Variable(new float[] { this.ProblemCfg.lambda }); } else { CLLambda.WriteToDevice(new float[] { this.ProblemCfg.lambda }); } } }
/// <summary>Writes Training Set into device memory</summary> private void WriteToDevice() { //Vector length if (HostVLen == null) { HostVLen = new int[1]; HostVLen[0] = (1 + ((TrainingSet.trainingArray[0].xVector.Length - 1) >> 2)) << 2; } //Populates OpenCL buffer if (TrainingFeatures == null) // || TrainingFeatures.Length != TrainingSet.getN * HostVLen[0]) { TrainingFeatures = new float[TrainingSet.getN * HostVLen[0]]; } for (int i = 0; i < TrainingSet.getN; i++) { for (int j = 0; j < TrainingSet.trainingArray[0].xVector.Length; j++) { TrainingFeatures[j + i * HostVLen[0]] = TrainingSet.trainingArray[i].xVector[j]; } } if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL) return; lock (CLResource) { //Writes to OpenCL memory //if (CLTrainingFeatures != null) CLTrainingFeatures.Dispose(); if (CLTrainingFeatures == null || CLTrainingFeatures.OriginalVarLength != TrainingFeatures.Length) { if (CLTrainingFeatures != null) CLTrainingFeatures.Dispose(); CLTrainingFeatures = new CLCalc.Program.Variable(TrainingFeatures); } else CLTrainingFeatures.WriteToDevice(TrainingFeatures); //if (CLXVecLen != null) CLXVecLen.Dispose(); if (CLXVecLen == null) CLXVecLen = new CLCalc.Program.Variable(HostVLen); else CLXVecLen.WriteToDevice(HostVLen); HostSample = new float[HostVLen[0]]; //if (CLSample != null) CLSample.Dispose(); if (CLSample == null) CLSample = new CLCalc.Program.Image2D(HostSample, HostVLen[0] >> 2, 1); else CLSample.WriteToDevice(HostSample); //if (CLKernelValues != null) CLKernelValues.Dispose(); if (CLKernelValues == null || CLKernelValues.OriginalVarLength != TrainingSet.getN) CLKernelValues = new CLCalc.Program.Variable(new float[TrainingSet.getN]); else CLKernelValues.WriteToDevice(new float[TrainingSet.getN]); //if (CLLambda != null) CLLambda.Dispose(); if (CLLambda == null) CLLambda = new CLCalc.Program.Variable(new float[] { this.ProblemCfg.lambda }); else CLLambda.WriteToDevice(new float[] { this.ProblemCfg.lambda }); } }
public void Process(BaseCameraApplication capture) { DepthCameraFrame depthFrame = capture.GetDevices()[0].GetDepthImage(); TextureMapFrame textureFrame = capture.GetDevices()[0].GetTextureImage(); this.rgbImage = (CLCalc.Program.Image2D)capture.GetDevices()[0].GetColorImage().GetMemoryObject(); kernelCopyImage.Execute(new CLCalc.Program.MemoryObject[] { depthFrame.GetMemoryObject(), textureFrame.GetMemoryObject(), uvImage, depthImage }, new int[] { depthFrame.Width, depthFrame.Height }); }
/// <summary>Equalizes image histogram using OpenCL</summary> private void CLEqualizeHistogram(ref Bitmap bmp) { if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) { CLCalc.InitCL(); } if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL) { return; } float[] PartialHistograms = new float[NLumIntens * bmp.Width]; float[] histLuminance = new float[NLumIntens]; if (kernelComputeHistograms == null || CLN == null || CLHistogram == null) { CLHistogram = new CLCalc.Program.Variable(histLuminance); CLPartialHistograms = new CLCalc.Program.Variable(PartialHistograms); } InitKernels(); System.Diagnostics.Stopwatch swTotal = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swCopyBmp = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swRescaling = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swComputeHistPartial = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swComputeHistConsolid = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swHistIntegral = new System.Diagnostics.Stopwatch(); swTotal.Start(); swCopyBmp.Start(); if (CLbmp == null || CLbmp.Height != bmp.Height || CLbmp.Width != bmp.Width) { CLbmp = new CLCalc.Program.Image2D(bmp); CLNewBmp = new CLCalc.Program.Image2D(bmp); CLPartialHistograms = new CLCalc.Program.Variable(PartialHistograms); } else { CLbmp.WriteBitmap(bmp); CLN.WriteToDevice(new int[] { NLumIntens }); CLWidth.WriteToDevice(new int[] { bmp.Width }); CLHeight.WriteToDevice(new int[] { bmp.Height }); } swCopyBmp.Stop(); swComputeHistPartial.Start(); //Partial histograms CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { CLbmp, CLPartialHistograms, CLHeight, CLN }; kernelComputeHistograms.Execute(args, bmp.Width); CLCalc.Program.Sync(); swComputeHistPartial.Stop(); swComputeHistConsolid.Start(); args = new CLCalc.Program.MemoryObject[] { CLPartialHistograms, CLHistogram, CLHeight, CLN }; kernelConsolidateHist.Execute(args, NLumIntens); CLHistogram.ReadFromDeviceTo(histLuminance); swComputeHistConsolid.Stop(); swHistIntegral.Start(); //Perform histogram integration - better performance in CPU //Compute histogram integrals in-place for (int i = 1; i < NLumIntens; i++) { histLuminance[i] += histLuminance[i - 1]; } float scale = 0.9f / histLuminance[NLumIntens - 1]; //Scales histograms for (int i = 0; i < NLumIntens; i++) { histLuminance[i] *= scale; } //Writes histogram integral CLHistogram.WriteToDevice(histLuminance); swHistIntegral.Stop(); swRescaling.Start(); //Computes equalized image args = new CLCalc.Program.MemoryObject[] { CLbmp, CLNewBmp, CLHistogram, CLN }; kernelPerformNormalization.Execute(args, new int [] { bmp.Width, bmp.Height }); bmp = CLNewBmp.ReadBitmap(); swRescaling.Stop(); swTotal.Stop(); }
public void Process(BaseCameraApplication capture) { DepthCameraFrame depthFrame = capture.GetDevices()[0].GetDepthImage(); TextureMapFrame textureFrame = capture.GetDevices()[0].GetTextureImage(); CLCalc.Program.MemoryObject input = depthFrame.GetMemoryObject(); CLCalc.Program.MemoryObject output = depthBuffer; CLCalc.Program.MemoryObject tmp; this.rgbImage = (CLCalc.Program.Image2D)capture.GetDevices()[0].GetColorImage().GetMemoryObject(); kernelCopyBuffer.Execute(new CLCalc.Program.MemoryObject[] { input, depthCopyBuffer }, new int[] { depthFrame.Width * depthFrame.Height }); for (int cycle = 0; cycle < smoothIterations; cycle++) { kernelMedianFilter1.Execute(new CLCalc.Program.MemoryObject[] { input, output }, new int[] { depthFrame.Width, depthFrame.Height }); tmp = input; input = output; output = tmp; } if (input != depthFrame.GetMemoryObject()) { System.Console.WriteLine("Wrong Buffer!"); Environment.Exit(1); } input = depthCopyBuffer; output = depthBuffer; for (int cycle = 0; cycle < smoothIterations2; cycle++) { kernelMedianFilter2.Execute(new CLCalc.Program.MemoryObject[] { input, output }, new int[] { depthFrame.Width, depthFrame.Height }); tmp = input; input = output; output = tmp; } for (int cycle = 0; cycle < erodeIterations; cycle++) { kernelErodeFilter.Execute(new CLCalc.Program.MemoryObject[] { input, output }, new int[] { depthFrame.Width, depthFrame.Height }); tmp = input; input = output; output = tmp; } for (int cycle = 0; cycle < dilateIterations; cycle++) { kernelDilateFilter.Execute(new CLCalc.Program.MemoryObject[] { input, output }, new int[] { depthFrame.Width, depthFrame.Height }); tmp = input; input = output; output = tmp; } if (input != depthCopyBuffer) { System.Console.WriteLine("Wrong Buffer!"); Environment.Exit(1); } if (once) { copyToTemporalBuffer.Execute(new CLCalc.Program.MemoryObject[] {historyIndex, depthCopyBuffer, depthFrame.GetMemoryObject(), depthTemporalBuffer }, new int[] { depthFrame.Width * depthFrame.Height }); } else { updateBuffer.Execute(new CLCalc.Program.MemoryObject[] { historyIndex,depthCopyBuffer, depthFrame.GetMemoryObject(), depthTemporalBuffer }, new int[] { depthFrame.Width * depthFrame.Height }); } historyIndex.value--; if (historyIndex.value < 0) { historyIndex.value = historySize - 1; once = false; } kernelCopyImage.Execute(new CLCalc.Program.MemoryObject[] { depthFrame.GetMemoryObject(), textureFrame.GetMemoryObject(), uvImage, depthImage }, new int[] { depthFrame.Width, depthFrame.Height }); }
/// <summary>Specific function when SVM contains only one object, such as faces</summary> /// <param name="bmp">Next frame to process</param> public List<int> FindSingleObj(Bitmap bmp) { //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(), sw2 = new System.Diagnostics.Stopwatch(); //sw.Start(); if (SVM == null) return null; if (imgWidth != bmp.Width || imgHeight != bmp.Height) { imgWidth = bmp.Width; imgHeight = bmp.Height; SubFramePos = 0; List<int> subFrames = new List<int>(); ComputeSubFrames(0, 0, bmp.Width, bmp.Height, subFrames); SubFrames = subFrames.ToArray(); SubFeatures = new float[(SubFrames.Length / 3) * 364]; if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLSubFrames = new CLCalc.Program.Variable(SubFrames); CLSubFeatures = new CLCalc.Program.Image2D(SubFeatures, 91, SubFrames.Length / 3); CLBmp = new CLCalc.Program.Image2D(bmp); //CLBmpTemp = new CLCalc.Program.Image2D(bmp); CLBmpPrev = new CLCalc.Program.Image2D(bmp); } } //Swaps current and previous bitmap pointers CLCalc.Program.Image2D temp = CLBmp; CLBmp = CLBmpPrev; CLBmpPrev = temp; //Computes frame difference ComputeFrameDiff(); //Replaces subFrames based on moving regions for (int k = 0; k < MovingRegionBoxes.Count >> 2; k++) { List<int> sframes = new List<int>(); int ind = 4 * k; ComputeSubFrames(MovingRegionBoxes[ind] << 3, MovingRegionBoxes[ind + 2] << 3, MovingRegionBoxes[ind + 1] << 3, MovingRegionBoxes[ind + 3] << 3, sframes); for (int p = 0; p < sframes.Count; p += 3) { SubFrames[SubFramePos] = sframes[p]; SubFrames[SubFramePos + 1] = sframes[p + 1]; SubFrames[SubFramePos + 2] = sframes[p + 2]; SubFramePos += 3; if (SubFramePos > SubFrames.Length - 1) SubFramePos = 0; } } CLSubFrames.WriteToDevice(SubFrames); CLBmp.WriteBitmap(bmp); ////Segments skin //kernelSegregateSkin.Execute(new CLCalc.Program.MemoryObject[] { CLBmpTemp, CLBmp }, new int[] { bmp.Width, bmp.Height }); //Extract features using OpenCL CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { CLSubFrames, CLSubFeatures, CLBmp }; kernelExtractFeatures.Execute(args, SubFrames.Length / 3); #region No OpenCL //float[] testSubFeats = new float[364 * (SubFrames.Length / 3)]; //CLSubFeatures.ReadFromDeviceTo(testSubFeats); //Extract features without OpenCL //ExtractFeatures(SubFrames, SubFeatures, bmp); //CLSubFeatures.WriteToDevice(SubFeatures); #endregion //sw2.Start(); float[] maxvals = OpenCLTemplate.MachineLearning.SVM.MultiClassify(SVM.SVMs[0], CLSubFeatures); //SVM.Classify(CLSubFeatures, out maxvals); //sw2.Stop(); List<int> FacesPos = new List<int>(); List<float> MaxVals = new List<float>(); //Goes in decreasing window size order for (int kk = Config.WINDOWSIZES.Length - 1; kk >= 0; kk--) { for (int i = maxvals.Length - 1; i >= 0; i--) { if (SubFrames[3 * i + 2] == Config.WINDOWSIZES[kk] && maxvals[i] > Config.REQCERTAINTY) { //Checks if a face already has been found in that region bool contido = false; int i3 = 3 * i; int kmax = FacesPos.Count / 3; for (int k = 0; k < kmax; k++) { int k3 = 3 * k; if ( (FacesPos[k3] <= SubFrames[i3] && SubFrames[i3] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] && SubFrames[i3 + 1] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] + SubFrames[i3 + 2] && SubFrames[i3] + SubFrames[i3 + 2] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] + SubFrames[i3 + 2] && SubFrames[i3 + 1] + SubFrames[i3 + 2] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] && SubFrames[i3] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] + SubFrames[i3 + 2] && SubFrames[i3 + 1] + SubFrames[i3 + 2] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) || (FacesPos[k3] <= SubFrames[i3] + SubFrames[i3 + 2] && SubFrames[i3] + SubFrames[i3 + 2] <= FacesPos[k3] + FacesPos[k3 + 2] && FacesPos[k3 + 1] <= SubFrames[i3 + 1] && SubFrames[i3 + 1] <= FacesPos[k3 + 1] + FacesPos[k3 + 2]) ) { contido = true; //Replaces if better if (maxvals[i] > MaxVals[k] && SubFrames[3 * i + 2] == FacesPos[3 * k + 2]) { FacesPos[k3] = SubFrames[i3]; FacesPos[k3 + 1] = SubFrames[i3 + 1]; FacesPos[k3 + 2] = SubFrames[i3 + 2]; MaxVals[k] = maxvals[i]; } k = FacesPos.Count; } } if (!contido) { FacesPos.Add(SubFrames[3 * i]); FacesPos.Add(SubFrames[3 * i + 1]); FacesPos.Add(SubFrames[3 * i + 2]); MaxVals.Add(maxvals[i]); } } } } //sw.Stop(); Random rnd = new Random(); //Updates frame search region if (MovingRegionBoxes.Count > 0) { for (int i = 0; i < maxvals.Length; i++) { if (maxvals[i] > Config.REFINEUNCERTAINTY) { int i3 = 3 * i; List<int> sframes = new List<int>(); int cx = SubFrames[i3] + (SubFrames[i3 + 2] >> 1) + rnd.Next(7) - 3; int cy = SubFrames[i3 + 1] + (SubFrames[i3 + 2] >> 1) + rnd.Next(7) - 3; int bigwSize = Config.WINDOWSIZES[Config.WINDOWSIZES.Length - 1]; try { ComputeSubFrames(cx - (bigwSize >> 1), cy - (bigwSize >> 1), cx + (bigwSize >> 1), cy + (bigwSize >> 1), sframes); for (int p = 0; p < sframes.Count; p += 3) { SubFrames[SubFramePos] = sframes[p]; SubFrames[SubFramePos + 1] = sframes[p + 1]; SubFrames[SubFramePos + 2] = sframes[p + 2]; SubFramePos += 3; if (SubFramePos > SubFrames.Length - 1) SubFramePos = 0; } } catch { } } } } return FacesPos; }
public static float[] CLFastTrack(Bitmap bmp, float threshold) { swIntegrate.Reset(); swBacktrack.Reset(); swSmooth.Reset(); int W = bmp.Width; int H = bmp.Height; CLthresh.WriteToDevice(new float[] { threshold }); CLpenalties.WriteToDevice(new float[] { distancePenalty, changePenalty }); CLDim.WriteToDevice(new int[] { W, H }); if (CLimg == null || CLimg.Width != W || CLimg.Height != H) { CLimg = new CLCalc.Program.Image2D(bmp); } else { CLimg.WriteBitmap(bmp); } swIntegrate.Start(); float[] intM = new float[W * H]; //intM[x,y] = intM[y + H*x] if (CLintM == null || CLintM.OriginalVarLength != W * H) { CLintM = new CLCalc.Program.Variable(intM); } //initialize integration - copy first column kernelinitIntM.Execute(new CLCalc.Program.MemoryObject[] { CLimg, CLintM, CLthresh }, H); //perform integration CLx.WriteToDevice(new int[] { 0 }); for (int x = 1; x < W; x++) { //CLx.WriteToDevice(new int[] { x }); kernelincCounter.Execute(new CLCalc.Program.MemoryObject[] { CLx }, 1); kernelintM.Execute(new CLCalc.Program.MemoryObject[] { CLimg, CLintM, CLthresh, CLx, CLpenalties }, H); } //CLintM.ReadFromDeviceTo(intM); swIntegrate.Stop(); swBacktrack.Start(); int[] path = new int[W]; if (CLPath == null || CLPath.OriginalVarLength != W) { CLPath = new CLCalc.Program.Variable(path); } kernelBackTrack.Execute(new CLCalc.Program.MemoryObject[] { CLintM, CLPath, CLDim }, 1); CLPath.ReadFromDeviceTo(path); //CLintM.ReadFromDeviceTo(intM); //int idMax = 0; //float valMax = intM[H*(W - 1)]; ////find most amplified path //for (int y = 0; y < H; y++) //{ // if (valMax < intM[H*(W - 1) + y]) // { // valMax = intM[H* (W - 1) + y]; // idMax = y; // } //} ////backtrack path which was used //int[] path = new int[W]; //path[W - 1] = idMax; //for (int x = W - 2; x >= 0; x--) //{ // int y = path[x + 1]; // float maxv = -1e8f; // int idv = -1; // for (int k = -PIXELSTOSEARCH; k <= PIXELSTOSEARCH; k++) // { // if (y + k >= 0 && y + k < H) // { // if (intM[H*x + y + k] > maxv) // { // maxv = intM[H*x + y + k]; // idv = y + k; // } // } // } // path[x] = idv; //} swBacktrack.Stop(); swSmooth.Start(); //integrate path float[] pathInt = new float[W]; float[] smoothPath = new float[W]; pathInt[0] = path[0]; for (int j = 1; j < W; j++) { pathInt[j] = pathInt[j - 1] + path[j]; } //smooth path for (int j = 0; j < W; j++) { int idxP0 = j - smoothWindowSize; int idxPf = j + smoothWindowSize; if (idxP0 < 0) { idxP0 = 0; } if (idxPf > W - 1) { idxPf = W - 1; } smoothPath[j] = (pathInt[idxPf] - pathInt[idxP0]) / (float)(idxPf - idxP0); } swSmooth.Stop(); return(smoothPath); }