论坛首页 Java企业应用论坛

图像的单色变换——原理与实现(Single color transformation)

浏览 3317 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (2)
作者 正文
   发表时间:2009-01-16  

Single color transformation——Principle and implement

 

From May 1 to May 3, I have a 3-day holiday. When I was on the Internet, I downloaded some resource about image process by chance. Before I have seen someone develop a java-based image process program (JPS short for java Photoshop). Then I think I can also develop one based CSharp. Good idea! Let’s do it.

 

But image process is a profound discipline. It requires adept programming skills and good mathematical knowledge. The best programming language for it is C or C plusplus, because they calculate very fast. However java and .Net are run time translated language. They are not fast enough for image process. So I just look it as playing and a method of wasting the vacuity time.

 

Chapter 1 Single color transformation

 

Today I will talk about single color transformation. That is to say how we can transform a color photo to a black and white one.

As we all know, in computer science color of a color monitor comprises R, G and B 3 color. RGB have a value form 0 to 255. So we change each pixel of a color photo to a black, white or gray one, then it will look like a black and white photo.

There are 5 methods can make it. I call R-transformation, G-transformation, B-transformation, Mean-transformation and Inverse-phase. Now I will interpret the principle of each.

 

R-transformation

 

getpixel() method of Picturebox or Bitmap can return a value of Color. Class Color has r, g and b 3 attributes. They are the RGB values of a pixel. Then we can change and save them with setPixel() method easily. Source codes are:

Bitmap bmpold = new Bitmap(picOld.Image);

            Color c = new Color();

 

            for (int i = 0; i < bmpold.Width; i++)

                for (int j = 0; j < bmpold.Height; j++)

                {

                    c = bmpold.GetPixel(i, j);

                    Color cc = Color.FromArgb(c.R, c.R, c.R);

                    bmpold.SetPixel(i, j, cc);

                }

            picNew.Image = bmpold;

 

We can use a picture to test it. The original picture is a photo of a beautiful girl, with short hair, big eyes and a big sweet smile. The transformation results can be find in my albums.

 

G and B-transformation

 

Please pay attention to the line with green color. We can change it to c.G and c.B. then we can get G and B-transformation.

 

Y-transformation

 

for (int i = 0; i < bmpold.Width; i++)

                for (int j = 0; j < bmpold.Height; j++)

                {

                    c = bmpold.GetPixel(i, j);

                    int r, g, b, y;

                    r = c.R;

                    g = c.G;

                    b = c.B;

                    y = (int)(0.31*r +0.59* g + 0.11*b);

                    if (y < 0) y = 0;

                    if (y > 255) y = 255;

                    Color cc = Color.FromArgb(y, y, y);

                    bmpold.SetPixel(i, j, cc);

                }

Mean-transformation

It is also easy to understand. We can get the mean value of a pixel’ RGB. And set the pixel with the mean value. Look source code below:

c = bmpold.GetPixel(i, j);

                    int r, g, b, y;

                    r = c.R;

                    g = c.G;

                    b = c.B;

                    y = (int)(0.31*r +0.59* g + 0.11*b);

                    if (y < 0) y = 0;

                    if (y > 255) y = 255;

                    Color cc = Color.FromArgb(y, y, y);

So easy! Isn’t it??

 

Inverse-transformation

 

c = bmpold.GetPixel(i, j);

                    int r, g, b, y;

                    r = 255-c.R;

                    g = 255 - c.G;

                    b = 255 - c.B;

                    Color cc = Color.FromArgb(r, g, b);

                    bmpold.SetPixel(i, j, cc);

Just pay attention to the green lines. If you have any knowledge of programming language, you can understand it.

 

The End

 

There are many funny pictures’ transformations. I will interpret the principle and implement it in C#.Net.

I just do it for fun. And writing in English can practice my written English.

 

101905_chengyuhanx400.jpgInverse变换.JPG

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics