C# 指標操作映像 二值化處理

來源:互聯網
上載者:User

標籤:

        /// <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# 指標操作映像 二值化處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.