• C# 指针操作图像 二值化处理


            /// <summary>
            /// 二值化图像
            /// </summary>
            /// <param name="bmp"></param>
            /// <returns></returns>
            private static unsafe Bitmap Binaryzation(Bitmap bmp)
            {
                BitmapData dstData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
                byte* data = (byte*)(dstData.Scan0);
                //将图像转换为0,1二值得图像; 
                int step = dstData.Stride;
    
                int means = getThreshold(data, bmp.Height * step);
                for (int y = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width * 3; x += 3)
                    {
                        if (data[y * step + x + 2] > means)
                            data[y * step + x]
                                = data[y * step + x + 1]
                                = data[y * step + x + 2]
                                = 255;
                        else
                            data[y * step + x]
                                = data[y * step + x + 1]
                                = data[y * step + x + 2]
                                = 0;
                    }
                }
                bmp.UnlockBits(dstData);
                return bmp;
            }
    
            /// <summary>
            /// 图像二值化 获取阀值
            /// </summary>
            /// <param name="inPixels"></param>
            /// <param name="length">height * Stride</param>
            /// <returns></returns>
            private static unsafe int getThreshold(byte* inPixels, int length)
            {
                int inithreshold = 127;
                int finalthreshold = 0;
                List<int> temp = new List<int>();
                for (int index = 0; index < length; index += 3)
                {
                    temp.Add(inPixels[index + 2]);
                }
                List<int> sub1 = new List<int>();
                List<int> sub2 = new List<int>();
                int means1 = 0, means2 = 0;
                while (finalthreshold != inithreshold)
                {
                    finalthreshold = inithreshold;
                    for (int i = 0; i < temp.Count(); i++)
                    {
                        if (temp[i] <= inithreshold)
                        {
                            sub1.Add(temp[i]);
                        }
                        else
                        {
                            sub2.Add(temp[i]);
                        }
                    }
                    means1 = getMeans(sub1);
                    means2 = getMeans(sub2);
                    sub1.Clear();
                    sub2.Clear();
                    inithreshold = (means1 + means2) / 2;
                }
                return finalthreshold;
            }
    
            /// <summary>
            /// 图像二值化 获取Means
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            private static int getMeans(List<int> data)
            {
                int result = 0;
                int size = data.Count();
                foreach (int i in data)
                {
                    result += i;
                }
                return (result / size);
            }
  • 相关阅读:
    核新同花顺数据结构
    python发送各类邮件的主要方法
    Tesseract-OCR引擎 入门
    ruby函数回调的实现方法
    软件单元测试之我见
    cocos cteator中tiled模式 用图集容易出线
    入坑CCC遇到的一些坑啊
    关于委托的理解
    火狐浏览器的三个坑
    抗锯齿的BUG
  • 原文地址:https://www.cnblogs.com/mahatmasmile/p/4247709.html
Copyright © 2020-2023  润新知