public static Bitmap MakeGrayscale(Bitmap original) { using (var gr = Graphics.FromImage(original)) { //var grayMatrix = new[] // { // new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, // new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, // new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, // new float[] { 0, 0, 0, 1, 0 }, // new float[] { 0, 0, 0, 0, 1 } // }; var grayMatrix = new[] { new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { -1, -1, -1, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(grayMatrix)); ia.SetThreshold(0.7f); // Change this threshold as needed var rc = new Rectangle(0, 0, original.Width, original.Height); gr.DrawImage(original, rc, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia); } return original; }
public static Bitmap MakeGrayscale(Bitmap original) { using (var gr = Graphics.FromImage(original)) { //var grayMatrix = new[] // { // new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, // new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, // new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, // new float[] { 0, 0, 0, 1, 0 }, // new float[] { 0, 0, 0, 0, 1 } // }; var grayMatrix = new[] { new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 1.5f, 1.5f, 1.5f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { -1, -1, -1, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(grayMatrix)); ia.SetThreshold(0.7f); // Change this threshold as needed var rc = new Rectangle(0, 0, original.Width, original.Height); gr.DrawImage(original, rc, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia); } return(original); }
private static string MakeImageBlackNWhite(string image_path, float threshold) { Bitmap SourceImage = new Bitmap(image_path); using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(threshold); // Change this threshold as needed var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height); gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia); } string bw_path = "C:/Users/Bruce Huffa/source/repos/DevelopOCR/DevelopOCR/test_bw.png"; SourceImage.Save(bw_path); return(bw_path); }
private static Image ConvertColorMatrix(Image SourceImage, float[][] ColorMatrix, float Threshold) { Bitmap work = new Bitmap(SourceImage); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(work)) { var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(bw_matrix)); ia.SetThreshold(Threshold); // Change this threshold as needed var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height); gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia); } return(work); }
private void btn_prikaziSliku_Click(object sender, EventArgs e) { //otvara se dialog openFile i izabere se slika (*.bmp|*.jpg|*.png) i prikaze se u PictureBoxu pb_slika Stream stream = null; openFile.InitialDirectory = "C:\\"; openFile.Filter = "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg)|*.jpg; *.jpeg"; openFile.FilterIndex = 3; openFile.RestoreDirectory = true; openFile.Title = "Odaberite sliku za prikaz na displeju."; if (openFile.ShowDialog() == DialogResult.OK) { try { if ((stream = openFile.OpenFile()) != null) { Bitmap slika = new Bitmap(openFile.FileName); Bitmap smanjenaSlika = Konverzije.Resize(slika); using (Graphics gr = Graphics.FromImage(smanjenaSlika)) { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.7f); // Change this threshold as needed var rc = new Rectangle(0, 0, smanjenaSlika.Width, smanjenaSlika.Height); gr.DrawImage(smanjenaSlika, rc, 0, 0, smanjenaSlika.Width, smanjenaSlika.Height, GraphicsUnit.Pixel, ia); } niz = Konverzije.ConvertToByteArray(smanjenaSlika); var image = Konverzije.ConvertToBitmap(niz); pb_slika.Image = image; toolStripStatusLabel1.Text = "Uspješno učitana slika."; btn_posaljiSliku.Enabled = true; } } catch (Exception ex) { toolStripStatusLabel1.Text = "Greska: nije moguce učitati datoteku. [" + ex.Message + "]"; } } }
public static Image BlackWhite(this Image Image, float threshold) { Bitmap SourceImage = new Bitmap(Image); using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(threshold); // Change this threshold as needed var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height); gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia); } return(SourceImage); }
public void Transform(IModel app) { Bitmap bitmap = app.Image; using (Graphics gr = Graphics.FromImage(bitmap)) { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.8f); var rc = new Rectangle(0, 0, bitmap.Width, bitmap.Height); gr.DrawImage(bitmap, rc, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, ia); } app.Image = bitmap; }
private void panelPreview_Paint(object sender, PaintEventArgs e) { if (_preview == null) { e.Graphics.Clear(Color.FromKnownColor(KnownColor.AppWorkspace)); return; } using (Graphics g = Graphics.FromImage(_preview)) { g.Clear(Color.FromKnownColor(KnownColor.AppWorkspace)); var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); // ia.SetThreshold(0.2f); // Change this threshold as needed float t = sliderAlpha.Value / 100f; ia.SetThreshold(t); // Change this threshold as needed g.DrawImage( _selection, new Rectangle(0, 0, (int)txtWidth.Value, (int)txtHeight.Value), _topX, _topY, (int)txtWidth.Value, (int)txtHeight.Value, GraphicsUnit.Pixel, ia); } //e.Graphics.Clear(Color.FromKnownColor(KnownColor.AppWorkspace)); //if (_source == null) // return; //var gray_matrix = new float[][] { // new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, // new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, // new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, // new float[] { 0, 0, 0, 1, 0 }, // new float[] { 0, 0, 0, 0, 1 } //}; //var ia = new System.Drawing.Imaging.ImageAttributes(); //ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); //// ia.SetThreshold(0.2f); // Change this threshold as needed //float t = sliderAlpha.Value / 100f; //ia.SetThreshold(t); // Change this threshold as needed //e.Graphics.Clear(Color.Black); //e.Graphics.DrawImage( // _selection, // new Rectangle(0, 0, (int)txtWidth.Value, (int)txtHeight.Value), // _topX, // _topY, // (int)txtWidth.Value, // (int)txtHeight.Value, // GraphicsUnit.Pixel, // ia); }