码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-01-25 06:31:24      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

        /// <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);
        }

 

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

标签:

原文地址:http://www.cnblogs.com/mahatmasmile/p/4247709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!