且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Win8 Metro(C#)数字图像处理--2.50图像运动模糊

更新时间:2022-09-27 14:27:16

原文:Win8 Metro(C#)数字图像处理--2.50图像运动模糊



[函数名称]

  图像运动模糊算法    MotionblurProcess(WriteableBitmap src,int k,int direction)

[算法说明]

  运动模糊是指在摄像机获取图像时,由于景物和相机之间的相对运动而造成的图像上的模糊。这里

我们主要介绍匀速直线运动所造成的模糊,由于非匀速直线运动在某些条件下可以近似为匀速直线

运动,或者可以分解为多个匀速直线运动的合成,因此,在摄像机较短的图像曝光时间内,造成图

像模糊的运动情况可以近似为匀速直线运动。

  对于匀速直线运动,图像的运动模糊可以用以下公式表示:

Win8 Metro(C#)数字图像处理--2.50图像运动模糊

Win8 Metro(C#)数字图像处理--2.50图像运动模糊

       /// <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;
            }
        }
Win8 Metro(C#)数字图像处理--2.50图像运动模糊