internal Node Apply(Patch p) { var nd = Root; while(!nd.IsLeaf) { if (nd.Feature.ComputeFeature(p)) nd=nd.Right; else nd=nd.Left; } return nd; }
public void AddPatches(List<Patch> patches,int offsetx, int offsety,int patchWidth,int patchHeight) { int w = Bitmap.PixelWidth; int h = Bitmap.PixelHeight; for (int y = 0; y <= h - patchHeight; y += offsety) { for (int x = 0; x <= w - patchWidth; x += offsetx) { var p = new Patch { Left = x, Top = y, Width = patchWidth, Height = patchHeight, Image = this }; patches.Add(p); } } }
/// <summary> /// Computes the feature. /// </summary> /// <param name="p"></param> /// <returns></returns> public bool ComputeFeature(Patch p) { double angleInRads = 2 * Math.PI * p.Angle / 360.0; var cos = Math.Cos(angleInRads); var sin = Math.Sin(angleInRads); double rotx = OffsetX * cos - OffsetY * sin; double roty = OffsetX * sin + OffsetY * cos; // todo: check that this doesn't go outside of the patch. int tx = p.Left + p.NudgeX+ + p.Width / 2 + (int)Math.Round(rotx); int ty = p.Top + p.NudgeY+ p.Height / 2 + (int)Math.Round(roty); var px = p.Image.Pixels[ty, tx]; byte val = px.Red; if (Channel == 1) val = px.Green; else if (Channel == 2) val=px.Blue; return (LowerThreshold <= val) && (val<UpperThreshold); }
public void Sample(Random rnd, Patch p) { bool inCircle = false; int halfWidth = p.Width / 2; do { // todo: work out about even vs odd size patches OffsetX = rnd.Next(p.Width) - halfWidth; OffsetY = rnd.Next(p.Height) - p.Height/2; inCircle = (OffsetX * OffsetX + OffsetY * OffsetY)<=(halfWidth*halfWidth); } while (!inCircle); LowerThreshold = rnd.Next(256); UpperThreshold = rnd.Next(256); if (LowerThreshold > UpperThreshold) { var dummy = LowerThreshold; LowerThreshold = UpperThreshold; UpperThreshold = dummy; } UpperThreshold = 256; Channel = (byte)rnd.Next(3); }
private List<IFeature> GetFeatures(Patch p,int N) { var features = new List<IFeature>(); for (int i = 0; i < N; i++) { bool useHist = rnd.Next(3)>0; var f = useHist ? (IFeature)new HistogramFeature() : (IFeature)new AbsoluteIntensityFeature(); f.Sample(rnd,p); features.Add(f); } return features; }
public void Sample(Random rnd, Patch p) { Bin = rnd.Next(p.Histogram[0].Length); Threshold = rnd.Next(p.Width * p.Height); Channel = (byte)rnd.Next(3); }
public bool ComputeFeature(Patch p) { return p.Histogram[Channel][Bin] > Threshold; }