Win8 Metro(C#)數字圖像處理--2.50圖像運動模糊

原文: Win8 Metro(C#)數字圖像處理--2.50圖像運動模糊



[函數名稱]html

  圖像運動模糊算法    MotionblurProcess(WriteableBitmap src,int k,int direction)算法

[算法說明]函數

  運動模糊是指在攝像機獲取圖像時,因爲景物和相機之間的相對運動而形成的圖像上的模糊。這裏spa

咱們主要介紹勻速直線運動所形成的模糊,因爲非勻速直線運動在某些條件下能夠近似爲勻速直線.net

運動,或者能夠分解爲多個勻速直線運動的合成,所以,在攝像機較短的圖像曝光時間內,形成圖code

像模糊的運動狀況能夠近似爲勻速直線運動。htm

  對於勻速直線運動,圖像的運動模糊能夠用如下公式表示:blog

/// <summary>
        /// Motion blur process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="k">The offset of motion, from 0 to 200.</param>
        /// <param name="direction">The direction of motion, x:1, y:2.</param>
        /// <returns></returns>
        public static WriteableBitmap MotionblurProcess(WriteableBitmap src,int k,int direction)////運動模糊處理
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                int b, g, r;
                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x ++)
                    {
                        b = g = r = 0;
                        switch (direction)
                        {
                            case 1:
                                if (x >= k)
                                {
                                    for (int i = 0; i <= k; i++)
                                    {
                                        b += (int)tempMask[(x - i) * 4 + y * w * 4];
                                        g += (int)tempMask[(x - i) * 4 + 1 + y * w * 4];
                                        r += (int)tempMask[(x - i) * 4 + 2 + y * w * 4];
                                    }
                                    temp[x * 4 + y * w * 4] = (byte)(b / (k + 1));
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(g / (k + 1));
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(r / (k + 1));
                                }
                                else
                                {
                                    if (x > 0)
                                    {
                                        for (int i = 0; i < x; i++)
                                        {
                                            b += (int)tempMask[(x - i) * 4 + y * w * 4];
                                            g += (int)tempMask[(x - i) * 4 + 1 + y * w * 4];
                                            r += (int)tempMask[(x - i) * 4 + 2 + y * w * 4];
                                        }
                                        temp[x * 4 + y * w * 4] = (byte)(b/(x+1));
                                        temp[x * 4 + 1 + y * w * 4] = (byte)(g/(x+1));
                                        temp[x * 4 + 2 + y * w * 4] = (byte)(r/(x+1));
                                    }
                                    else
                                    {
                                        temp[x * 4 + y * w * 4] = (byte)(tempMask[x * 4 + y * w * 4] / k);
                                        temp[x * 4 + 1 + y * w * 4] = (byte)(tempMask[x * 4 + 1 + y * w * 4] / k);
                                        temp[x * 4 + 2 + y * w * 4] = (byte)(tempMask[x * 4 + 2 + y * w * 4] / k);
                                    }
                                }
                                break;
                            case 2:
                                if (y >= k)
                                {
                                    for (int i = 0; i <= k; i++)
                                    {
                                        b += (int)tempMask[x * 4 + (y - i) * w * 4];
                                        g += (int)tempMask[x * 4 + 1 + (y - i) * w * 4];
                                        r += (int)tempMask[x * 4 + 2 + (y - i) * w * 4];
                                    }
                                    temp[x * 4 + y * w * 4] = (byte)(b / (k + 1));
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(g / (k + 1));
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(r / (k + 1));
                                }
                                else
                                {
                                    if (y > 0)
                                    {
                                        for (int i = 0; i < y; i++)
                                        {
                                            b += (int)tempMask[x * 4 + (y - i) * w * 4];
                                            g += (int)tempMask[x * 4 + 1 + (y - i) * w * 4];
                                            r += (int)tempMask[x * 4 + 2 + (y - i) * w * 4];
                                        }
                                        temp[x * 4 + y * w * 4] = (byte)(b/(y+1));
                                        temp[x * 4 + 1 + y * w * 4] = (byte)(g/(y+1));
                                        temp[x * 4 + 2 + y * w * 4] = (byte)(r/(y+1));
                                    }
                                    else
                                    {
                                        temp[x * 4 + y * w * 4] = (byte)(tempMask[x * 4 + y * w * 4] / k);
                                        temp[x * 4 + 1 + y * w * 4] = (byte)(tempMask[x * 4 + 1 + y * w * 4] / k);
                                        temp[x * 4 + 2 + y * w * 4] = (byte)(tempMask[x * 4 + 2 + y * w * 4] / k);
                                    }
                                }
                                break;
                            default :
                                break;                              
                        }
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }
相關文章
相關標籤/搜索