本文作者:佚名

C#之图象编程

佚名 2019-06-29 ???
摘要:最近一直在研究C#的图象编程,本来想做个PhotoShop中的颜色自动处理功能(类似自动色阶,自动对比度,自动颜色的功能)研究了一段时间


    最近一直在研究C#的图象编程,本来想做个PhotoShop中的颜色自动处理功能(类似自动色阶,自动对比度,自动颜色的功能)研究了一段时间,没有达到预期的效果,但是有一点心得还是想跟大家分享一下.
    自动色阶(在网上查找了一些资料,在CSDN上找到了一些帖子)
    简单介绍一下这个原理
      0-----------------------------------255   
      ----min---------------------max--------   
    扫描图像,找到图像明度的最大最小值,将max映射为255或250等较大值,min映射到或5等较小值,重新计算各点的明度,思想上把各点的明度值差距拉开. R,G,B三种颜色分别处理
    代码如下:
    public class test
    {
        //图象为Argb,所以BPP= 4,如果是RGB,BPP=3 
        public const int BPP = 4;
        //下面这些变量定义的是R,G,B三个分量亮度的最大值和最小值
        public int Max_R_bright = 0;
        public int Min_R_bright = 255;
        public int Max_B_bright = 0;
        public int Min_B_bright = 255;
        public int Max_G_bright = 0;
        public int Min_G_bright = 255;

        

        //c#对图象进行操作,推荐大家使用unsafe,使用指针操作图象速度方面还不错,比getpixel要快的多.

        public Bitmap bright(Bitmap b)
        {
            int width = b.Width;
            int height = b.Height;
            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
            ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * BPP;

                int pixel = 0;

                //这一部分主要是统计出图象R,G,B三个颜色的最大值和最小值分别是多少
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        // 处理像素 B, G, R 亮度三分量
                        for (int i = 0; i < 3; i++)
                        {
                            //i==0的时候处理B颜色分量                            
                            if (i == 0)
                            {
                                Max_B_bright = Math.Max(p[i], Max_B_bright);
                                Min_B_bright = Math.Min(p[i], Min_B_bright);
                            }

                            //i==1的时候处理G颜色分量                            
                            if (i == 1)
                            {
                                Max_G_bright = Math.Max(p[i], Max_G_bright);
                                Min_G_bright = Math.Min(p[i], Min_G_bright);
                            }

                            //i==2的时候处理R颜色分量                            
                            if (i == 2)
                            {
                                Max_R_bright = Math.Max(p[i], Max_R_bright);
                                Min_R_bright = Math.Min(p[i], Min_R_bright);
                            }
                            pixel = p[i];
                            if (pixel < 0) pixel = 0;
                            if (pixel > 255) pixel = 255;

                            p[i] = (byte)pixel;
                        }
                        p += BPP;
                    }
                    p += offset;
                }



                

                p = (byte*)data.Scan0;
                offset = data.Stride - width * BPP;
                pixel = 0;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        // 处理像素 B, G, R 亮度三分量
                        //下面主要就是进行颜色的处理了.R,G,B,三种颜色分别做处理.
                        //用到了公式 (Color - Min_Color) * 255/(Max_color - Min_Color)
                        //Color代表当前颜色 Min_Color代表颜色的最小值 Max_Color代表颜色的最大值
                        for (int i = 0; i < 3; i++)
                        {

                            if (i == 0)
                            {
                                pixel = (p[i] - Min_B_bright) * 255 / ((Max_B_bright) - Min_B_bright);
                            }
                            if (i == 1)
                            {
                                pixel = (p[i] - Min_G_bright) * 255 / ((Max_G_bright) - Min_G_bright);
                            }
                            if (i == 2)
                            {
                                pixel = (p[i] - Min_R_bright) * 255 / ((Max_R_bright) - Min_R_bright);
                            }

                            if (pixel < 0) pixel = 0;
                            if (pixel > 255) pixel = 255;

                            p[i] = (byte)pixel;
                        }
                        p += BPP;
                    }
                    p += offset;
                }

        }

    }

     

未经允许不得转载:

作者:佚名,标题:C#之图象编程,原文地址:https://www.vfjianzhan.com/android/201906/92768.html发布于2019-06-29
转载或复制请以超链接形式并注明出处唯福建站

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏