public static double[] AverageRGB(Bitmap img, Window w) { double[] colors = new double[3]; for(int x = w.StartX; x < w.EndX; x++) for(int y = w.StartY; y < w.EndY; y++) { Color pixval = img.GetPixel(x,y); colors[0] += pixval.R; colors[1] += pixval.G; colors[2] += pixval.B; } double multfactor = 1/((double)w.Area); for(int i = 0; i < 3; i++) colors[i] *= multfactor; return colors; }
/// <summary> /// Gets the histogram of oriented gradients corresponding to a particular window in the specified bmp. /// </summary> /// <returns> /// The hogs. /// </returns> /// <param name='bmp'> /// The bitmap. /// </param> /// <param name='w'> /// The window over which to make the histogram. /// </param> public double[] GetHogs(Bitmap bmp, Window w) { DenseVector[,] grads = GradientMaker.GetGradients(bmp); double[] hogs = new double[NumOrientations]; for(int x = w.StartX; x < w.EndX; x++) for(int y = w.StartY; y < w.EndY; y++) { DenseVector g = grads[x,y]; //PREPARE FOR HACK double angle = Math.Atan2 (g[1],g[0]); if(angle < 0) angle += 2*Math.PI; int orientation = (int)Math.Floor((((double)NumOrientations)/(2*Math.PI))*angle); orientation = orientation % NumOrientations; //Hack moar--mitigated now. Still bad. double magnitude = g.Norm(2); hogs[orientation] += magnitude; } return hogs; }
/// <summary> /// Gets the histogram of oriented gradients corresponding to a particular window in the specified bmp, /// and smoothes it. /// </summary> /// <returns> /// The hogs. /// </returns> /// <param name='bmp'> /// The bitmap. /// </param> /// <param name='w'> /// The window over which to make the histogram. /// </param> public double[] GetSmoothedHogs(Bitmap bmp, Window w) { if(bmp == null) throw new ArgumentNullException("bmp", "Specify a non-null argument."); if(w == null) throw new ArgumentNullException("w", "Specify a non-null argument."); double[] hogs = GetHogs(bmp, w); double[] shogs = new double[NumOrientations]; for(int i = 0; i < NumOrientations; i++) { double numerator = 0; double denom = 0; int b = Convert.ToInt32(Math.Ceiling(SMOOTHING_KERNEL_BANDWIDTH)); for(int j = i - Math.Min(b, NumOrientations/2); j <= i + Math.Min(b, NumOrientations/2); j++) { // As long as the kernel bandwidth is not larger than NUM_ORIENTATIONS/2, we don't // count directions multiple times. double coeff = SmoothingKernel(((double)(i-j))/SMOOTHING_KERNEL_BANDWIDTH); denom += coeff; numerator += coeff*hogs[(j+NumOrientations)%NumOrientations]; } shogs[i] = numerator/denom; if(denom == 0) throw new NotFiniteNumberException(); //Dealing with area mismatches shogs[i] *= 4096; shogs[i] /= w.Area; } return shogs; }