private void CameraOne_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap bitmap1 = (Bitmap)(eventArgs.Frame.DeepClone()); Grayscale filter = new Grayscale(0.2, 0.7, 0.07); if (checkBox1.Checked) { bitmap1 = filter.Apply(bitmap1); } int x = 0; Invoke(new MethodInvoker(delegate { x = trackBar1.Value; })); RotateNearestNeighbor filterrot = new RotateNearestNeighbor(x, true); bitmap1 = filterrot.Apply(bitmap1); ResizeBicubic filterResizeBicubic = new ResizeBicubic(320, 240); bitmap1 = filterResizeBicubic.Apply(bitmap1); if (button1WasClicked) { Convolution conv = new Convolution(new[, ] { { int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text) }, { int.Parse(textBox4.Text), int.Parse(textBox5.Text), int.Parse(textBox6.Text) }, { int.Parse(textBox7.Text), int.Parse(textBox8.Text), int.Parse(textBox9.Text) } }); bitmap1 = conv.Apply(bitmap1); } pictureBox1.Image = bitmap1; }
private static Bitmap CreateGrayscale(Bitmap image) { const int size = 512; float scale = Math.Min(size / (float)image.Width, size / (float)image.Height); ResizeBicubic resize = new ResizeBicubic((int)(image.Width*scale), (int)(image.Height*scale)); ImageStatistics stat = new ImageStatistics( image ); LevelsLinear levelsLinear = new LevelsLinear { Output = new IntRange(0, 255), InRed = stat.Red.GetRange(.95), InBlue = stat.Blue.GetRange(.95), InGreen = stat.Green.GetRange(.95) }; SaturationCorrection sc = new SaturationCorrection(-1); Bitmap square = new Bitmap(size, size); using(Bitmap resized = resize.Apply(sc.Apply(levelsLinear.Apply(image)).MakeGrayscale())) using (Graphics g = Graphics.FromImage(square)) { g.DrawImage(resized, new Point((size-resized.Width) / 2 ,0)); } return square; }
public Bitmap Resize(Size resize, Bitmap inputBitmap) { Bitmap outputBitmap = inputBitmap; // Resize to max dimension ResizeBicubic filterResize = null; if (inputBitmap.Height > resize.Height) { double factor = System.Convert.ToDouble(inputBitmap.Height) / System.Convert.ToDouble(resize.Height); int newWidth = System.Convert.ToInt32(Math.Round(System.Convert.ToDouble(inputBitmap.Width) / factor, 0)); filterResize = new ResizeBicubic(newWidth, resize.Height); } else if (inputBitmap.Width > resize.Width) { double factor = System.Convert.ToDouble(inputBitmap.Width) / System.Convert.ToDouble(resize.Width); int newHeight = System.Convert.ToInt32(Math.Round(System.Convert.ToDouble(inputBitmap.Height) / factor, 0)); filterResize = new ResizeBicubic(resize.Width, newHeight); } if (filterResize != null) { outputBitmap = filterResize.Apply(inputBitmap); } return(outputBitmap); }
private Bitmap preProcess(Bitmap originalImage) { Invert invertObj = new Invert(); Bitmap invertImage = invertObj.Apply((Bitmap)originalImage.Clone()); invertImage = Grayscale.CommonAlgorithms.BT709.Apply(invertImage); Threshold bwObject = new Threshold(); invertImage = bwObject.Apply(invertImage); ExtractBiggestBlob blobObject = new ExtractBiggestBlob(); invertImage = blobObject.Apply(invertImage); ResizeBicubic resize = new ResizeBicubic(60, 90); invertImage = resize.Apply(invertImage); //CannyEdgeDetector edgeDetector = new CannyEdgeDetector(); //invertImage = edgeDetector.Apply(invertImage); return(invertImage); }
public static Bitmap ResizeImage(this Bitmap originalImage, int newWidth, int newHeight) { double aspectRatio; int calculatedWidth, calculatedHeight; if (originalImage.Width > originalImage.Height) { calculatedWidth = newWidth; aspectRatio = (float)newWidth / originalImage.Width; calculatedHeight = Convert.ToInt32(originalImage.Height * aspectRatio); } else { calculatedHeight = newHeight; aspectRatio = (float)newHeight / originalImage.Height; calculatedWidth = Convert.ToInt32(originalImage.Width * aspectRatio); } if (originalImage.Width <= calculatedWidth || originalImage.Height <= calculatedHeight) { calculatedHeight = originalImage.Height; calculatedWidth = originalImage.Width; } var resizeFilter = new ResizeBicubic(calculatedWidth, calculatedHeight); Bitmap result = resizeFilter.Apply(originalImage); return(result); }
public mFitScale(Bitmap SourceBitmap, Bitmap TargetBitmap) { BitmapType = mFilter.BitmapTypes.Rgb24bpp; double Xratio = (((double)TargetBitmap.Width) / ((double)SourceBitmap.Width)); double Yratio = (((double)TargetBitmap.Height) / ((double)SourceBitmap.Height)); double Scale = 1; if (Xratio > Yratio) { Scale = Xratio; } else { Scale = Yratio; } int X = (int)(((double)SourceBitmap.Width) * Scale); int Y = (int)(((double)SourceBitmap.Height) * Scale); EffectA = new ResizeBicubic(X, Y); int XC = (int)(((double)(X - TargetBitmap.Width)) / 2.0); int XY = (int)(((double)(Y - TargetBitmap.Height)) / 2.0); EffectB = new Crop(new Rectangle(XC, XY, TargetBitmap.Width, TargetBitmap.Height)); Sequence.Clear(); Sequence.Add(EffectA); Sequence.Add(EffectB); }
public Bitmap GenerateThumb(int width) { int height = Convert.ToInt32(Convert.ToDouble(this.ComicImage.Height) / Convert.ToDouble(this.ComicImage.Width) * Convert.ToDouble(width)); ResizeBicubic filterResize = new ResizeBicubic(width, height); return(filterResize.Apply(this.ComicImage)); }
/// <summary> /// 将图片大小标准化(4M限制) /// </summary> /// <param name="image"></param> /// <param name="width">压缩目标宽,会按原比例缩放</param> /// <param name="height">压缩目标高,会按原比例缩放</param> /// <returns></returns> public static Bitmap miniSizeImage(System.Drawing.Image image, int width, int height) { int _height = 0; int _width = 0; //height much bigger , so height is the max number of size if (image.Size.Height > image.Size.Width) { _height = height; _width = (int)((double)image.Size.Width / image.Size.Height * height); } else { _height = (int)((double)image.Size.Height / image.Size.Width * width); _width = width; }; ResizeBicubic filter = new ResizeBicubic(_width, _height); // apply the filter try { return(filter.Apply(image as Bitmap)); } catch (Exception ex) { return(image as Bitmap); } }
private bool CheckScreen(Bitmap temp, int thresh) { if (_screen == null) { return(false); } float threshold = thresh / 100f; System.Drawing.Bitmap sourceImage = AForge.Imaging.Image.Clone(_screen, PixelFormat.Format24bppRgb); System.Drawing.Bitmap template = AForge.Imaging.Image.Clone(temp, PixelFormat.Format24bppRgb); // create template matching algorithm's instance //strip sizes down for speed double reductionAmount = .2; sourceImage = new ResizeBicubic((int)(sourceImage.Width * reductionAmount), (int)(sourceImage.Height * reductionAmount)).Apply(sourceImage); template = new ResizeBicubic((int)(template.Width * reductionAmount), (int)(template.Height * reductionAmount)).Apply(template); // (set similarity threshold to 92.1%) ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(threshold); // find all matchings with specified above similarity TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template); if (matchings.Length == 0) { return(false); } else if (matchings.Length == 1) { return(true); } Compiler.ExceptionListener.ThrowSilent(new ExceptionHandler($"[IMG RECOGNITION] More than one match found: {matchings.Length}")); return(false); }
public void resize_bicubic() { double[,] diag = Matrix.Magic(5); diag = diag.Divide(diag.Max()); Bitmap input = diag.ToBitmap(); // Create a new resize bilinear filter var filter = new ResizeBicubic(7, 8); // Apply the filter Bitmap output = filter.Apply(input); Assert.AreEqual(7, output.Width); Assert.AreEqual(8, output.Height); double[,] actual; new ImageToMatrix().Convert(output, out actual); string str = actual.ToString(CSharpMatrixFormatProvider.InvariantCulture); double[,] expected = { { 0.725490196078431, 0.780392156862745, 1, 0.352941176470588, 0.0313725490196078, 0.341176470588235, 0.588235294117647 }, { 0.733333333333333, 0.749019607843137, 0.909803921568627, 0.313725490196078, 0.0745098039215686, 0.352941176470588, 0.56078431372549 }, { 1, 0.827450980392157, 0.376470588235294, 0.211764705882353, 0.286274509803922, 0.501960784313725, 0.596078431372549 }, { 0.733333333333333, 0.568627450980392, 0.16078431372549, 0.250980392156863, 0.470588235294118, 0.666666666666667, 0.745098039215686 }, { 0.16078431372549, 0.164705882352941, 0.223529411764706, 0.407843137254902, 0.623529411764706, 0.811764705882353, 0.866666666666667 }, { 0.290196078431373, 0.290196078431373, 0.364705882352941, 0.56078431372549, 0.780392156862745, 0.874509803921569, 0.462745098039216 }, { 0.443137254901961, 0.43921568627451, 0.529411764705882, 0.745098039215686, 0.819607843137255, 0.654901960784314, 0.203921568627451 }, { 0.447058823529412, 0.474509803921569, 0.67843137254902, 0.96078431372549, 0.72156862745098, 0.12156862745098, 0.282352941176471 } }; Assert.IsTrue(expected.IsEqual(actual, 1e-6)); }
public Bitmap ReescaladoBicubico(int anchura, int altura) { ResizeBicubic resize = new ResizeBicubic(anchura, altura); imagen = resize.Apply(imagen); return(imagen); }
public static Bitmap FitImage(Size targetSize, Bitmap inputBitmap) { Size resize = GetFitImageSize(new Size(inputBitmap.Width, inputBitmap.Height), targetSize); ResizeBicubic filterResize = new ResizeBicubic(resize.Width, resize.Height); return(filterResize.Apply(inputBitmap)); }
public static Bitmap Zoom(Bitmap image, double zoomPercentage) { int newWidth = (int)Math.Floor(image.Width * zoomPercentage); int newHeight = (int)Math.Floor(image.Height * zoomPercentage); ResizeBicubic resizer = new ResizeBicubic(newWidth, newHeight); return(resizer.Apply(image)); }
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) { ResizeBicubic filter = new ResizeBicubic(328, 275); Bitmap newImage = filter.Apply((Bitmap)eventArgs.Frame.Clone()); pictureBox1.Image = newImage; //pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); }
private static Bitmap Resizeimage(Bitmap image, int NewWidth, int NewHeight) { //set up filter ResizeBicubic ResizeFilter = new ResizeBicubic(NewWidth, NewHeight); //apply filter image = ResizeFilter.Apply(image); return(image); }
private void ResizeBitmapToAnalyze() { if (_bitmapToAnalyze.Width > _maxWidth) { var bmp = new ResizeBicubic((int)Math.Floor(_bitmapToAnalyze.Width * _resizefactor), (int)Math.Floor(_bitmapToAnalyze.Height * _resizefactor)).Apply(_bitmapToAnalyze); _bitmapToAnalyze.Dispose(); _bitmapToAnalyze = bmp; } }
//scale bicubic private static Bitmap Scale(Bitmap image) { IRandomNumberGenerator generator = new GaussianGenerator(1.05f, 0.1f); ResizeBicubic filter = new ResizeBicubic((int)(image.Width * 1.035), (int)(image.Height * 1.035)); // apply the filter return(filter.Apply(image)); }
public AForge.Imaging.UnmanagedImage GetImage() { ResizeBicubic resize = new ResizeBicubic(28, 28); lock (balanceLock) { return(resize.Apply(processed)); } }
public mFitStretch(Bitmap SourceBitmap, Bitmap TargetBitmap) { BitmapType = mFilter.BitmapTypes.Rgb24bpp; Effect = new ResizeBicubic(TargetBitmap.Width, TargetBitmap.Height); Sequence.Clear(); Sequence.Add(Effect); }
public AForge.Imaging.UnmanagedImage GetInput(out double[] input, int countBlocks = 100) { double toDbl(Color c) { var s = c.R + c.B + c.G; if (s > 50) { return(1.0); } return(-1); } input = new double[countBlocks * countBlocks + 1]; AForge.Imaging.UnmanagedImage img; var filter = new ResizeBicubic(countBlocks, countBlocks); double angle; lock (balanceLock) { angle = AngleRad; if (processed.Height == countBlocks && processed.Width == countBlocks) { img = processed.Clone(); } else { img = filter.Apply(processed); } } double max_input = 0; for (int i = 0; i < countBlocks; i++) { for (int j = 0; j < countBlocks; j++) { var d = toDbl(img.GetPixel(i, j)); input[i * countBlocks + j] = d; if (d > max_input) { max_input = d; } } } if (max_input != 0) { for (int i = 0; i < input.Length - 1; i++) { input[i] /= max_input; } } input[countBlocks * countBlocks] = AngleRad / Math.PI / 2.0; return(img); }
void goruntuGuncelle() { try { if (comboBox1.SelectedIndex == 0) { //resim döndürme filtresi tanımlandı //bu filtre sistemi yormuyor //diğerleri sistemi zorluyor //resim döndürme saat yönünün tersine doğru yapılıyor //fonksiyona paremetre olarak true verilirse resmin tamamı ekrana sığdırılmıyor //bazı yerler kırpılıyor //fakat resim boyutu (genişliği ve yükseliği) değişmiyor //görüntü daha güzel görünüyor //eğer false olursa resim küçültme büyütme işlemelerinde resim boyutuda (genişliği ve yükseliği) değişiyor //yani false olunca resim daima ekrana sığdırılıyor RotateNearestNeighbor boyutlandirmaFiltresi = new RotateNearestNeighbor(trackBar1.Value, tamEkran); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirmaFiltresi.Apply((Bitmap)pictureBox1.Image); //resim boyut değiştirme filtresi tanımlandı //bu filtre sistemi yormuyor //diğerleri sistemi zorluyor ResizeNearestNeighbor boyutlandirma = new ResizeNearestNeighbor(resim.Width + trackBar2.Value, resim.Height + trackBar2.Value); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirma.Apply((Bitmap)pictureBox1.Image); } if (comboBox1.SelectedIndex == 1) { //resim döndürme filtresi tanımlandı RotateBilinear boyutlandirmaFiltresi = new RotateBilinear(trackBar1.Value, tamEkran); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirmaFiltresi.Apply((Bitmap)pictureBox1.Image); //resim boyut değiştirme filtresi tanımlandı ResizeBicubic boyutlandirma = new ResizeBicubic(resim.Width + trackBar2.Value, resim.Height + trackBar2.Value); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirma.Apply((Bitmap)pictureBox1.Image); } if (comboBox1.SelectedIndex == 2) { //resim döndürme filtresi tanımlandı RotateBicubic boyutlandirmaFiltresi = new RotateBicubic(trackBar1.Value, tamEkran); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirmaFiltresi.Apply((Bitmap)pictureBox1.Image); //resim boyut değiştirme filtresi tanımlandı ResizeBilinear boyutlandirma = new ResizeBilinear(resim.Width + trackBar2.Value, resim.Height + trackBar2.Value); //resim dosyasına filtre uygulandı pictureBox2.Image = boyutlandirma.Apply((Bitmap)pictureBox1.Image); } } catch { } }
public static void video_Start() { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); FilterResize = new ResizeBicubic(W_resolution, H_resolution); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); CloseVideoSource(); videoSource.Start(); }
public mResizeBicubic(int ImageWidth, int ImageHeight) { BitmapType = mFilter.BitmapTypes.Rgb24bpp; Width = ImageWidth; Height = ImageHeight; Effect = new ResizeBicubic(Width, Height); Sequence.Clear(); Sequence.Add(Effect); }
/// <summary> /// /// </summary> /// <param name="img"></param> /// <returns></returns> private Image CreateThumb(Image img) { double thumbSize = 160f; //double aspect = (float)img.Width / (float)img.Height; double factor = Math.Max(img.Width, img.Height) / thumbSize; int w = (int)Math.Round(img.Width / factor); int h = (int)Math.Round(img.Height / factor); ResizeBicubic filter = new ResizeBicubic(w, h); return(AddinUtils.ProcessImage(filter, img)); }
private void finalFrameEvent(Object sender, NewFrameEventArgs nfea) { try { ResizeBicubic filter = new ResizeBicubic(pboReader.Width, pboReader.Height); pboReader.Image = filter.Apply((Bitmap)nfea.Frame.Clone()); } catch (Exception ex) { Integrity.GetExceptionDetails(ex); this.CloseCam(); } }
/// <summary> /// /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> private Image ResizeImage(Image image, int newSize) { float largeSize = Math.Max(image.Width, image.Height); float factor = (float)newSize / largeSize; ResizeBicubic filter = new ResizeBicubic((int)Math.Round(image.Width * factor), (int)Math.Round(image.Height * factor)); Image dst = filter.Apply(image as Bitmap); foreach (PropertyItem item in image.PropertyItems) { dst.SetPropertyItem(item); } return(dst); }
public Bitmap Filter(Bitmap input_image) { Grayscale gray_filter = new Grayscale(0.2125, 0.7154, 0.0721); BradleyLocalThresholding threshold_filter = new BradleyLocalThresholding(); threshold_filter.PixelBrightnessDifferenceLimit = 0.5f; ResizeBicubic scale_small_filter = new ResizeBicubic(28, 28); Crop crop_filter = new Crop(new Rectangle(51, 51, 199, 199)); BlobCounter blober = new BlobCounter(); blober.FilterBlobs = true; blober.ObjectsOrder = ObjectsOrder.Size; Bitmap image = gray_filter.Apply(input_image); image = threshold_filter.Apply(image); image = crop_filter.Apply(image); //blober.ProcessImage(image); //Blob[] blobs = blober.GetObjectsInformation(); //if (blobs.Length > 0) //{ // var bigger = blobs[0]; // UnmanagedImage img = UnmanagedImage.FromManagedImage(image); // blober.ExtractBlobsImage(img, bigger, false); // Accord.Point mc = bigger.CenterOfGravity; // Accord.Point ic = new Accord.Point((float)bigger.Image.Width / 2, (float)bigger.Image.Height / 2); // float AngleRad = (ic.Y - mc.Y) / (ic.X - mc.X); // float Angle = (float)(AngleRad * 180 / Math.PI); // image = img.ToManagedImage(); // RotateBicubic rot_filter = new RotateBicubic(Angle); // image = rot_filter.Apply(image); //} image = scale_small_filter.Apply(image); return(image); }
private void button_catch_Click(object sender, EventArgs e) { Bitmap input_image = pictureBox1.Image as Bitmap; ResizeBicubic scale_filter = new ResizeBicubic(pictureBox2.Width, pictureBox2.Height); image = net.Filter(input_image); pictureBox3.Image = image; pictureBox3.Refresh(); Bitmap bigger_image = scale_filter.Apply(image); pictureBox2.Image = bigger_image; pictureBox2.Refresh(); }
private void button7_Click(object sender, EventArgs e) { string path = OriPath + "\\test.jpg"; string pathoutput2 = OriPath + "\\testoutput2.jpg"; string pathoutput3 = OriPath + "\\testoutput3.jpg"; string pathoutput4 = OriPath + "\\testoutput4.jpg"; string pathoutput5 = OriPath + "\\testoutput5.jpg"; string pathoutput6 = OriPath + "\\testoutput6.jpg"; string pathoutput7 = OriPath + "\\testoutput7.jpg"; Bitmap image = new Bitmap(path); // 普通最近领算法 ResizeNearestNeighbor filter = new ResizeNearestNeighbor(4000, 3000); // 双线性插值 ResizeBicubic filter2 = new ResizeBicubic(4000, 3000); // 双三次插值 ResizeBilinear filter3 = new ResizeBilinear(4000, 3000); // create filter - rotate for 30 degrees keeping original image size RotateNearestNeighbor filter4 = new RotateNearestNeighbor(30, true); RotateBilinear filter5 = new RotateBilinear(30, true); RotateBicubic filter6 = new RotateBicubic(30, true); // apply the filter Bitmap newImage = filter.Apply(image); newImage.Save(pathoutput2); newImage = filter2.Apply(image); newImage.Save(pathoutput3); newImage = filter3.Apply(image); newImage.Save(pathoutput4); newImage = filter4.Apply(image); newImage.Save(pathoutput5); newImage = filter5.Apply(image); newImage.Save(pathoutput6); newImage = filter6.Apply(image); newImage.Save(pathoutput7); }
/// <summary> /// Apply a filter over an image /// </summary> /// <param name="imageFilterParameters">Filter parameters</param> /// <returns>Image result of apply filter</returns> public static Bitmap ApplyFilter(ImageFilterParameters imageFilterParameters) { dynamic filterProcessor; // choose the filter switch (imageFilterParameters.Filter) { case EnumImageFilter.GrayScale: filterProcessor = new Grayscale(0.2125, 0.7154, 0.0721); break; case EnumImageFilter.Invert: filterProcessor = new Invert(); break; case EnumImageFilter.Median: filterProcessor = new Median(); break; case EnumImageFilter.Rotate: var finalAngle = imageFilterParameters.Angle ?? 0; filterProcessor = new RotateBicubic(finalAngle); break; case EnumImageFilter.TexturedHue: filterProcessor = new TexturedFilter(new CloudsTexture(), new HueModifier(50)); break; case EnumImageFilter.Resize: var finalWidth = imageFilterParameters.Width ?? imageFilterParameters.Image.Width; var finalHeight = imageFilterParameters.Height ?? imageFilterParameters.Image.Height; filterProcessor = new ResizeBicubic(finalWidth, finalHeight); break; case EnumImageFilter.FaceDetection: return(FaceDetection(imageFilterParameters.Image).ImageResult); default: filterProcessor = new TexturedFilter(new CloudsTexture(), new GrayscaleBT709(), new Sepia()); break; } // apply the filter var response = filterProcessor.Apply(imageFilterParameters.Image); // return new image return(response); }
void searchSolution(Bitmap rgb) { long start = DateTime.Now.Ticks / 10000; Globals.FRAMES_PROCESSED_TRIANGULAR++; int com_x_sum = 0, com_y_sum = 0, com_x_y_point_count = 0; Globals.HARVEST_SIGN_ID++; //if (redTestPoints == null) // calculateRedTestPoints(); Bitmap bmp = sf.Apply(rgb); GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, this, null); Population tmpPopulation = new Population(Constants.GA_POPULATION_SIZE, sampleChromosome, sampleChromosome, new EliteSelection() ); tmpPopulation.MutationRate = Constants.GA_MUTATION_RATE; tmpPopulation.CrossoverRate = Constants.GA_CROSSOVER_RATE; if (population == null || population.BestChromosome == null || population.BestChromosome.Fitness < RS_THRESHOLD / 4) { // fresh population } else { // half from previous for (int j = 0; j < tmpPopulation.Size / 2; j++) { ((GeoTransChromosome)tmpPopulation[j]).copyContent((GeoTransChromosome)population[j]); } } population = tmpPopulation; Graphics gg = null; /* Bitmap bmp_x = null; if (1!=1) { bmp_x = new Bitmap(Constants.IMAGE_WIDTH, Constants.IMAGE_HEIGHT * 2, PixelFormat.Format24bppRgb); gg = Graphics.FromImage(bmp_x); gg.DrawImage(bmp, 0, 0); gg.DrawImage(rgb, 0, Constants.IMAGE_HEIGHT); for (int x = 0; x < population.Size; x++) { GeoTransChromosome chromo = (GeoTransChromosome)population[x]; gg.DrawRectangle(Pens.Cyan, chromo.X, chromo.Y, 1, 1); } } */ int i = 0; do { long start_epoch = DateTime.Now.Ticks / 10000; Globals.FRAMES_PROCESSED_GA_RUNEPOCH++; // run one epoch of genetic algorithm population.RunEpoch(); if (Constants.EVALUATE_TIME_ENABLED) { int x = (int)(DateTime.Now.Ticks / 10000 - start_epoch); if (x >= 0) { Globals.TIME_GA_RUNEPOCH_MIN = x < Globals.TIME_GA_RUNEPOCH_MIN ? x : Globals.TIME_GA_RUNEPOCH_MIN; Globals.TIME_GA_RUNEPOCH_MAX = x > Globals.TIME_GA_RUNEPOCH_MAX ? x : Globals.TIME_GA_RUNEPOCH_MAX; Globals.TIME_GA_RUNEPOCH_TOTAL += x; } } i++; } while (i < Constants.GA_NUMBER_ITERATIONS); GeoTransChromosome bestChromo = null; float rs = 0; if (population.BestChromosome != null && population.BestChromosome.Fitness > RS_THRESHOLD) { bestChromo = (GeoTransChromosome)population.BestChromosome; int rx = bestChromo.X, ry = bestChromo.Y, rr = bestChromo.Width; rs = (float)bestChromo.Fitness; if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) { rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_0.bmp"); bmp.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_1.bmp"); } // findTriangle(bestChromo.X, bestChromo.Y, bmp, ref rx, ref ry, ref rr, ref rs); if (rs < RS_THRESHOLD || rr==0) { if (gg != null) { gg.DrawRectangle(Pens.Red, bestChromo.X - 1, bestChromo.Y - 1, 3, 3); gg.DrawRectangle(Pens.Red, bestChromo.X - bestChromo.Width / 2, bestChromo.Y - bestChromo.Width / 2, bestChromo.Width, bestChromo.Width); } bestChromo = null; } else { rr = (int)(Constants.CAPTURE_RESIZE_TRIANGLE * rr); int xx = (int)(rr * Math.Cos(Math.PI * 30 / 180)); // üçgenin kenarı: 2 . xx int yy = (int)(rr * Math.Sin(Math.PI * 30 / 180)); // üçgenin yüksekliği: 3 . yy int x_left = rx - xx; int y_top = ry - (2 * yy) - ((3*yy)/10); rgb = new Crop(new Rectangle(x_left, y_top, (x_left + 2 * xx > rgb.Width - 1 ? rgb.Width - 1 - x_left : 2 * xx), (y_top + 2 * xx > rgb.Height - 1 ? rgb.Height - 1 - y_top : 2 * xx))).Apply(rgb); if (gg != null) { gg.DrawImage(rgb, 1, Constants.IMAGE_HEIGHT + 1); gg.DrawRectangle(Pens.DarkOrchid, rx - 1, ry - 1, 3, 3); gg.DrawRectangle(Pens.Green, x_left, y_top, rgb.Width, rgb.Height); } rgb = new ResizeBicubic(Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT).Apply(rgb); if (panel != null) { Graphics g = panel.getPanelGraphics(); g.DrawRectangle(Pens.Silver, 5, 4, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT); g.DrawImage(rgb, new Point(5, 4)); } if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_3_" +rs+ ".bmp"); rgb = AutoBrightnessProcessor.autoBrightness(rgb, Rectangle.Empty, Constants.AUTOBRIGHTNESS_BASE_LUM); if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_4.bmp"); int dynamic_gray = 0; if (Constants.LABELING_TYPE == Constants.LabelingType.redBlackWhite) rgb = new ColorLabelFilter(new Color[] { Color.FromArgb(255, 0, 0), Color.White, Color.Black }, true).Apply(rgb); else if (Constants.LABELING_TYPE == Constants.LabelingType.blackAndWhite) { rgb = new ColorLabelFilter(new Color[] { Color.White, Color.FromArgb(dynamic_gray, dynamic_gray, dynamic_gray) }, true).Apply(rgb); } if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_5.bmp"); int start_x = 0, start_y = 0; int y = 0; for (; y < Constants.SIGN_HEIGHT && (start_x+start_y)==0; y++) { for (int x = 0; x < Constants.SIGN_WIDTH; x++) { Color clr = rgb.GetPixel(x, y); if (clr.R == 0) { start_x = x; start_y = y; break; } } } PointedColorFloodFill filter = new PointedColorFloodFill(); filter.Tolerance = Color.FromArgb(0, 0, 0); filter.FillColor = Color.FromArgb(255, 255, 255); filter.StartingPoint = new Point(start_x, start_y); Bitmap rgb_flood_fill = filter.Apply(rgb); int black_region_found_at_center = 0; y = 4 * (Constants.SIGN_HEIGHT / 10); for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2) { Color clr = rgb_flood_fill.GetPixel(x, y); if (clr.R == 0) { black_region_found_at_center++; } } if (black_region_found_at_center < 8) { y = 6 * (Constants.SIGN_HEIGHT / 10); for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2) { Color clr = rgb_flood_fill.GetPixel(x, y); if (clr.R == 0) { black_region_found_at_center++; } } } if (black_region_found_at_center >= 5) { rgb = rgb_flood_fill; } BitmapData image_data = rgb.LockBits(new Rectangle(0, 0, rgb.Width, rgb.Height), ImageLockMode.ReadWrite, rgb.PixelFormat); int bpp = 3; int nOffset = image_data.Stride - rgb.Width * bpp; System.IntPtr Scan0 = image_data.Scan0; unsafe { byte* p = (byte*)Scan0; for (y = 0; y < Constants.SIGN_HEIGHT; y++) { // for each pixel for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp) { if (y >= Constants.SIGN_HEIGHT - 12 || y <= 14 || x >= Constants.SIGN_WIDTH - 12 || x <= 12 || (x <= Constants.SIGN_WIDTH / 2 && y <= (Constants.SIGN_HEIGHT) - 2 * x + 8) || (x > Constants.SIGN_WIDTH / 2 && y <= 2 * (x - Constants.SIGN_HEIGHT / 2) + 8) ) { p[RGB.R] = 255; p[RGB.G] = 255; p[RGB.B] = 255; } else if (p[RGB.R] == 0) { com_x_sum += x; com_y_sum += y; com_x_y_point_count++; } } p += nOffset; } } rgb.UnlockBits(image_data); if (com_x_y_point_count < 10) bestChromo = null; else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest) { if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestIntoFolder) { string[] signs = Globals.SIGN_IN_FRAME.Split(','); for (int j = 1; j < signs.Length; j++) { if (!Directory.Exists(Constants.base_folder + "hasat\\" + signs[j])) Directory.CreateDirectory(Constants.base_folder + "hasat\\" + signs[j]); lock (Globals.HARVEST_LOCK) { rgb.Save(Constants.base_folder + "hasat\\" + signs[j] + "\\" + Globals.HARVEST_SIGN_ID + ".bmp"); } } } else { rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_6.bmp"); } } gtcBmp = rgb; } } if (Constants.EVALUATE_TIME_ENABLED) { int x = (int)(DateTime.Now.Ticks / 10000 - start); if (x >= 0) { Globals.TIME_TRIANGLE_MIN = x < Globals.TIME_TRIANGLE_MIN ? x : Globals.TIME_TRIANGLE_MIN; Globals.TIME_TRIANGLE_MAX = x > Globals.TIME_TRIANGLE_MAX ? x : Globals.TIME_TRIANGLE_MAX; Globals.TIME_TRIANGLE_TOTAL += x; } } if (bestChromo != null) { if (panel != null) { Graphics g = panel.getPanelGraphics(); g.DrawRectangle(Pens.Silver, 5, 74, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT); g.DrawImage(gtcBmp, new Point(5, 74)); } if (msgService != null) { VisionMessage vm = new VisionMessage(gtcBmp.Height, gtcBmp.Width, ByteTools.pixelFormatToBPP(gtcBmp.PixelFormat), gtcBmp); // Center of Mass vm.CoM_X = com_x_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count); vm.CoM_Y = com_y_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count); msgService.sendMsg(vm); } } else { if (Globals.RIGHT_PANEL_SHOWING_STH) { Globals.RIGHT_PANEL_SHOWING_STH = false; Graphics g = panel.getPanelGraphics(); g.Clear(Color.Silver); } } //if (rs > RS_THRESHOLD) // bmp_x.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_" +rs+ "_" +sf.BlueCoeff+ "_" +sf.GreenCoeff+ ".bmp"); threadRunning = false; }
public NN_Processor(MsgService msgService, Constants.SignType signType) : base(msgService, VisionMessage.msgType) { this.signType = signType; if (signs == null) { signs = new Bitmap[Constants.NUM_OF_SIGN_TYPES + 1]; ResizeBicubic resizer = new ResizeBicubic(32, 32); for (int i = 1; i <= Constants.NUM_OF_SIGN_TYPES; i++) { String file_name = "signs\\sign_" + (i < 10 ? ("0" + i) : ("" + i)) + ".bmp"; signs[i] = resizer.Apply((Bitmap)Bitmap.FromFile(Constants.base_folder + file_name, false)); } } if (networks == null) { networks = NNTrain.loadNetworks(); } }
public NN_SURFProcessor(MsgService msgService, Constants.SignType signType) : base(msgService, VisionMessage.msgType) { this.signType = signType; if (signs == null) { signs = new Bitmap[Constants.NUM_OF_SIGN_TYPES + 1]; ResizeBicubic resizer = new ResizeBicubic(32, 32); for (int i = 1; i <= Constants.NUM_OF_SIGN_TYPES; i++) { String file_name = "signs\\sign_" + (i < 10 ? ("0" + i) : ("" + i)) + ".bmp"; signs[i] = resizer.Apply((Bitmap)Bitmap.FromFile(Constants.base_folder + file_name, false)); } } if (network == null) { network = Network.Load(Constants.base_folder + Constants.NN_SVM_SURF + "_" + (signType == Constants.SignType.circular ? "circle" : "triangle") + ".dat"); } }
void searchSolution(Bitmap rgb) { long start = DateTime.Now.Ticks / 10000; Globals.FRAMES_PROCESSED_CIRCULAR++; int com_x_sum = 0, com_y_sum = 0, com_x_y_point_count = 0; Globals.HARVEST_SIGN_ID++; Bitmap bmp = sf.Apply(rgb); //if (redTestPoints == null) // calculateRedTestPoints(); // GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, redTestPoints, nonRedTestPoints); GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, null, this); if (population == null || population.BestChromosome == null || population.BestChromosome.Fitness < RS_THRESHOLD - 10) { // fresh population population = new Population(Constants.GA_POPULATION_SIZE, sampleChromosome, sampleChromosome, new EliteSelection() ); population.MutationRate = Constants.GA_MUTATION_RATE; population.CrossoverRate = Constants.GA_CROSSOVER_RATE; } else { // half from previous Population tmpPopulation = new Population(Constants.GA_POPULATION_SIZE, sampleChromosome, sampleChromosome, new EliteSelection() ); tmpPopulation.MutationRate = Constants.GA_MUTATION_RATE; tmpPopulation.CrossoverRate = Constants.GA_CROSSOVER_RATE; for (int j = 0; j < tmpPopulation.Size / 2; j++) { ((GeoTransChromosome)tmpPopulation[j]).copyContent((GeoTransChromosome)population.BestChromosome); } population = tmpPopulation; } int i = 0; do { long start_epoch = DateTime.Now.Ticks / 10000; Globals.FRAMES_PROCESSED_GA_RUNEPOCH++; // run one epoch of genetic algorithm population.RunEpoch(); i++; if (Constants.EVALUATE_TIME_ENABLED) { int x = (int)(DateTime.Now.Ticks / 10000 - start_epoch); if (x >= 0) { Globals.TIME_GA_RUNEPOCH_MIN = x < Globals.TIME_GA_RUNEPOCH_MIN ? x : Globals.TIME_GA_RUNEPOCH_MIN; Globals.TIME_GA_RUNEPOCH_MAX = x > Globals.TIME_GA_RUNEPOCH_MAX ? x : Globals.TIME_GA_RUNEPOCH_MAX; Globals.TIME_GA_RUNEPOCH_TOTAL += x; } } } while (i < Constants.GA_NUMBER_ITERATIONS); GeoTransChromosome bestChromo = null; if (population.BestChromosome != null && population.BestChromosome.Fitness > RS_THRESHOLD) { bestChromo = (GeoTransChromosome)population.BestChromosome; // int rx = 0, ry = 0, rr = 0; // float rs = 0f; int rx = bestChromo.X, ry = bestChromo.Y, rr = bestChromo.Width; float rs = (float)bestChromo.Fitness; // findCircle(bestChromo.X, bestChromo.Y, bmp, ref rx, ref ry, ref rr, ref rs); if (rs < RS_THRESHOLD || rr==0) { bestChromo = null; } else { rr = (int)(Constants.CAPTURE_RESIZE_CIRCLE * rr); // rr = rr - 2; // Graphics g = Graphics.FromImage(rgb); // g.DrawEllipse(Pens.Cyan, rx - rr, ry - rr, rr * 2, rr * 2); if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) { rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_0.bmp"); bmp.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_1.bmp"); } rgb = new Crop(new Rectangle(rx - rr, ry - rr, (rx + rr > rgb.Width - 1 ? rgb.Width - 1 - rx : 2 * rr + 2), (ry + rr > rgb.Height - 1 ? rgb.Height - 1 - ry : 2 * rr + 2))).Apply(rgb); rgb = new ResizeBicubic(Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT).Apply(rgb); if (panel != null) { Graphics g = panel.getPanelGraphics(); g.DrawRectangle(Pens.Silver, 5, 4, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT); g.DrawImage(rgb, new Point(5, 4)); } if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_3_" +rs+ ".bmp"); rgb = AutoBrightnessProcessor.autoBrightness(rgb, Rectangle.Empty, Constants.AUTOBRIGHTNESS_BASE_LUM); if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses) rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_4.bmp"); if (Constants.LABELING_TYPE == Constants.LabelingType.redBlackWhite) rgb = new ColorLabelFilter(new Color[] { Color.FromArgb(255, 0, 0), Color.White, Color.Black }, true).Apply(rgb); else if (Constants.LABELING_TYPE == Constants.LabelingType.blackAndWhite) { int dynamic_gray = 0; rgb = new ColorLabelFilter(new Color[] { Color.White, Color.FromArgb(dynamic_gray, dynamic_gray, dynamic_gray) }, true).Apply(rgb); } /* int start_x = 0, start_y = 0; int y = 0; for (; y < Constants.SIGN_HEIGHT && (start_x + start_y) == 0; y++) { for (int x = 0; x < Constants.SIGN_WIDTH; x++) { Color clr = rgb.GetPixel(x, y); if (clr.R == 0) { start_x = x; start_y = y; break; } } } PointedColorFloodFill filter = new PointedColorFloodFill(); filter.Tolerance = Color.FromArgb(0, 0, 0); filter.FillColor = Color.FromArgb(255, 255, 255); filter.StartingPoint = new Point(start_x, start_y); Bitmap rgb_flood_fill = filter.Apply(rgb); bool black_region_found_at_center = false; y = 4 * (Constants.SIGN_HEIGHT / 10); for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2) { Color clr = rgb_flood_fill.GetPixel(x, y); if (clr.R == 0) { black_region_found_at_center = true; } } if (!black_region_found_at_center) { y = 6 * (Constants.SIGN_HEIGHT / 10); for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2) { Color clr = rgb_flood_fill.GetPixel(x, y); if (clr.R == 0) { black_region_found_at_center = true; break; } } } if (black_region_found_at_center) { rgb = rgb_flood_fill; } */ BitmapData image_data = rgb.LockBits(new Rectangle(0, 0, rgb.Width, rgb.Height), ImageLockMode.ReadWrite, rgb.PixelFormat); int bpp = 3; int nOffset = image_data.Stride - rgb.Width * bpp; System.IntPtr Scan0 = image_data.Scan0; unsafe { byte* p = (byte*)Scan0; for (int y = 0; y < Constants.SIGN_HEIGHT; y++) { // for each pixel for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp) { if (y >= Constants.SIGN_HEIGHT - 10 || y <= 10 || x >= Constants.SIGN_WIDTH - 10 || x <= 10 || Math.Sqrt( (y - Constants.SIGN_HEIGHT / 2) * (y - Constants.SIGN_HEIGHT / 2) + (x - Constants.SIGN_WIDTH / 2) * (x - Constants.SIGN_WIDTH / 2)) > 24 ) { p[RGB.R] = 255; p[RGB.G] = 255; p[RGB.B] = 255; } else if (p[RGB.R] == 0) { com_x_sum += x; com_y_sum += y; com_x_y_point_count++; } } p += nOffset; } } rgb.UnlockBits(image_data); // Graphics g = Graphics.FromImage(rgb); // g.DrawEllipse(Pens.Green, 32 - 24, 32 - 24, 24 * 2, 24 * 2); if (com_x_y_point_count < 10) bestChromo = null; else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest) { if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestIntoFolder) { string[] signs = Globals.SIGN_IN_FRAME.Split(','); for (int j = 1; j < signs.Length; j++) { if (!Directory.Exists(Constants.base_folder + "hasat\\" + signs[j])) Directory.CreateDirectory(Constants.base_folder + "hasat\\" + signs[j]); lock (Globals.HARVEST_LOCK) { rgb.Save(Constants.base_folder + "hasat\\" + signs[j] + "\\" + Globals.HARVEST_SIGN_ID + ".bmp"); } } } else { rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_6.bmp"); } } gtcBmp = rgb; } } if (Constants.EVALUATE_TIME_ENABLED) { int x = (int)(DateTime.Now.Ticks / 10000 - start); if (x >= 0) { Globals.TIME_CIRCULAR_MIN = x < Globals.TIME_CIRCULAR_MIN ? x : Globals.TIME_CIRCULAR_MIN; Globals.TIME_CIRCULAR_MAX = x > Globals.TIME_CIRCULAR_MAX ? x : Globals.TIME_CIRCULAR_MAX; Globals.TIME_CIRCULAR_TOTAL += x; } } if (bestChromo != null) { if (panel != null) { Graphics g = panel.getPanelGraphics(); g.DrawRectangle(Pens.Silver, 5, 74, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT); g.DrawImage(gtcBmp, new Point(5, 74)); } if (msgService != null) { VisionMessage vm = new VisionMessage(gtcBmp.Height, gtcBmp.Width, ByteTools.pixelFormatToBPP(gtcBmp.PixelFormat), gtcBmp); // Center of Mass int com_x = com_x_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count); int com_y = com_y_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count); vm.CoM_X = com_x; vm.CoM_Y = com_y; msgService.sendMsg(vm); } } else { if (Globals.RIGHT_PANEL_SHOWING_STH) { Globals.RIGHT_PANEL_SHOWING_STH = false; // Bitmap img = new Bitmap(160, Constants.IMAGE_HEIGHT, PixelFormat.Format24bppRgb); Graphics g = panel.getPanelGraphics(); g.Clear(Color.Silver); /*if (msgService != null) { VisionMessage vm = new VisionMessage(img.Height, img.Width, ByteTools.pixelFormatToBPP(img.PixelFormat), img); vm.bypass = true; msgService.sendMsg(vm); }*/ } } threadRunning = false; }
public SVM_SURFProcessor(MsgService msgService, Constants.SignType signType) : base(msgService, VisionMessage.msgType) { this.signType = signType; if (signs == null) { signs = new Bitmap[Constants.NUM_OF_SIGN_TYPES + 1]; ResizeBicubic resizer = new ResizeBicubic(32, 32); for (int i = 1; i <= Constants.NUM_OF_SIGN_TYPES; i++) { String file_name = "signs\\sign_" + (i < 10 ? ("0" + i) : ("" + i)) + ".bmp"; signs[i] = resizer.Apply((Bitmap)Bitmap.FromFile(Constants.base_folder + file_name, false)); } } if (model == null) { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(Constants.base_folder + Constants.NN_SVM_SURF + "_" + (signType == Constants.SignType.circular ? "circle" : "triangle") + ".dat", FileMode.Open, FileAccess.Read, FileShare.None); model = (Model)formatter.Deserialize(stream); stream.Close(); } }
/// <summary> /// Process the filter on the specified image. /// </summary> /// /// <param name="image">Source image data.</param> /// protected override unsafe void ProcessFilter( UnmanagedImage image ) { UnmanagedImage bgImage = null; BitmapData bgLockedData = null; // get image size int width = image.Width; int height = image.Height; int offset = image.Stride - ( ( image.PixelFormat == PixelFormat.Format8bppIndexed ) ? width : width * 3 ); // check if we have provided background if ( ( backgroundImage == null ) && ( unmanagedBackgroundImage == null ) ) { // resize image to 1/3 of its original size to make bluring faster ResizeBicubic resizeFilter = new ResizeBicubic( (int) width / 3, (int) height / 3 ); UnmanagedImage tempImage = resizeFilter.Apply( image ); // create background image from the input image blurring it with Gaussian 5 times GaussianBlur blur = new GaussianBlur( 5, 21 ); blur.ApplyInPlace( tempImage ); blur.ApplyInPlace( tempImage ); blur.ApplyInPlace( tempImage ); blur.ApplyInPlace( tempImage ); blur.ApplyInPlace( tempImage ); // resize the blurred image back to original size resizeFilter.NewWidth = width; resizeFilter.NewHeight = height; bgImage = resizeFilter.Apply( tempImage ); tempImage.Dispose( ); } else { if ( backgroundImage != null ) { // check background image if ( ( width != backgroundImage.Width ) || ( height != backgroundImage.Height ) || ( image.PixelFormat != backgroundImage.PixelFormat ) ) { throw new InvalidImagePropertiesException( "Source image and background images must have the same size and pixel format" ); } // lock background image bgLockedData = backgroundImage.LockBits( new Rectangle( 0, 0, width, height ), ImageLockMode.ReadOnly, backgroundImage.PixelFormat ); bgImage = new UnmanagedImage( bgLockedData ); } else { bgImage = unmanagedBackgroundImage; } } // get background image's statistics (mean value is used as correction factor) ImageStatistics bgStatistics = new ImageStatistics( bgImage ); byte* src = (byte*) image.ImageData.ToPointer( ); byte* bg = (byte*) bgImage.ImageData.ToPointer( ); // do the job if ( image.PixelFormat == PixelFormat.Format8bppIndexed ) { // grayscale image double mean = bgStatistics.Gray.Mean; for ( int y = 0; y < height; y++ ) { for ( int x = 0; x < width; x++, src++, bg++ ) { if ( *bg != 0 ) { *src = (byte) Math.Min( mean * *src / *bg, 255 ); } } src += offset; bg += offset; } } else { // color image double meanR = bgStatistics.Red.Mean; double meanG = bgStatistics.Green.Mean; double meanB = bgStatistics.Blue.Mean; for ( int y = 0; y < height; y++ ) { for ( int x = 0; x < width; x++, src += 3, bg += 3 ) { // red if ( bg[RGB.R] != 0 ) { src[RGB.R] = (byte) Math.Min( meanR * src[RGB.R] / bg[RGB.R], 255 ); } // green if ( bg[RGB.G] != 0 ) { src[RGB.G] = (byte) Math.Min( meanG * src[RGB.G] / bg[RGB.G], 255 ); } // blue if ( bg[RGB.B] != 0 ) { src[RGB.B] = (byte) Math.Min( meanB * src[RGB.B] / bg[RGB.B], 255 ); } } src += offset; bg += offset; } } if ( backgroundImage != null ) { backgroundImage.UnlockBits( bgLockedData ); } // dispose background image if it was not set manually if ( ( backgroundImage == null ) && ( unmanagedBackgroundImage == null ) ) { bgImage.Dispose( ); } }