Convert image to black and white or sepia in C #

I want to change the image to Black-White or Sepia. After converting the image, I want to replace the existing image with the converted image.

Please give me some suggestion.

+5
source share
6 answers

There are many ways to bleach a color image. In fact, there is probably not a single “true” or “right” way to do this, although some methods are more “correct” than others.
I assume your image is in RGB (Red-Green-Blue) format (although BGR is also common).

The easiest way that should work for most photos (but nonetheless for synthetic images) is to simply use the green channel of 3 RGB channels. People are most sensitive to changes in the green part of the spectrum, so the green channel covers most of the visible range and is a good approximation to the grayscale image you want.

The best way to generate grayscale images is to use a weighted average for 3 RGB channels. Choosing equal weights (0.33 * R + 0.33 * G + 0.33 * B) will give a pretty good grayscale image. Other convex weights (non-negative weights, the sum of which is 1) will give different results, some of which can be considered more aesthetically pleasing, and some may take into account perceptual parameters. ( YUV uses these weights : Y = 0.299*R + 0.587*G + 0.114*B )

You can always convert the image to another color space that has only one channel in shades of gray (and 2 "color channels), for example, HSV (V - shades of gray), YUV (Y - shades of gray) or Lab (L is shades of gray ). The differences should not be very large.

The term "desaturation" comes from the HSV space. If you convert the image to HSV, set the S-channel (Saturation) to all zeros and make the image, you will get a 3-channel unsaturated "color" image.

Duplicating these grayscales in RGB will give you a 3-channel unsaturated “color” image, where all 3 RGB channels are identical.

Once you have this 3-channel (RGB) unsaturated image, you can multiply each channel by a separate weight to colorize the image - a sepia image.

Given the gray pixel [v,v,v] , color it like this: [v*a, v*b, v*c] such that 0 <= a,b,c <=1 .

+10
source
 // Loop through the images pixels to reset color. for(x=0; x<image1.Width; x++) { for(y=0; y<image1.Height; y++) { Color pixelColor = image1.GetPixel(x, y); int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11)); //create the color object Color newColor = Color.FromArgb(grayScale, grayScale, grayScale); image1.SetPixel(x, y, newColor); } } 

Convert a color pixel to grayscale using this

 Gray = Green * 0.59 + Blue * 0.11 + Red * 0.30; 
+2
source

For sepia, you want to convert to grayscale first. Then, just multiply each pixel by a given zoom triple, such as [1, 0.95, 0.82], you can play with this triple to get the correct view ... so the code could be ...

 for(x=0; x<image1.Width; x++) { for(y=0; y<image1.Height; y++) { Color pixelColor = image1.GetPixel(x, y); int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11)); //create the color object Color newColor = Color.FromArgb(grayScale, grayScale, grayScale); //now apply a sepia filter pixelColor.R = newColor.R*1; pixelColor.G = newColor.G*0.95; pixelColor.B = newColor.B*0.82; image1.SetPixel(x, y, pixelColor); } } 
+2
source
 private Bitmap grayscale(Image image) { Bitmap btm = new Bitmap(image); for (int i = 0; i < btm.Width; i++) { for (int j = 0; j < btm.Height; j++) { int ser = (btm.GetPixel(i, j).R + btm.GetPixel(i, j).G + btm.GetPixel(i, j).B) / 3; btm.SetPixel(i, j, Color.FromArgb(ser, ser, ser)); } } return btm; } 
+1
source

Here I found a good answer: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1d6b5960-c509-433c-b000-c885b8c727c4/how-to-convert-an-image-to-black- and-white-not-grayscale? forum = csharpgeneral & prof = required

 var color = new Bitmap("file.jpg"); var bw = color.Clone(new Rectangle(0, 0, color.Width, color.Height), PixelFormat.Format1bppIndexed); 
0
source

how to switch from color photo to black and white photo ??

0
source

All Articles