c#中的白色像素

时间:2017-04-28 13:17:44

标签: c#

我想在具有最接近的彩色像素(R,G,B< 150)的位图图像中对白色像素(R,G,B> 150)着色,更确切地说,我希望每个白色像素都被着色。

   for (int X = 1; X < myBitmap.Width-1; X++)
        {
            for (int Y = 1; Y < myBitmap.Height-1; Y++)
            {
                pix = myBitmap.GetPixel(X, Y);
                pixx = myBitmap.GetPixel(X-1, Y-1);
                Color pixwhite = myBitmap.GetPixel(X, Y);
                Color pixelColor = myBitmap.GetPixel(X - 1, Y - 1);

                if (pix.R > 150 && pix.G > 150 && pix.B > 150)
                {
                    Color myWhite = new Color();
                    Color myColor = new Color();
                    myWhite = Color.FromArgb(pix.R, pix.G, pix.B);

                    if (pixx.R < 150 && pixx.G < 150 && pixx.B < 150)
                    {
                        myColor = Color.FromArgb(pixx.R, pixx.G, pixx.B);
                        if (pixwhite != pixelColor)
                        {
                            myBitmap.SetPixel(X, Y, myColor);

                        }

                    }

                }
                else                   
                    myBitmap.SetPixel(X, Y, pix);
            }
        }
        e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
    }

很明显,我犯了错误,有人能帮助我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

private void pictureBox1_Paint(object sender,PaintEventArgs e)         {

        Bitmap myBitmap = new Bitmap(pictureBox1.Image);
        //pictureBox1_Paint.Image = bmp;
        // Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);
        Color pix;
        Color pixx;

        for (int X = 1; X < myBitmap.Width-1; X++)
        {
            for (int Y = 1; Y < myBitmap.Height-1; Y++)
            {
                pix = myBitmap.GetPixel(X, Y);
                pixx = myBitmap.GetPixel(X, Y);
                Color pixwhite = myBitmap.GetPixel(X, Y);
                Color pixelColor = myBitmap.GetPixel(X - 1, Y - 1);

                if (pix.R > 150 && pix.G > 150 && pix.B > 150)
                {
                    Color myWhite = new Color();
                    Color myColor = new Color();
                    myWhite = Color.FromArgb(pix.R, pix.G, pix.B);

                    if (pixx.R < 150 && pixx.G < 150 && pixx.B < 150)
                    {
                        myColor = Color.FromArgb(pixx.R, pixx.G, pixx.B);
                        if (pixwhite != pixelColor)
                        {
                            myBitmap.SetPixel(X, Y, myColor);

                        }

                    }

                    Color newcolor = new Color();

                    newcolor = Color.FromArgb(Math.Abs(pix.R - pixx.R) + Math.Abs(pix.G - pixx.G) + Math.Abs(pix.B - pixx.B));

                }
                //else
                //{  //myBitmap.SetPixel(X, Y, pix);
                 //   Color nearc = myBitmap.GetPixel(X, Y);
                //    myBitmap.SetPixel(X, Y, nearc);
                //}
            }
        }
        // Draw myBitmap to the screen again.
        e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
    }

enter image description here

第一个是原始的,第二个是“改变的”

答案 1 :(得分:0)

如果你想用好的像素替换坏像素,你不仅需要一种识别坏像素的方法,还必须有一种识别好像素的方法。如果你盲目地采取另一个像素,那个也可能是坏的。

我会把坏像素周围的所有好像素的平均颜色,即在3 x 3的正方形中看好像素,假设你只想消除微小的亮点。

// Search for good pixels and take their average color.
// The coordinates of the bad pixel are (X, Y).
int totR = 0, totG = 0, totB = 0, n = 0;
for (int i = X - 1; i <= X + 1; i++) {
    for (int j = Y - 1; j <= Y + 1; j++) {
        Color p = myBitmap.GetPixel(i, j);
        if (p.R < 150 || p.G < 150 || p.B < 150) {
            // The pixel at [i, j] is a good one.
            totR += p.R; totG += p.G; totB += p.B;
            n++;
        }
    }
}
if (n > 0) {
    // We found at least one good pixel.
    myBitmap.SetPixel(X, Y, Color.FromArgb(totR / n, totG / n, totB / n));
}

如果找不到任何人,你可以看一个5 x 5的方格(这有点困难,因为你必须留在图像内)。