/// <summary> /// Binarize a given bitmap based on the given threshold value. /// First the bitmap is converted to a grayscale, then there is a binarization based on the threshold value /// and finally there is an optional mirror filter. /// </summary> /// <param name="bmp">Bitmap to binarize</param> /// <param name="treshold">Threshold value (0 - 255)</param> /// <param name="mirrorChecked">If true, the image will be mirrored</param> /// <returns>Binarized with threshold value bitmap</returns> public static Bitmap imageBinarization(Bitmap bmp, int threshold, bool mirrorChecked) { // Create bitmap with grayScale pixelformat Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // Create grayscale filter (BT709) and apply filter Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); bitmap = filter.Apply(bmp); // Create Threshold (binarization) filter and apply Threshold filter1 = new Threshold(threshold); filter1.ApplyInPlace(bitmap); // Create mirror filter if checkbox was checked if (mirrorChecked == true) { Mirror filter2 = new Mirror(false, true); filter2.ApplyInPlace(bitmap); } // Return binarized bitmap return(bitmap); }
public static bool HasData(OmrPageOutput page, OmrBarcodeField barcode) { String tPath = Path.GetTempFileName(); using (Bitmap bmp = new Bitmap((int)barcode.TopRight.X - (int)barcode.TopLeft.X, (int)barcode.BottomLeft.Y - (int)barcode.TopLeft.Y, PixelFormat.Format24bppRgb)) { using (Graphics g = Graphics.FromImage(bmp)) using (System.Drawing.Image img = System.Drawing.Image.FromFile(page.AnalyzedImage)) { g.DrawImage(img, 0, 0, new Rectangle(new Point((int)barcode.TopLeft.X, (int)barcode.TopLeft.Y), bmp.Size), GraphicsUnit.Pixel); // is g empty? } // Blobs BlobCounter blobCounter = new BlobCounter(); blobCounter.MinHeight = 30; blobCounter.MinWidth = 30; blobCounter.FilterBlobs = true; //blobCounter.BackgroundThreshold = Color.Gray;// new Color(255, 255, 255); using (var grayscale = new GrayscaleY().Apply(bmp)) { Threshold binaryThreshold = new Threshold(page.Template.ScanThreshold); binaryThreshold.ApplyInPlace(grayscale); new Invert().ApplyInPlace(grayscale); // Check for circles blobCounter.ProcessImage(grayscale); Blob[] blobs = blobCounter.GetObjectsInformation(); return(blobs.Where(o => o.ColorMean == Color.FromArgb(255, 255, 255, 255)).Count() > 0); } } }
/// <summary> /// 处理(当前)画面 /// </summary> /// <param name="image"></param> public void ProcessFrame(ref Bitmap image) { if (backgroundFrame == null) { backgroundFrame = GrayscaleFilter.Apply(image); // create initial backgroung image // get image dimension width = image.Width; height = image.Height; // just return for the first time return; } Bitmap tmpImage = GrayscaleFilter.Apply(image); //灰度过滤 // set backgroud frame as an overlay for difference filter DifferenceFilter.OverlayImage = backgroundFrame; Bitmap tmpImage2 = DifferenceFilter.Apply(tmpImage); //差异过滤 // lock the temporary image and apply some filters on the locked data bitmapData = tmpImage2.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // threshold filter ThresholdFilter.ApplyInPlace(bitmapData); //阈值过滤 // erosion filter Bitmap tmpImage3 = ErosionFilter.Apply(bitmapData); //腐蚀过滤 // unlock temporary image tmpImage2.UnlockBits(bitmapData); tmpImage2.Dispose(); // calculate amount of changed pixels pixelsChanged = (calculateMotionLevel) ? CalculateWhitePixels(tmpImage3) : 0; // dispose old background backgroundFrame.Dispose(); // set backgound to current backgroundFrame = tmpImage; // extract red channel from the original image Bitmap redChannel = extrachChannel.Apply(image); //提取通道 // merge red channel with moving object MergeFilter.OverlayImage = tmpImage3; Bitmap tmpImage4 = MergeFilter.Apply(redChannel); //合并过滤 redChannel.Dispose(); tmpImage3.Dispose(); // replace red channel in the original image replaceChannel.ChannelImage = tmpImage4; Bitmap tmpImage5 = replaceChannel.Apply(image); //替换通道 tmpImage4.Dispose(); image.Dispose(); image = tmpImage5; }
/// <summary> /// Creates a comic rendered copy of the input image. /// </summary> public override Bitmap Render(Bitmap sourceImage) { // Converters GrayscaleY convertGray = new GrayscaleY(); GrayscaleToRGB convertColor = new GrayscaleToRGB(); // Convert grayscal images if (sourceImage.PixelFormat == PixelFormat.Format8bppIndexed) { sourceImage = convertColor.Apply(sourceImage); } Bitmap comicImage = AForge.Imaging.Image.Clone(sourceImage); Bitmap edgeLayer = null; Bitmap glowLayer = null; // Glow for smooth colors GaussianBlur filterBlur = new GaussianBlur(); filterBlur.Sigma = 2.0; filterBlur.Size = 4; glowLayer = filterBlur.Apply(comicImage); //SmartBlur filterBlur = new SmartBlur(10, 0.2); //glowLayer = filterBlur.Apply(comicImage); ContrastCorrection filterContrast = new ContrastCorrection(1 - (-this.Coloring * 0.1)); filterContrast.ApplyInPlace(glowLayer); BrightnessCorrection filterBrightness = new BrightnessCorrection((-this.Coloring * 0.1) + 0.1); filterBrightness.ApplyInPlace(glowLayer); Screen blendScreen = new Screen(glowLayer); blendScreen.ApplyInPlace(comicImage); // Create a layer for edges Convolution filterConvolution = new Convolution(ConvolutionKernel); edgeLayer = filterConvolution.Apply(comicImage); // Convert to grayscale edgeLayer = convertGray.Apply(edgeLayer); // Threshold (edge thickness) Threshold filterThreshold = new Threshold((byte)(this.Edging * 255 / 100)); filterThreshold.ApplyInPlace(edgeLayer); edgeLayer = convertColor.Apply(edgeLayer); // intersect comic with top layer (Darken blend) Intersect blendIntersect = new Intersect(edgeLayer); blendIntersect.ApplyInPlace(comicImage); return(comicImage); }
public Bitmap GreyscaleEdgeDetectionImage(Bitmap bitmap) { // Greyscale var greyscaleImage = Grayscale.CommonAlgorithms.BT709.Apply(bitmap); // Contrast - try to sharpen edges //ContrastStretch filter = new ContrastStretch(); //filter.ApplyInPlace(filteredBitmap); // edge filter // This filters accepts 8 bpp grayscale images for processing //Alternatives: //DifferenceEdgeDetector edgeFilter = new DifferenceEdgeDetector(); //HomogenityEdgeDetector filter = new HomogenityEdgeDetector(); //CannyEdgeDetector filter = new CannyEdgeDetector( ); var edgeFilter = new SobelEdgeDetector(); edgeFilter.ApplyInPlace(greyscaleImage); var threshholdFilter = new Threshold(240); //180 threshholdFilter.ApplyInPlace(greyscaleImage); return(greyscaleImage); }
private void DecodeBarcode() { var reader = new BarcodeReader(); while (true) { if (currentBitmapForDecoding != null) { currentBitmapForDecoding = gsFilter.Apply(currentBitmapForDecoding); if (cbContrast.Checked) { csFilter.ApplyInPlace(currentBitmapForDecoding); } if (cbHistogram.Checked) { heFilter.ApplyInPlace(currentBitmapForDecoding); } if (btnThreshold.BackColor == Color.Green) { tsFilter.ApplyInPlace(currentBitmapForDecoding); } Invoke(new Action <Bitmap>(ShowFrame), currentBitmapForDecoding.Clone()); var result = reader.Decode(currentBitmapForDecoding); if (result != null) { Invoke(new Action <Result>(ShowResult), result); } currentBitmapForDecoding.Dispose(); currentBitmapForDecoding = null; } Thread.Sleep(sleep); } }
/// <summary> /// This method is preparing Learning Matrix basis on image from LearningPictures /// </summary> /// <returns></returns> private double[][] CreateLearningMatrix() { //loading and cuting learning matrix for each character GraphicProcessing graph = new GraphicProcessing(); Bitmap mBitmap; System.Drawing.Image img = histogramAforge.Properties.Resources.learningAll; mBitmap = new Bitmap(img); Grayscale greyScaleFilter = new Grayscale(1.0, 0.0, 0.0); mBitmap = greyScaleFilter.Apply(mBitmap); Threshold tresholdFilter = new Threshold(120); tresholdFilter.ApplyInPlace(mBitmap); List <Bitmap> listOfCharacters = graph.ProcesLearningImage(mBitmap); //crate input matrix for each character to learn NN double[][] learnigTable = new double[listOfCharacters.Count][]; int i = 0; foreach (Bitmap map in listOfCharacters) { learnigTable[i] = CreteInputMatrix(map); i++; } return(learnigTable); }
/// <summary> /// Apply filter /// </summary> /// <returns>Bitmap - filtered image</returns> public Bitmap Apply() { try { bitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap); var normalizer = new ContrastStretch(); normalizer.ApplyInPlace(bitmap); var er = new Erosion(); er.ApplyInPlace(bitmap); var ed = new SobelEdgeDetector(); ed.ApplyInPlace(bitmap); var th = new Threshold(); th.ApplyInPlace(bitmap); return(bitmap); } catch { return(bitmap); } }
public IScanImage Scan(ISource source, ScannerOptions options) { var image = new Bitmap(source.Take()); // create grayscale filter (BT709) var filter = new Grayscale(0.2125, 0.7154, 0.0721); var mBinarized = filter.Apply(image); // Binarize Picture. var bin = new Threshold(options.Threshold); bin.ApplyInPlace(mBinarized); // create filter var inv = new Invert(); inv.ApplyInPlace(mBinarized); // create an instance of blob counter algorithm var bc = new BlobCounter { ObjectsOrder = ObjectsOrder.XY }; bc.ProcessImage(mBinarized); var blobsRect = bc.GetObjectsRectangles(); var b = Order(blobsRect); return(new ScanImage(image, b)); }
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBox2.SelectedIndex) { case 0: selected = redFilter; break; case 1: selected = greenFilter; break; case 2: selected = blueFilter; break; default: break; } Bitmap channel = selected.Apply((Bitmap)pictureBox1.Image); thresholdFilter = new Threshold(Int32.Parse(textBox1.Text)); thresholdFilter.ApplyInPlace(channel); pictureBox2.Image = channel; }
/// <summary> /// Transforms a grayscale image into a black and white one /// </summary> /// <param name="bitmap">Bitmap needs to be in grayscale!</param> /// <returns></returns> private Bitmap GetBlackAndWhiteBitmap(Bitmap bitmap, out Bitmap original) { Threshold filter = new Threshold(Threshold); original = new Bitmap(bitmap); filter.ApplyInPlace(bitmap); return(bitmap); }
/* * 2- binarization (Otsu's method is the most referenced technique); * (i don't know, yet, if i can apply binarization in a color img or if i must do grayscale first) * */ public void ToBlackWhite(ref Bitmap image) { // create filter Threshold filter = new Threshold(100); // apply the filter filter.ApplyInPlace(image); }
public static Bitmap Threshold(Bitmap image, int limit) { Threshold th; th = new Threshold(limit); th.ApplyInPlace(image); return(image); }
public Bitmap SetToBW(Bitmap bmp, int ithreshold) { // create filter Threshold filter = new Threshold(ithreshold); // apply the filter filter.ApplyInPlace(bmp); return(bmp); }
private Bitmap ProcessImage(Bitmap frame) { // convert the image to grayscale var grayConverter = new GrayscaleBT709(); var grayFrame = grayConverter.Apply(frame); // use a sobel edge detector to find color edges var edgeDetector = new SobelEdgeDetector(); var edgeFrame = edgeDetector.Apply(grayFrame); // threshold the edges var thresholdConverter = new Threshold(200); thresholdConverter.ApplyInPlace(edgeFrame); // use a blobcounter to find interesting shapes var detector = new BlobCounter() { FilterBlobs = true, MinWidth = 25, MinHeight = 25 }; detector.ProcessImage(edgeFrame); // find the circular shape var shapeDetector = new SimpleShapeChecker(); var blobs = detector.GetObjectsInformation(); var circles = from blob in blobs let edgePoints = detector.GetBlobsEdgePoints(blob) where shapeDetector.CheckShapeType(edgePoints) == ShapeType.Circle select blob; // show the traffic sign if (circles.Count() > 0) { var circleFrame = frame.Clone(circles.First().Rectangle, PixelFormat.DontCare); trafficSignBox.Image = circleFrame; } // highlight every circle in the image using (Graphics g = Graphics.FromImage(frame)) { var rects = detector.GetObjectsRectangles(); var pen = new Pen(Color.Blue, 4); foreach (var circle in circles) { g.DrawRectangle(pen, circle.Rectangle); } } // update picture boxes thresholdBox.Image = edgeFrame; return(frame); }
private Bitmap thresholdImage(Bitmap image, int thresholdVal) { Threshold thrshold = new Threshold(); thrshold.ThresholdValue = thresholdVal; thrshold.ApplyInPlace(image); return(image); }
private void button1_Click(object sender, EventArgs e) { double width = Math.Floor(img1.Width / img1.HorizontalResolution); double height = Math.Floor(img1.Height / img1.VerticalResolution); label1.Text = img1.HorizontalResolution + " " + img1.VerticalResolution; img1 = Grayscale.CommonAlgorithms.BT709.Apply(img1); Threshold filter = new Threshold(150); filter.ApplyInPlace(img1); DocumentSkewChecker skewChecker = new DocumentSkewChecker(); double angle = skewChecker.GetSkewAngle(img1); RotateBilinear rotationFilter = new RotateBilinear(-angle); rotationFilter.FillColor = Color.White; img1 = rotationFilter.Apply(img1); pictureBox1.Image = img1; TesseractEngine engine = new TesseractEngine(@"C:\Users\moham\source\repos\pfe3\tessdata\", "fra", EngineMode.Default); if (((width == 3) && (height == 2)) || (width == 2 && height == 3)) { if ((width == 2 && height == 3)) { rotationFilter = new RotateBilinear(-90); rotationFilter.FillColor = Color.White; img1 = rotationFilter.Apply(img1); Console.WriteLine("if1"); } string ocrText = engine.Process(img1, new Rect(700, 450, 150, 100)).GetText(); engine.Dispose(); ocrText = ocrText.Replace(" ", " "); Console.WriteLine(ocrText + " " + ocrText.Length); if (ocrText.Length <= 9) { label1.Text = "carte national " + ocrText; } else { rotationFilter = new RotateBilinear(180); rotationFilter.FillColor = Color.White; img1 = rotationFilter.Apply(img1); engine = new TesseractEngine(@"C:\Users\moham\source\repos\pfe3\tessdata\", "fra", EngineMode.Default); ocrText = engine.Process(img1, new Rect(700, 450, 150, 100)).GetText(); engine.Dispose(); ocrText = ocrText.Replace(" ", ""); } label1.Text = "carte national " + ocrText; pictureBox1.Image = img1; engine.Dispose(); } else if ((width == 8 && height == 11) || (width == 11 && height == 8)) { } else { } }
private void ThresholdImg(ref Bitmap frame) { frame = Grayscale.CommonAlgorithms.RMY.Apply(frame); Threshold filter = new Threshold(ThresholdValue); filter.ApplyInPlace(frame); GrayscaleToRGB toColFilter = new GrayscaleToRGB(); frame = toColFilter.Apply(frame); }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { pictureBox1.Image = new Bitmap(Bitmap.FromFile("C:\\testimg\\" + comboBox1.SelectedIndex + ".png"), pictureBox1.Size); Bitmap channel = selected.Apply((Bitmap)pictureBox1.Image); thresholdFilter = new Threshold(Int32.Parse(textBox1.Text)); thresholdFilter.ApplyInPlace(channel); pictureBox2.Image = channel; pictureBox3.Image = brownFilter.Apply((Bitmap)pictureBox1.Image); }
public Bitmap ProcessImage(Bitmap bmp) { Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap grayimage = filter.Apply(bmp); Threshold th = new Threshold(); th.ApplyInPlace(grayimage); return(grayimage); }
public Bitmap WatershedTest(Bitmap image) { Threshold threshold = new Threshold(150); threshold.ApplyInPlace(image); return(image); }
// ========================================================= private void ThresholdFunct(ref Bitmap frame, double par_d) { try { frame = Grayscale.CommonAlgorithms.RMY.Apply(frame); Threshold filter = new Threshold((int)par_d); filter.ApplyInPlace(frame); GrayscaleToRGB toColFilter = new GrayscaleToRGB(); frame = toColFilter.Apply(frame); } catch { } }
void kamera_ProsesFrame(object sender, NewFrameEventArgs eventArgs) { frame = (Bitmap)eventArgs.Frame.Clone(); Grayscale gs = new Grayscale(0.2125, 0.7154, 0.0721); Threshold th = new Threshold(100); Bitmap img = gs.Apply(frame); th.ApplyInPlace(img); pictureBox1.Image = frame; pictureBox2.Image = img; }
public static Bitmap Sketch(Bitmap input, int redThreshold, int blueThreshold, int greenThreshold) { int[,] kernel = { { 1, 2, 1, }, { 2, 4, 2, }, { 1, 2, 1, }, }; Convolution convultionFilters = new Convolution(kernel); Threshold thresholdFilter = new Threshold(redThreshold); input = convultionFilters.Apply(input); // extract red channel ExtractChannel extractFilter = new ExtractChannel(RGB.R); Bitmap channel = extractFilter.Apply(input); // threshold channel thresholdFilter.ApplyInPlace(channel); // put the channel back ReplaceChannel replaceFilter = new ReplaceChannel(RGB.R, channel); replaceFilter.ApplyInPlace(input); // extract blue channel extractFilter = new ExtractChannel(RGB.B); channel = extractFilter.Apply(input); // threshold channel thresholdFilter.ThresholdValue = blueThreshold; thresholdFilter.ApplyInPlace(channel); // put the channel back replaceFilter = new ReplaceChannel(RGB.B, channel); replaceFilter.ApplyInPlace(input); // extract green channel extractFilter = new ExtractChannel(RGB.G); channel = extractFilter.Apply(input); // threshold channel thresholdFilter.ThresholdValue = greenThreshold; thresholdFilter.ApplyInPlace(channel); // put the channel back replaceFilter = new ReplaceChannel(RGB.G, channel); replaceFilter.ApplyInPlace(input); return input; }
/// <summary> /// Filter: Threshold (Binary) /// Binarize the image (threshold) /// </summary> public void ToBinary() { var filter = new Threshold(230); // create filter filter.ApplyInPlace(Image); // apply the filter Save("binarized"); /* * Note: Since the filter can be applied as to 8 bpp and to 16 bpp images, the Threshold Value value * should be set appropriately to the pixel format. In the case of 8 bpp images the threshold value * is in the [0, 255] range, but in the case of 16 bpp images the threshold value is in the [0, 65535] range. */ }
int MotionDetection(Bitmap CurrentFrame) { if (PreFrame == null) { PreFrame = GrayFilter.Apply(CurrentFrame); PixFilter.ApplyInPlace(PreFrame); return(0); } Bitmap ProcFrame = GrayFilter.Apply(CurrentFrame); PixFilter.ApplyInPlace(ProcFrame); MoveTowardsFilter.StepSize = 1; MoveTowardsFilter.OverlayImage = ProcFrame; MoveTowardsFilter.ApplyInPlace(PreFrame); DiffFilter.OverlayImage = PreFrame; Bitmap DiffImage = DiffFilter.Apply(ProcFrame); ThreFilter.ApplyInPlace(DiffImage); BlobsFilter.ProcessImage(DiffImage); Blob[] Blobs = BlobsFilter.GetObjectsInformation(); int ret = 0; if (Blobs.Length != 0) { Graphics graph = Graphics.FromImage(CurrentFrame); Pen pen = new Pen(Color.Yellow, 2); graph.DrawRectangle(pen, new Rectangle(3, 3, 10, 10)); foreach (Blob item in Blobs) { if (item.Area > blobArea) { if (ShowMotionCheckBox.Checked) { graph.DrawRectangle(pen, item.Rectangle); } ++ret; } } } videoShowPicBox.Image = CurrentFrame; PreFrame = ProcFrame; return(ret); }
public static Bitmap PixelDiff(Bitmap a, Bitmap b) { var difference = new Difference(a); var dif = difference.Apply(b); gaussianBlur.ApplyInPlace(dif); Bitmap clone = Grayscale.CommonAlgorithms.Y.Apply(dif); threshold.ApplyInPlace(clone); fillHoles.ApplyInPlace(clone); return(clone); }
private void fillHoleToolStripMenuItem_Click(object sender, EventArgs e) { GrayscaleBT709 greyscale = new GrayscaleBT709(); Bitmap grey = greyscale.Apply(skin); Threshold filter = new Threshold(100); filter.ApplyInPlace(grey); Closing close = new Closing(); Bitmap j = close.Apply(grey); Opening open = new Opening(); k = open.Apply(j); pictureBox3.Image = k; }
private void otsuToolStripMenuItem_Click(object sender, EventArgs e) { OtsuThreshold otsuFilter = new OtsuThreshold(); //Фильтр, котор Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap srcBitmap = new Bitmap(sourcePictureBox.Image); Bitmap grayImage = grayFilter.Apply(srcBitmap); int threshold = otsuFilter.CalculateThreshold(grayImage, new Rectangle(0, 0, grayImage.Width, grayImage.Height)) + trackBar1.Value; int delta = 10; Threshold filter = new Threshold(threshold - delta); filter.ApplyInPlace(grayImage); resultPictureBox.Image = grayImage; chart1.Series[0].Points.DataBindY(calculateChart(grayImage)); }
private void ProcessImage() { m_selectedBlobs.Clear(); pictureBox1.Controls.Clear(); pictureBox1.Image = null; if (m_original != null) { m_original.Dispose(); } if (m_binarized != null) { m_binarized.Dispose(); } m_original = new Bitmap(txtFile.Text); // create grayscale filter (BT709) Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); m_binarized = filter.Apply(m_original); // Binarize Picture. Threshold bin = new Threshold((int)txtBinThershold.Value); bin.ApplyInPlace(m_binarized); // create filter Invert inv = new Invert(); inv.ApplyInPlace(m_binarized); // create an instance of blob counter algorithm BlobCounter bc = new BlobCounter(); bc.ObjectsOrder = ObjectsOrder.XY; bc.ProcessImage(m_binarized); Rectangle[] blobsRect = bc.GetObjectsRectangles(); Dictionary <int, List <Rectangle> > orderedBlobs = ReorderBlobs(blobsRect); foreach (KeyValuePair <int, List <Rectangle> > orderedBlob in orderedBlobs) { orderedBlob.Value.ForEach(r => AddBlobPanel(orderedBlob.Key, r)); } pictureBox1.Image = chkShowBinarize.Checked ? m_binarized : m_original; pictureBox1.Invalidate(); }