Win8 Metro(C#)數位影像處理--2.55OSTU法映像二值化

來源:互聯網
上載者:User

標籤:fill   mpm   eth   ==   ase   view   win   edit   return   

原文:Win8 Metro(C#)數位影像處理--2.55OSTU法映像二值化



[函數名稱]

  Ostu法映像二值化      WriteableBitmap OstuThSegment(WriteableBitmap src)

[函數代碼]

        /// <summary>        /// Ostu method of image segmention.        /// </summary>        /// <param name="src">The source image.</param>        /// <returns></returns>        public static WriteableBitmap OstuThSegment(WriteableBitmap src) ////Ostu法閾值分割        {            if (src != null)            {                int w = src.PixelWidth;                int h = src.PixelHeight;                WriteableBitmap dstImage = new WriteableBitmap(w, h);                byte[] temp = src.PixelBuffer.ToArray();                byte[] tempMask = (byte[])temp.Clone();                //定義灰階映像資訊儲存變數                int[] srcData = new int[w * h];                //定義閾值變數                int Th = 0; ;                //定義背景和目標像素數目變數N1,N2,灰階變數U1,U2,灰階和變數Sum1,Sum2,臨時緩衝變數Temp                int N1 = 0, N2 = 0, Sum1 = 0, Sum2 = 0;                //定義背景和目標像素比例變數W1,W2,映像整體平均灰階變數U,方差變數g,對比閾值變數TT                double W1 = 0, W2 = 0, U1 = 0, U2 = 0, g = 0, TT = 0;                for (int j = 0; j < h; j++)                {                    for (int i = 0; i < w; i++)                    {                        srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299);                    }                }                //尋找最大類間方差                for (int T = 0; T <= 255; T++)                {                    for (int i = 0; i < srcData.Length; i++)                    {                        if (srcData[i] > T)                        {                            N2++;                            Sum2 += srcData[i];                        }                        else                        {                            N1++;                            Sum1 += srcData[i];                        }                    }                    W1 = (double)(N1 / (N1 + N2));                    W2 = (double)(1.0 - W1);                    U1 = (N1 == 0 ? 0.0 : (Sum1 / N1));                    U2 = (N2 == 0 ? 0.0 : (Sum2 / N2));                    g = N1 * N2 * (U1 - U2) * (U1 - U2);                    if (g > TT)                    {                        TT = g;                        Th = T;                    }                    N1 = 0; N2 = 0;                    Sum1 = 0; Sum2 = 0; W1 = 0.0; W2 = 0.0; U1 = 0.0; U2 = 0.0; g = 0.0;                }                for (int j = 0; j < h; j++)                {                    for (int i = 0; i < w; i++)                    {                        temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);                    }                }                Stream sTemp = dstImage.PixelBuffer.AsStream();                sTemp.Seek(0, SeekOrigin.Begin);                sTemp.Write(temp, 0, w * 4 * h);                return dstImage;            }            else            {                return null;            }        }

Win8 Metro(C#)數位影像處理--2.55OSTU法映像二值化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.