Win8 Metro(C#)數字圖像處理--2.69中點濾波器

原文: Win8 Metro(C#)數字圖像處理--2.69中點濾波器

[函數代碼]php



<strong>        /// <summary>
        /// Mid-point filter.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap MidPointFilterProcess(WriteableBitmap src)////中點濾波器
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap filterImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                double[] Gray = new double[9];
                double gray = 255, graymax = 0;
                int tempr = 0, tempb = 0, tempg = 0, temprMax = 0, tempgMax = 0, tempbMax = 0;
                for (int j = 1; j < h - 1; j++)
                {
                    for (int i = 1; i < w - 1; i++)
                    {
                        tempb = 0;
                        tempg = 0;
                        tempr = 0;
                        gray = 255;
                        graymax = 0;
                        int[] B = new int[9] { tempMask[i * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j - 1) * w * 4], tempMask[i * 4 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + j * w * 4], tempMask[(i + 1) * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j + 1) * w * 4], tempMask[i * 4 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + (j + 1) * w * 4] };
                        int[] G = new int[9] { tempMask[i * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4], tempMask[i * 4 + 1 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 1 + j * w * 4], tempMask[(i + 1) * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4], tempMask[i * 4 + 1 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] };
                        int[] R = new int[9] { tempMask[i * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4], tempMask[i * 4 + 2 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 2 + j * w * 4], tempMask[(i + 1) * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4], tempMask[i * 4 + 2 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] };
                        for (int n = 0; n < 9; n++)
                        {
                            Gray[n] = (double)B[n] * 0.114 + (double)G[n] * 0.587 + (double)R[n] * 0.299;
                            if (gray > Gray[n])
                            {
                                gray = Gray[n];
                                tempb = B[n];
                                tempr = R[n];
                                tempg = G[n];
                            }
                            if (graymax < Gray[n])
                            {
                                graymax = Gray[n];
                                tempbMax = B[n];
                                tempgMax = G[n];
                                temprMax = R[n];
                            }
                        }
                        temp[i * 4 + j * w * 4] = (byte)((tempb + tempbMax) / 2);
                        temp[i * 4 + 1 + j * w * 4] = (byte)((tempg + tempgMax) / 2);
                        temp[i * 4 + 2 + j * w * 4] = (byte)((tempr + temprMax) / 2);
                    }
                }
                Stream sTemp = filterImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return filterImage;
            }
            else
            {
                return null;
            }
        }</strong>

[圖像效果]html


最後,分享一個專業的圖像處理網站(微像素),裏面有不少源代碼下載:




相關文章
相關標籤/搜索