• C#获取加强的图片数据


            public byte[] GetStrongPictureData(string fileName)
            {
                byte[] bmpData = null;
                if (File.Exists(fileName))
                {
                    Bitmap bitmap = (Bitmap)(Image.FromFile(fileName));
                    int width = bitmap.Width;                //长度像素值
                    int height = bitmap.Height;              //高度像素值
                    Rectangle rect = new Rectangle(0, 0, width, height);
                    BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    IntPtr srcPtr = bitmapData.Scan0;
                    int bmpLength = bitmapData.Stride * height;
                    bmpData = new byte[bmpLength];
                    Marshal.Copy(srcPtr, bmpData, 0, bmpLength);            //复制GRB信息到byte数组
                    bmpData = OnOutClean(width, height, bmpData);
                    Marshal.Copy(bmpData, 0, srcPtr, bmpData.Length);       //复制回位图
                    bitmap.UnlockBits(bitmapData);                          //解锁位图

                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, bitmap.RawFormat);
                    ms.Flush();
                    bmpData = ms.GetBuffer();
                    ms.Close();

                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, bitmap.RawFormat);
                    ms.Flush();
                    bmpData = ms.GetBuffer();
                    ms.Close();
                }
                else
                {
                    bmpData = null;
                }
                return bmpData;
            }

    public static void memset(byte[] buf, byte val, int size)
            {
                for (int i = 0; i < size; i++)
                {
                    buf[i] = val;
                }
            }

            public static byte[] OnOutClean(int width, int height, byte[] _old)
            {
                int x, y;
                byte r, g, b;
                byte[] rp = new byte[width * height];
                byte[] gp = new byte[width * height];
                byte[] bp = new byte[width * height];
                byte[] _newRGB = new byte[3 * width * height];
                memset(rp, 255, rp.Length);
                memset(gp, 255, gp.Length);
                memset(bp, 255, bp.Length);
                memset(_newRGB, 255, _newRGB.Length);

                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        b = _old[y * width * 3 + 3 * x + 0];
                        g = _old[y * width * 3 + 3 * x + 1];
                        r = _old[y * width * 3 + 3 * x + 2];
                        rp[y * width + x] = b;
                        gp[y * width + x] = g;
                        bp[y * width + x] = r;
                    }
                }

                int[] tol1 = new int[2];
                int[] tol2 = new int[2];
                int[] tol3 = new int[2];

                tol1 = Low_High_Value(width, height, rp, 256);
                tol2 = Low_High_Value(width, height, gp, 256);
                tol3 = Low_High_Value(width, height, bp, 256);

                ImgAdjust(width, height, tol1[0], tol1[1], 0.0, 1.0, rp, rp);
                ImgAdjust(width, height, tol2[0], tol2[1], 0.0, 1.0, gp, gp);
                ImgAdjust(width, height, tol3[0], tol3[1], 0.0, 1.0, bp, bp);

                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        _newRGB[y * width * 3 + 3 * x + 0] = bp[y * width + x];
                        _newRGB[y * width * 3 + 3 * x + 1] = gp[y * width + x];
                        _newRGB[y * width * 3 + 3 * x + 2] = rp[y * width + x];
                    }
                }
                return _newRGB;
            }

            //图像对比度加强
            public static int[] Low_High_Value(int width, int height, byte[] input, int count)
            {
                int i, x, y;
                int sum = 0;
                double bot = 0.0005;
                double top = 0.9995;
                int[] values = new int[2];
                double[] his = new double[count];
                double[] his_plus = new double[count];
               
                for (x = 0; x < width; x++)
                {
                    for (y = 0; y < height; y++)
                    {
                        int a = input[x + y * width];
                        his[a]++;
                        sum++;
                    }
                }
                his_plus[0] = his[0];
                for (i = 1; i < count; i++)
                {
                    his_plus[i] = his_plus[i - 1] + his[i];
                }
                for (i = 0; i < count; i++)
                {
                    his_plus[i] = his_plus[i] / sum;
                }
                //双阀值
                for (i = 0; i < count; i++)
                {
                    if (his_plus[i] > bot)
                    {
                        values[0] = i;
                        break;
                    }
                }
                for (i = 0; i < count; i++)
                {
                    if (his_plus[i] > top)
                    {
                        values[1] = i;
                        break;
                    }
                }
                return values;
            }

            public static void ImgAdjust(int width, int height, int lIn, int hIn, double lOut, double hOut, byte[] input, byte[] output)
            {
                double[] lpstr = new double[width * height];
                double[] lut = new double[256];
                double[] lut1 = new double[256];
                int i = 0;
                int x, y;
                //门限值
                double fmin = (double)lIn * 1.0 / 255;
                double fmax = (double)hIn * 1.0 / 255;
                //得到拉伸后的图像
                for (i = 0; i < 256; i++)
                {
                    lut[i] = i * 1.0 / 256;//像素为i的值对应的线性变化值
                    //确保像素点在[fmin,fmax]间
                    if (lut[i] > fmax)
                        lut[i] = fmax;
                    if (lut[i] > fmin)
                    {
                        lut1[i] = lut[i];
                    }
                    else
                    {
                        lut1[i] = fmin;
                    }
                    //图像拉伸,[fmin,fmax]拉伸到[0,1]
                    lut1[i] = (lut1[i] - fmin) / (fmax - fmin);
                    lut1[i] = lut1[i] * (hOut - lOut) + lOut;
                }
                for (x = 0; x < width; x++)
                {
                    for (y = 0; y < height; y++)
                    {
                        lpstr[x + y * width] = lut1[input[x + y * width]] * 255;
                        output[x + y * width] = (byte)lpstr[x + y * width];
                    }
                }
            }

  • 相关阅读:
    最近的几个坑
    最近对Allegro的几个总结
    sub drawing
    Allegro的几点小结
    产品量产的几个问题
    电源板问题的定位
    PCB学习
    servlet(6) 链接数据库
    servlet(5) HttpSession
    servlet(4)异常处理
  • 原文地址:https://www.cnblogs.com/junior/p/2536628.html
Copyright © 2020-2023  润新知