• C#图片处理之:亮度和对比度的校正


    亮度和对比度应该是最常见的处理要求了。就算是N年前9寸黑白电视机也必有这两个旋钮。

    亮度调整算法很简单。对每一个像素的RGB值同时加上或减去一个特定的值就可以了。当然由于RGB取值范围都是在[0,255]的,所以要考虑到越界的问题。

            /**//// <summary>
            
    /// 图像明暗调整
            
    /// </summary>
            
    /// <param name="b">原始图</param>
            
    /// <param name="degree">亮度[-255, 255]</param>
            
    /// <returns></returns>

            public static Bitmap KiLighten(Bitmap b, int degree)
            ...{
                if (b == null)
                ...{
                    return null;
                }


                if (degree < -255) degree = -255;
                if (degree > 255) degree = 255;

                try
                ...{

                    int width = b.Width;
                    int height = b.Height;

                    int pix = 0;

                    BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                    unsafe
                    ...{
                        byte* p = (byte*)data.Scan0;
                        int offset = data.Stride - width * 3;
                        for (int y = 0; y < height; y++)
                        ...{
                            for (int x = 0; x < width; x++)
                            ...{
                                // 处理指定位置像素的亮度
                                for (int i = 0; i < 3; i++)
                                ...{
                                    pix = p[i] + degree;

                                    if (degree < 0) p[i] = (byte)Math.Max(0, pix);
                                    if (degree > 0) p[i] = (byte)Math.Min(255, pix);

                                }
     // i
                                p += 3;
                            }
     // x
                            p += offset;
                        }
     // y
                    }


                    b.UnlockBits(data);

                    return b;
                }

                catch
                ...{
                    return null;
                }


            }
     // end of Lighten

    对比度校正的思想也很简单,大的原则就是让亮的更亮,暗的更暗。具体实现的方式有很多种,比如取一个阀值,超过的就加一定的值,不到的就减一定的值等等。

            /**//// <summary>
            
    /// 图像对比度调整
            
    /// </summary>
            
    /// <param name="b">原始图</param>
            
    /// <param name="degree">对比度[-100, 100]</param>
            
    /// <returns></returns>

            public static Bitmap KiContrast(Bitmap b, int degree)
            ...{
                if (b == null)
                ...{
                    return null;
                }


                if (degree < -100) degree = -100;
                if (degree > 100) degree = 100;

                try
                ...{

                    double pixel = 0;
                    double contrast = (100.0 + degree) / 100.0;
                    contrast *= contrast;
                    int width = b.Width;
                    int height = b.Height;
                    BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    unsafe
                    ...{
                        byte* p = (byte*)data.Scan0;
                        int offset = data.Stride - width * 3;
                        for (int y = 0; y < height; y++)
                        ...{
                            for (int x = 0; x < width; x++)
                            ...{
                                // 处理指定位置像素的对比度
                                for (int i = 0; i < 3; i++)
                                ...{
                                    pixel = ((p[i] / 255.0 - 0.5) * contrast + 0.5) * 255;
                                    if (pixel < 0) pixel = 0;
                                    if (pixel > 255) pixel = 255;
                                    p[i] = (byte)pixel;
                                }
     // i
                                p += 3;
                            }
     // x
                            p += offset;
                        }
     // y
                    }

                    b.UnlockBits(data);
                    return b;
                }

                catch
                ...{
                    return null;
                }

            }
     // end of Contrast
  • 相关阅读:
    (三)认识twisted reactor
    (二)inlineCallbacks,同步方式写异步代码
    (一)使用twisted Deferred
    javascript通过字典思想操作数据
    锱铢必较,从(function(){}())与(function(){})()说起
    针对谷歌默认最小字体12px的正确解决方案 (css、html)
    百度搜索研发部:同义词反馈机制
    LinkedList与ArrayList的区别
    从源码的角度分析List与Set的区别
    springboot整合redisson分布式锁
  • 原文地址:https://www.cnblogs.com/chennie/p/2324594.html
Copyright © 2020-2023  润新知