What is the fastest way to convert image to black and black in C #?

Before someone cries to me for not checking other messages, I already did it. I have a specific question about one of the methods that exist for converting an image to grayscale.

I read other posts on SO and basically copied Technique 3 (ColorMatrix) from a tutorial on this site . It works very, very fast.

The problem I am facing is that I need a clean BW image. (i.e. if the average (R, B, G)> 200 => white is still black). I implemented this straightforwardly, and actually it takes almost 10 times until the grayscale algorithm from the tutorial.

I am not a specialist in image processing, and I was wondering if there is a way to change a fragment from a tutorial to convert images to black and white. If not, any effective way could do.

EDIT:

Here's the code I'm using for black and white (without approaching the brain):

public Bitmap make_bw(Bitmap original) { Bitmap output = new Bitmap(original.Width, original.Height); for (int i = 0; i < original.Width; i++) { for (int j = 0; j < original.Height; j++) { Color c = original.GetPixel(i, j); int average = ((cR + cB + cG) / 3); if (average < 200) output.SetPixel(i, j, Color.Black); else output.SetPixel(i, j, Color.White); } } return output; } 
+4
source share
3 answers
+3
source

This guy does three tests, and the fastest is ColorMatrix , as mentioned here. External Image Conversion Link

BUT one of the commentators claims that the reason that the direct and unsafe pixel access method is slower causes .Width and .Height in bitmap images and copying these values ​​to local variables makes direct pixel access much faster.

I tested this, and it seems that the unsafe method is about 2.5 times faster than when using ColorMatrix . But this is probably also more active, and obviously it requires unsafe code to be allowed in the project, which may be undesirable.

+2
source

I am using this simple code:

 public Bitmap GrayScaleFilter(Bitmap image) { Bitmap grayScale = new Bitmap(image.Width, image.Height); for (Int32 y = 0; y < grayScale.Height; y++) for (Int32 x = 0; x < grayScale.Width; x++) { Color c = image.GetPixel(x, y); Int32 gs = (Int32)(cR * 0.3 + cG * 0.59 + cB * 0.11); grayScale.SetPixel(x, y, Color.FromArgb(gs, gs, gs)); } return grayScale; } 
+2
source

Source: https://habr.com/ru/post/1339781/


All Articles