zoukankan      html  css  js  c++  java
  • 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(00, 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(00, 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
     
    需要实例的朋友请到文件中去下载。[图片亮度调整实例代码]
  • 相关阅读:
    Oracle Flashback技术
    管理Redo Log
    管理UNDO
    Oracle利用PIVOT和UNPIVOT进行行列转换
    如何在SQL CASE表达式中返回多个值
    第二十八节 jQuery事件委托
    第二十七节 jQuery弹框练习
    第二十六节 jQuery中的事件冒泡
    第二十五节 jQuery中bind绑定事件和解绑
    第二十四节 jQuery中的resize事件
  • 原文地址:https://www.cnblogs.com/chennie/p/2335487.html
Copyright © 2011-2022 走看看