internal unsafe static Histograms Calculate(Bitmap bitmap) { Bitmap b = bitmap; BitmapData bData = b.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, b.PixelFormat); Histograms histograms = new Histograms(); byte bitsPerPixel = (byte)Image.GetPixelFormatSize(b.PixelFormat); /*This time we convert the IntPtr to a ptr*/ byte* scan0 = (byte*)bData.Scan0.ToPointer(); for (int i = 0; i < bData.Height; ++i) { for (int j = 0; j < bData.Width; ++j) { // Ignore borders. if (i < 40 || j < 40 || i >= 112 - 40 || j >= 112 - 40) continue; byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8; byte blue = *data; byte green = *(data + 1); byte red = *(data + 2); histograms.Red.Buckets[red / Histogram.BucketSize]++; histograms.Green.Buckets[green / Histogram.BucketSize]++; histograms.Blue.Buckets[blue / Histogram.BucketSize]++; //data is a pointer to the first byte of the 3-byte color data } } b.UnlockBits(bData); return histograms; }
public Gem(GemColor gemColor, Bitmap bitmap, int x = 0, int y = 0) { Bitmap = bitmap; X = x; Y = y; GemColor = gemColor; Histograms = Histograms.Calculate(bitmap); }
private void bRecalcHisto_Click(object sender, EventArgs e) { foreach (Gem gem in KnowledgeBase.TrainingData) { gem.Histograms = Histograms.Calculate(gem.Bitmap); } Log("Recalc complete. Don't forget to save!"); }
internal static int GetDistance(Histograms histograms1, Histograms histograms2) { int distance = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < Histogram.BucketCount; j++) { distance += Math.Abs(histograms1.AllHistograms[i].Buckets[j] - histograms2.AllHistograms[i].Buckets[j]); } } return(distance); }
public override GemColor GetColor(Gem gem) { if (Database.Count == 0) { return(GemColor.Unrecognized); } Gem nearestGem = Database[0]; int smallestDistance = Int32.MaxValue; foreach (Gem datagem in Database) { int dist = Histograms.GetDistance(gem.Histograms, datagem.Histograms); if (dist < smallestDistance) { smallestDistance = dist; nearestGem = datagem; } } gem.Inaccuracy = smallestDistance; return(nearestGem.GemColor); }
internal unsafe static Histograms Calculate(Bitmap bitmap) { Bitmap b = bitmap; BitmapData bData = b.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, b.PixelFormat); Histograms histograms = new Histograms(); byte bitsPerPixel = (byte)Image.GetPixelFormatSize(b.PixelFormat); /*This time we convert the IntPtr to a ptr*/ byte *scan0 = (byte *)bData.Scan0.ToPointer(); for (int i = 0; i < bData.Height; ++i) { for (int j = 0; j < bData.Width; ++j) { // Ignore borders. if (i < 40 || j < 40 || i >= 112 - 40 || j >= 112 - 40) { continue; } byte *data = scan0 + i * bData.Stride + j * bitsPerPixel / 8; byte blue = *data; byte green = *(data + 1); byte red = *(data + 2); histograms.Red.Buckets[red / Histogram.BucketSize]++; histograms.Green.Buckets[green / Histogram.BucketSize]++; histograms.Blue.Buckets[blue / Histogram.BucketSize]++; //data is a pointer to the first byte of the 3-byte color data } } b.UnlockBits(bData); return(histograms); }
public void Process(Bitmap fullScreenshot) { Mode mode = Form.CurrentMode; Bitmap field = fullScreenshot.Clone(mode.GetField(), fullScreenshot.PixelFormat); if (Form.CurrentMode.GetType() == typeof(DiamondMine)) { Bitmap cleared = fullScreenshot.Clone(new Rectangle(582, 788, 875, 6), fullScreenshot.PixelFormat); Color averageColor = Histograms.GetAverageColor(cleared); if (averageColor.R >= 200 && averageColor.G >= 200) { DiamondMineCleared = true; DiamondMineLastCleared = DateTime.Now; } else { if (DiamondMineLastCleared.AddSeconds(1) < DateTime.Now) { DiamondMineCleared = false; } } cleared.Dispose(); } // Update Grid from image for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { Bitmap thisGem = field.Clone(new Rectangle(x * 112, y * 112, 112, 112), field.PixelFormat); Grid[x, y] = new Gem(GemColor.Unrecognized, thisGem, x, y); } } // Recognize for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { Grid[x, y].GemColor = Form.RecognitionStrategy.GetColor(Grid[x, y]); } } // Find options Predictor.Predict(Grid); // Update Field Image if (Form.pictureBoxField.Image != null) { Form.pictureBoxField.Image.Dispose(); } Form.pictureBoxField.Image = field; // Update Grid based on Grid Action a = () => { Form.pictureBoxGrid.Refresh(); }; Form.pictureBoxGrid.Invoke(a); }
internal static int GetDistance(Histograms histograms1, Histograms histograms2) { int distance = 0; for(int i = 0; i < 2; i++) { for (int j = 0; j < Histogram.BucketCount; j++) { distance += Math.Abs(histograms1.AllHistograms[i].Buckets[j] - histograms2.AllHistograms[i].Buckets[j]); } } return distance; }