图像基本变换---图像亮度对比度调增算法

[图像亮度]

图像亮度调整公式如公式2-(5)所示:

  其中v属于[-255, 255]。

  由于像素值的大小范围为[0,255],因此,像素的亮度值可以用原始值与调整增量的和表示,且最大亮度为255,即白色,最小亮度为0,即黑色。

[函数代码]

        /// <summary>

        /// Bright adjust process.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <param name="brightnessValue">Brightness value, from -255 to 255.</param>

        /// <returns></returns>

        public static WriteableBitmap BrightnessAdjustProcess(WriteableBitmap src,int brightnessValue)////5 亮度调整

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap brightImage = new WriteableBitmap(w,h);

            byte[] temp = src.PixelBuffer.ToArray();

            for (int i = 0; i < temp.Length; i += 4)

            {

                temp[i] = (byte)(((brightnessValue + temp[i]) > 255 ? 255 : (brightnessValue + temp[i])) < 0 ? 0 : ((brightnessValue + temp[i]) > 255 ? 255 : (brightnessValue + temp[i])));

                temp[i+1] = (byte)(((brightnessValue + temp[i+1]) > 255 ? 255 : (brightnessValue + temp[i+1])) < 0 ? 0 : ((brightnessValue + temp[i+1]) > 255 ? 255 : (brightnessValue + temp[i+1])));

                temp[i+2] = (byte)(((brightnessValue + temp[i+2]) > 255 ? 255 : (brightnessValue + temp[i+2])) < 0 ? 0 : ((brightnessValue + temp[i+2]) > 255 ? 255 : (brightnessValue + temp[i+2])));

            }

            Stream sTemp = brightImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

            sTemp.Write(temp, 0, w * 4 * h);

            return brightImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

图像基本变换---图像亮度对比度调增算法_第1张图片

Fig.1原图                     Fig.2效果图(brightnessValue =192)


[图像对比度指]

对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,差异范围越大代表对比越大,差异范围越小代表对比越小,对比度调整就是在保证平均亮度不变的情况下,增大或缩小这个差异,因此可以用如下公式表示:

  其中,k属于[-1,1],average为图像平均亮度。

  简化后如公式2-(7)所示:

  由于average计算量大,比较耗时,而它对图像亮度调整的贡献比较小,因此,这里我们一般取average=127.5,以此来代替平均亮度。于是,对比度调整公式变化如下:

[函数代码]

        /// <summary>

        /// Contrast adjust process.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <param name="contrastValue">Contrast value, from -1 to 1.</param>

        /// <returns></returns>

        public static WriteableBitmap ContrastAdjustProcess(WriteableBitmap src,double contrastValue)////6 对比度调整

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap contrastImage = new WriteableBitmap(w,h);

            byte[] temp = src.PixelBuffer.ToArray();

            for (int i = 0; i < temp.Length; i += 4)

            {

                temp[i] = Convert.ToByte((((temp[i] - 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])) < 0 ? 0 : (((temp[i] - 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])));

                temp[i + 1] = Convert.ToByte((((temp[i + 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])) < 0 ? 0 : (((temp[i + 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])));

                temp[i + 2] = Convert.ToByte((((temp[i + 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])) < 0 ? 0 : (((temp[i + 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])));

            }

            Stream sTemp = contrastImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

            sTemp.Write(temp, 0, w * 4 * h);

            return contrastImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

图像基本变换---图像亮度对比度调增算法_第2张图片

Fig.1原图                       Fig.2效果图(contrastValue =0.6)


demo 下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=42&extra=page%3D1

你可能感兴趣的:(图像基本变换)