图像颜色处理——简单三色加减
颜色主要是由于红,绿,蓝,三色构成,所以只要调节这三色就可以了。
在每个分量上加加减减便可以看出效果。
原图:
减小红色当量后的效果红色减弱
值全部价高后发现变亮
值全部减小后发现变暗
所以一并做了亮度,全部加上同一个值就可以
主要代码:
public Bitmap ColorBalance(Bitmap b,int red,int green,int blue) { if (red < -255) red = -255; if (red > 255) red = 255; if (green < -255) green = -255; if (green > 255) green = 255; if (blue < -255) blue = -255; if (blue > 255) blue = 255; int width = b.Width; int height = b.Height; BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, b.PixelFormat); //Bytes Per Pixel int BPP = 4; unsafe { byte* p = (byte*) data.Scan0; int offset = data.Stride - width*BPP; int pixel; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixel = p[0] + blue; if (pixel < 0) pixel = 0; if (pixel > 255) pixel = 255; p[0] = (byte)pixel; pixel = p[1] + green; if (pixel < 0) pixel = 0; if (pixel > 255) pixel = 255; p[1] = (byte)pixel; pixel = p[2] + red; if (pixel < 0) pixel = 0; if (pixel > 255) pixel = 255; p[2] = (byte)pixel; p += BPP; } p += offset; } b.UnlockBits(data); return b; } }