我怎样才能颜色不黑的位图使用LockBits黄像素? [英] How can I color pixels that are not black in bitmap in yellow using LockBits?

查看:252
本文介绍了我怎样才能颜色不黑的位图使用LockBits黄像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用getPixel和SetPixel是容易的,但速度很慢,所以我想用LockBits。

Using GetPixel and SetPixel is easy but very slow so I'm trying to using LockBits.

我有这个方法我也很久前两幅图像之间的比较:

I have this method I did long time ago to compare between two images:

public static Bitmap FastComparison(Bitmap bmp1,Bitmap bmp2)
    {
       tolerancenumeric = 15;
       int tolerance = tolerancenumeric * tolerancenumeric + 
                       tolerancenumeric * tolerancenumeric + 
                       tolerancenumeric * tolerancenumeric; //dr * dr + dg * dg + db * db;
       bmp3 = new Bitmap(512,512);  
       PixelFormat pxf = PixelFormat.Format24bppRgb;
       Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
       BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadWrite, pxf);
       BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadWrite, pxf);
       BitmapData bmpData3 = bmp3.LockBits(rect, ImageLockMode.ReadWrite, pxf);

       IntPtr ptr1 = bmpData1.Scan0;
       IntPtr ptr2 = bmpData2.Scan0;
       IntPtr ptr3 = bmpData3.Scan0;

       int numBytes = bmpData1.Stride * bmp1.Height;
       byte[] rgbValues1 = new byte[numBytes];
       Marshal.Copy(ptr1, rgbValues1, 0, numBytes);
       bmp1.UnlockBits(bmpData1);

       byte[] rgbValues2 = new byte[numBytes];
       Marshal.Copy(ptr2, rgbValues2, 0, numBytes);
       bmp2.UnlockBits(bmpData2);


       for (int counter = 0; counter < rgbValues1.Length; counter += 3)
       {
          int  dr, dg, db;
          dr = (int)rgbValues2[counter] - (int)rgbValues1[counter];
          dg = (int)rgbValues2[counter + 1] - (int)rgbValues1[counter + 1];
          db = (int)rgbValues2[counter + 2] - (int)rgbValues1[counter + 2];
          int error = dr * dr + dg * dg + db * db;

          int y, x;
          y = (counter / 3) / 512;
          x = (counter - y * 512 * 3)/3;
          if ((x == 479) && (y == 474))
          {
             Byte r1, g1, b1, r2, g2, b2;
             r1 = rgbValues1[counter];
             b1 = rgbValues1[counter+1];
             g1 = rgbValues1[counter+2];
             r2 = rgbValues2[counter];
             b2 = rgbValues2[counter+1];
             g2 = rgbValues2[counter+2];
           }

           if (error < tolerance)
           {
             rgbValues1[counter] = 0; 
             rgbValues1[counter + 1] = 0;
             rgbValues1[counter + 2] = 0;
           }
         }
         Marshal.Copy(rgbValues1, 0, ptr3, numBytes);
         bmp3.UnlockBits(bmpData3);
         return bmp3;
       }

但现在我也想用LockBits但有一个形象和上色所有不黑黄色的像素。

But now I want to use also LockBits but with one image and to color all the pixels that are not black in yellow.

我开始新的方法:

public Bitmap ChangeColors(Bitmap bmp1)
        {
            bmpColors = new Bitmap(512, 512);
            PixelFormat pxf = PixelFormat.Format24bppRgb;
            Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
            BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadWrite, pxf);

            IntPtr ptr1 = bmpData1.Scan0;

            int numBytes = bmpData1.Stride * bmp1.Height;
            byte[] rgbValues1 = new byte[numBytes];
            Marshal.Copy(ptr1, rgbValues1, 0, numBytes);
            bmp1.UnlockBits(bmpData1);

            for (int counter = 0; counter < rgbValues1.Length; counter += 3)
            {
                int y, x;
                y = (counter / 3) / 512;
                x = (counter - y * 512 * 3) / 3;

                Byte r1, g1, b1;
                r1 = rgbValues1[counter];
                b1 = rgbValues1[counter + 1];
                g1 = rgbValues1[counter + 2];
            }

            return bmpColors;
        }

但不知道如何使它所以位图bmpColors将是原来的,但与所有不黑的黄像素。

But not sure how to make it so the bitmap bmpColors will be the original one but with all pixels that are not black in yellow.

推荐答案

如何简单测试字节,并相应设置它们?

How about simply testing the bytes and setting them accordingly?

Byte r1, g1, b1;
r1 = rgbValues1[counter];         // should be + 2 !!
b1 = rgbValues1[counter + 1];     // should be + 0 !!
g1 = rgbValues1[counter + 2];     // should be + 1 !!

if (r1 + b1 + g1 == 0 ) 
{
    r1 = 255;
    g1 = 255;
}

当然,这是假设一个真正的黑色和黄色的基本都OK与你..

Of course this assumes a real black and a basic yellow are ok with you..

如果您需要更多的控制,你需要更多的code像

If you need more control, you need a bit more code like

if (r1 + b1 + g1 < threshold)

为黑色色调,并为黄可能:

for shades of black, and for the yellows maybe:

    r1 = myYellow_R;
    g1 = myYellow_G;
    b1 = myYellow_B;

BTW :您需要检查这些指标;我最后一次在LockBits阵列看着数据均逆转:不是 RGB ,(更不用说 RBG 因为你拥有了它),但 BGR ! (对于32bpp的 BGRA !)

BTW: You need to check on those indices; the last time I looked the data in the LockBits array were reversed: not RGB, (let alone RBG as you have it) but BGR! (And for 32bpp BGRA !)

由于您使用的是旧的方法,也请确保您的像素格式是确定的;如果是 32bpp的你需要一些简单的修改。

Since you are using that old method, please also make sure your pixel format is ok; if it is 32bpp you'll need a few simple modifications.

这篇关于我怎样才能颜色不黑的位图使用LockBits黄像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆