public double Calculate(GenerationImage image) { double error = 0; using (Bitmap bitmap = image.GetBitmap()) { int width = bitmap.Width; int height = bitmap.Height; using (Graphics g = Graphics.FromImage(bitmap)) { image.Render(g); BitmapData bmd = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c1 = GetPixel(bmd, x, y); Color c2 = _source[x, y]; double pixelError = GetColorFitness(c1, c2); error += pixelError; } } bitmap.UnlockBits(bmd); } } return(error); }
private void InitializeImages(Image image) { _sourceImage = image; pictureBoxSource.Image = image; int width = image.Width; int height = image.Height; _sourceColors = new Color[width, height]; using (Bitmap b = new Bitmap(image)) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = b.GetPixel(x, y); _sourceColors[x, y] = c; } } } _fitness = new Fitness(_sourceColors); _currentResultImage = new GenerationImage(width, height); _currentResultScore = double.MaxValue; }
private void RunGeneration() { GenerationImage bestNewImage = null; double bestNewScore = double.MaxValue; for (int i = 0; i < 40; i++) { GenerationImage newImage = _currentResultImage.Clone(); MutateImage(newImage); double score = _fitness.Calculate(newImage); if (score < bestNewScore) { bestNewImage = newImage.Clone(); bestNewScore = score; } } if (bestNewScore < _currentResultScore) { _currentResultImage = bestNewImage.Clone(); _currentResultScore = bestNewScore; } _generationCount++; }
public GenerationImage Clone() { GenerationImage image = new GenerationImage(_width, _height); foreach (IElement element in _elements) { image._elements.Add(element); } return(image); }
private void MutateImage(GenerationImage image) { if (radioButtonDot.Checked) { image.Mutate <Dot>(ColorMode); } else if (radioButtonLine.Checked) { image.Mutate <Line>(ColorMode); } else if (radioButtonPolygon.Checked) { image.Mutate <Polygon>(ColorMode); } }