public static Bitmap Threshold(Bitmap original) { int width = original.Width; int height = original.Height; Bitmap bmp = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bmp)) { ImageAttributes threshold = new ImageAttributes(); threshold.SetThreshold(0.92f); //attributes.SetThreshold(0.08f); g.DrawImage(original, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, threshold); ImageAttributes invert = new ImageAttributes(); ColorMap[] map = new ColorMap[2]; map[0] = new ColorMap(); map[0].OldColor = Color.Black; map[0].NewColor = Color.White; map[1] = new ColorMap(); map[1].OldColor = Color.White; map[1].NewColor = Color.Black; invert.SetRemapTable(map); g.DrawImage(bmp, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, invert); } return bmp; }
public void BlackAndWhite() { using (Graphics gr = Graphics.FromImage(bmp)) // 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 } }; int width = bmp.Width; int height = bmp.Height; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold((float)0.8); // Change this threshold as needed var rc = new Rectangle(0, 0, bmp.Width, bmp.Height); gr.DrawImage(bmp, rc, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia); Bitmap b = new Bitmap(width, height, gr); OnImageFinished(b); } }
public void PrintScreenThreshold() { //Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); //System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(printscreen as Image); //graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); Bitmap printscreen = new Bitmap(@"test\cap2.jpg"); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(printscreen)) { 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((float)0.50); // Change this threshold as needed (old .33) var rc = new Rectangle(0, 0, printscreen.Width, printscreen.Height); gr.DrawImage(printscreen, rc, 0, 0, printscreen.Width, printscreen.Height, GraphicsUnit.Pixel, ia); printscreen.Save("rewards.jpg", ImageFormat.Jpeg); } }
public static Bitmap Threshold(Image bitmap, int thresholdValue) { ColorMatrix grayscale = new ColorMatrix(new float[][]{ new float[]{ 0.30f, 0.30f, 0.30f, 0, 0}, new float[]{ 0.59f, 0.59f, 0.59f, 0, 0}, new float[]{ 0.11f, 0.11f, 0.11f, 0, 0}, new float[]{ 0, 0, 0, 1, 0}, new float[]{ 0, 0, 0, 0, 1}}); ImageAttributes attrib = new ImageAttributes(); attrib.SetColorMatrix(grayscale); attrib.SetThreshold(thresholdValue / 255.0f); Rectangle dimensions = new Rectangle(0,0, bitmap.Width, bitmap.Height); Bitmap output = new Bitmap(dimensions.Width, dimensions.Height, PixelFormat.Format32bppArgb); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(output)) { g.DrawImage(bitmap, dimensions, 0, 0, dimensions.Width, dimensions.Height, GraphicsUnit.Pixel, attrib); } return output; }
/* * public static Image CreateDisabledImage(Image srcImage) * { * if (srcImage == null) return null; * return ToolStripRenderer.CreateDisabledImage(srcImage); * } */ public static Image CreateBlackWhite(Image srcImage) { if (srcImage == null) { return(null); } var newImage = (Image)srcImage.Clone(); using (var gr = Graphics.FromImage(newImage)) // SourceImage is a Bitmap object { var gray_matrix = 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 ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.8f); // Change this threshold as needed var rc = new Rectangle(0, 0, newImage.Width, newImage.Height); gr.DrawImage(newImage, rc, 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia); } return(newImage); }
public void ConvertToBlackAndWhite(Bitmap sourceImage) { 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(0.8f); // 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); } }
public void ToGrayScale(Bitmap SourceImage) { 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(0.5F); // 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); } }
public void convertToBlackAndWhite(Bitmap input) { using (Graphics gr = Graphics.FromImage(input)) // 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(0.8f); // Change this threshold as needed var rc = new Rectangle(0, 0, input.Width, input.Height); gr.DrawImage(input, rc, 0, 0, input.Width, input.Height, GraphicsUnit.Pixel, ia); } }
static internal void MakeBW(Image sourceImage, float threshold = 0.8f) { 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); gr.Dispose(); } }
// remove color for better detection (probably don't need but beneficial to try out) public Bitmap RemoveColor(Bitmap source) { using (Graphics gr = Graphics.FromImage(source)) // 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((float)0.7); // Change this threshold as needed var rc = new Rectangle(0, 0, source.Width, source.Height); gr.DrawImage(source, rc, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); } return(source); }
/// <summary> /// Convierte el bitmap a blanco y negro utilzando un threshold específico. /// </summary> /// <param name="sourceImage">Imagen fuente</param> /// <param name="threshold">Threshold que se utilizara para determinar si un pixel es blanco o negro</param> public static Bitmap BitmapToMonochrome(Bitmap sourceImage, float threshold) { 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); 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 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 ia = new ImageAttributes(); ia.SetColorMatrix(new ColorMatrix(grayMatrix)); ia.SetThreshold(0.8f); // 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 bool CheckRunFail() { Bitmap sourceImage = new Bitmap(@"C:\Test\sampleDefeated.png"); 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(0.8f); // 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(false); }
// on applique un filtre noir et blanc sur l'image public Bitmap ToBlackAndwhite(Bitmap bmp) { Bitmap TempBmp = (Bitmap)bmp.Clone(); using (Graphics gr = Graphics.FromImage(TempBmp)) { var gray_matrix = new float[][] { new float[] { 0.399f, 0.399f, 0.399f, 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((float)1.8); var rc = new Rectangle(0, 0, TempBmp.Width, TempBmp.Height); gr.DrawImage(TempBmp, rc, 0, 0, TempBmp.Width, TempBmp.Height, GraphicsUnit.Pixel, ia); } return(TempBmp); }
public static Bitmap blackAndWhite(Bitmap originalbmp) { // var originalbmp = new Bitmap(Bitmap.FromFile(OFD.FileName)); // Load the image using (Graphics gr = Graphics.FromImage(originalbmp)) // 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(0.8f); // Change this threshold as needed var rc = new Rectangle(0, 0, originalbmp.Width, originalbmp.Height); gr.DrawImage(originalbmp, rc, 0, 0, originalbmp.Width, originalbmp.Height, GraphicsUnit.Pixel, ia); } return(originalbmp); }
public static Bitmap GetBlackAndWhiteImage(Bitmap SourceImage) { Bitmap bmp = new Bitmap(SourceImage.Width, SourceImage.Height); using (Graphics gr = Graphics.FromImage(bmp)) // 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(0.7f); // 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(bmp); }
public static void monochrome(Bitmap image) { using (Graphics g = Graphics.FromImage(image)) { 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 imageAttrib = new System.Drawing.Imaging.ImageAttributes(); imageAttrib.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); imageAttrib.SetThreshold((float)0.7); var rec = new Rectangle(0, 0, image.Width, image.Height); g.DrawImage(image, rec, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttrib); } }
public void createGreyValPic(string eingabe_in) { Color oldColor, greyColor; Bitmap helpMap, greyMap; int width, height, i, j, greyValue; //neue Instanz anlegen, sonst bekommt man eine Zugriffsverletzung helpMap = new Bitmap(bildPicturebox.Image); width = helpMap.Width; //Bildbreite bestimmen height = helpMap.Height; //Bildhöhe bestimmen greyMap = new Bitmap(width, height); //Bitmap für das Grauwertbild erstellen form1ProgressBar.Invoke(new Action(() => { form1ProgressBar.Maximum = height; form1ProgressBar.Visible = true; form1ProgressBar.Value = 0; })); for (j = 0; j < height; j++) //Schleife zum durchlaufen der Bitmap in der Breite { for (i = 0; i < width; i++) //Schleife zum durchlaufen der Bitmap in der Höhe { oldColor = helpMap.GetPixel(i, j); //Farbwert bestimmen greyValue = (int)(0.3 * oldColor.R + 0.6 * oldColor.G + 0.1 * oldColor.B); //Grauwert berechnen greyColor = Color.FromArgb(greyValue, greyValue, greyValue); //Colorvariable aus Grauwert erzeugen greyMap.SetPixel(i, j, greyColor); //Farbe(Grau) setzen } //falls die bearbeitung abgebrochen wird if (grauwertBW.CancellationPending) return; grauwertBW.ReportProgress(j); } //Berechnung des Schwarz-Weiß Bildes if (eingabe_in != "grau") { Graphics gr = Graphics.FromImage(greyMap); ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetThreshold(0.5f); // Change this threshold as needed Rectangle rc = new Rectangle(0, 0, width, height); gr.DrawImage(greyMap, rc, 0, 0, width, height, GraphicsUnit.Pixel, ia); } GC.Collect(); if (grauwertBW.CancellationPending) return; bildPicturebox.Invoke(new Action(() => { setAndSavePictureBox(greyMap); })); }
/// <summary> /// Threshold the image using the given value. /// </summary> /// <param name="source image ( will be overwritten with thresholded image)"></param> /// <param name="the threshold value ( 0..1)"></param> public static void ThresholdImage(Image source, float t) { // create image attributes to blit image into itself with. ImageAttributes ia = new ImageAttributes(); ia.SetThreshold(t); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.Dispose(); }
private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { // 리셋 case Keys.Space: Attr = new ImageAttributes(); Src = Image.FromFile("오솔길.jpg"); Threshold = 0.5f; Gamma = 1.0f; Bright = 0.0f; break; // 시계 방향 회전 case Keys.Right: Src.RotateFlip(RotateFlipType.Rotate90FlipNone); break; // 반시계 방향 회전 case Keys.Left: Src.RotateFlip(RotateFlipType.Rotate270FlipNone); break; // 수평 뒤집기 case Keys.Up: Src.RotateFlip(RotateFlipType.RotateNoneFlipX); break; // 수직 뒤집기 case Keys.Down: Src.RotateFlip(RotateFlipType.RotateNoneFlipY); break; // 스레시 홀드 증감 case Keys.Q: if (Threshold < 1) Threshold += 0.1f; Attr.SetThreshold(Threshold); break; case Keys.W: if (Threshold > 0) Threshold -= 0.1f; Attr.SetThreshold(Threshold); break; // 감마 증감 case Keys.E: if (Gamma < 5.0) Gamma += 0.1f; Attr.SetGamma(Gamma); break; case Keys.R: if (Gamma > 0.1) Gamma -= 0.1f; Attr.SetGamma(Gamma); break; // 밝게 case Keys.D1: if (Bright < 1.0f) Bright += 0.1f; float[][] M1 = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat1 = new ColorMatrix(M1); Attr.SetColorMatrix(Mat1); break; // 어둡게 case Keys.D2: if (Bright > -1.0f) Bright -= 0.1f; float[][] M2 = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat2 = new ColorMatrix(M2); Attr.SetColorMatrix(Mat2); break; // 반전 case Keys.D3: float[][] M3 = { new float[] {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, -1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, -1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {1.0f, 1.0f, 1.0f, 0.0f, 1.0f}, }; ColorMatrix Mat3 = new ColorMatrix(M3); Attr.SetColorMatrix(Mat3); break; // 그레이 스케일 case Keys.D4: float[][] M4 = { new float[] {0.299f, 0.299f, 0.299f, 0.0f, 0.0f}, new float[] {0.587f, 0.587f, 0.587f, 0.0f, 0.0f}, new float[] {0.114f, 0.114f, 0.114f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}, }; ColorMatrix Mat4 = new ColorMatrix(M4); Attr.SetColorMatrix(Mat4); break; case Keys.D5: ColorMap[] Map = new ColorMap[1]; Map[0] = new ColorMap(); Map[0].OldColor = Color.White; Map[0].NewColor = Color.Blue; Attr.SetRemapTable(Map); break; } Invalidate(); }
private Bitmap ChangeColour(Bitmap original, ColorMatrix colorMatrix, bool setThresholdForBitonal) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); // if the image is required is bitonal set a threshold if (setThresholdForBitonal) attributes.SetThreshold(0.75f); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; }
/// <summary> /// イメージを2値化する /// </summary> /// <param name="sourceImage"></param> /// <returns></returns> private Bitmap ChangeColor(Bitmap sourceImage) { //RGBの比率(YIQカラーモデル) const float r = 1.0F; const float g = 1.0F; const float b = 1.0F; //ColorMatrixにセットする行列を 5 * 5 の配列で用意 //入力のRGBの各チャンネルを重み付けをした上で、 //出力するRGBがR = G = B となるような行列をセット float[][] matrixElement = {new float[]{r, r, r, 0, 0}, new float[]{g, g, g, 0, 0}, new float[]{b, b, b, 0, 0}, new float[]{0, 0, 0, 1, 0}, new float[]{0, 0, 0, 0, 1}}; //ColorMatrixオブジェクトの作成 ColorMatrix matrix = new ColorMatrix(matrixElement); //ImageAttributesにセット ImageAttributes attr = new ImageAttributes(); attr.SetColorMatrix(matrix); //閾値の設定 attr.SetThreshold(0.5F); //50%を指定 int imageWidth = sourceImage.Width; int imageHeight = sourceImage.Height; //新しいビットマップを用意 Bitmap changedImage = new Bitmap(imageWidth, imageHeight); //新しいビットマップにImageAttributesを指定して //元のビットマップを描画 Graphics graph = Graphics.FromImage(changedImage); graph.DrawImage(sourceImage, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, imageWidth, imageHeight, GraphicsUnit.Pixel, attr); graph.Dispose(); return changedImage; }