Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

c# 3.0 - convert image to Black-White or Sepia in c#

I want to change 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many ways to desaturate a color image. In fact, there is probably no one "true" or "correct" way to do it, though some ways are more "correct" than others.
I assume that your image is in RGB (Red-Green-Blue) format (though BGR is also common).

The simplest way, which should work for most photos (but less so for synthetic images), is to just use the Green channel of the 3 RGB channels. Humans are most sensitive to variations 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.

A better way to generate a grayscale image is to use a weighted average of the 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 that sum to 1) will give different results some of which may be considered more aesthetically pleasing, and some may take into consideration perceptual parameters. (YUV uses these weights: Y = 0.299*R + 0.587*G + 0.114*B)

You could always convert the image to another color space which has only a single grayscale channel (and 2 "color" channels), such as HSV (V is the grayscale), YUV (Y is the grayscale) or Lab (L is the grayscale). The differences should not be very big.

The term "de-saturation" comes from the HSV space. If you convert you image to HSV, set the S channel (Saturation) to be all zeros, and render the image, you will get a 3-channel desaturated "color" image.

Duplicating these grayscale channels into RGB will give you a 3-channel desaturated "color" image - where all 3 RGB channels are identical.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...