图像基本变换--- 平移、旋转、缩放、仿射变换、镜像

图像平移变换函数

[算法说明]

  图像平移就是使图像沿水平方向和垂直方向移动。

  如果把座标原点(0,0)平移到点(x0,y0)处,则变换公式为:

  (x,y)为原始图像座标,(x', y')为变换后的图像座标。而图像中的各个像素点移动了sqrt(x*x + y*y)距离。用矩阵表示为2-(22)

[函数代码]

        /// 

        /// Translation process.

        /// 

        /// Source image.

        /// Translate value of x.

        /// Translate value of y.

        /// 

        public static WriteableBitmap TranslationProcess(WriteableBitmap src,int x,int y)////18 平移变换

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap translateImage = new WriteableBitmap(w, h);

            byte[] temp = src.PixelBuffer.ToArray();

            byte[] tempMask = new byte[w * h * 4];

            for (int j = 0; j < h; j++)

            {

                for (int i = 0; i < w; i ++)

                {

                    if (i + x < 0 || i + x >= w || j + y < 0 || j + y >= h)

                    {

                        tempMask[i * 4 + j * w * 4] = (byte)0;

                        tempMask[i * 4 + 1 + j * w * 4] = (byte)0;

                        tempMask[i * 4 + 2 + j * w * 4] = (byte)0;

                    }

                    else

                    {

                        tempMask[i * 4 + j * w * 4] = (byte)(temp[(i + x) * 4 + (j + y) * w * 4]);

                        tempMask[i * 4 + 1 + j * w * 4] = (byte)(temp[(i + x) * 4 + 1 + (j + y) * w * 4]);

                        tempMask[i * 4 + 2 + j * w * 4] = (byte)(temp[(i + x) * 4 + 2 + (j + y) * w * 4]);

                        tempMask[i * 4 + 3 + j * w * 4] = (byte)(temp[(i + x) * 4 + 3 + (j + y) * w * 4]);

                    }

                }

            }

            Stream sTemp = translateImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

            sTemp.Write(tempMask, 0, w * 4 * h);

            return translateImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

Fig.1原图                          Fig.2效果图(x=-20,y=-20)



图像水平镜像函数

[算法说明]

  图像水平镜像就是将图像作如下矩阵变换:

[函数代码]

        /// 

        /// Horizontal mirror process.

        /// 

        /// Source image.

        /// 

        public static WriteableBitmap MirrorXProcess(WriteableBitmap src)////19 水平镜像

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap mirrorImage = new WriteableBitmap(w,h);

            byte[] temp = src.PixelBuffer.ToArray();

            byte[] tempMask = new byte[w * h * 4];

            for (int j = 0; j < h; j++)

            {

                for (int i = 0; i < w; i++)

                {

                    tempMask[i * 4 + j * w * 4] = temp[(w - 1 - i) * 4 + j * w * 4];

                    tempMask[i * 4 + 1 + j * w * 4] = temp[(w - 1 - i) * 4 + 1 + j * w * 4];

                    tempMask[i * 4 + 2 + j * w * 4] = temp[(w - 1 - i) * 4 + 2 + j * w * 4];

                    tempMask[i * 4 + 3 + j * w * 4] = temp[(w - 1 - i) * 4 + 3 + j * w * 4];

                }

            }

            Stream sTemp = mirrorImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

            sTemp.Write(tempMask, 0, w * 4 * h);

            return mirrorImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

Fig.1原图                                Fig.2效果图



图像垂直镜像函数

[算法说明]

  图像垂直镜像就是将图像作如下矩阵变换:

 

          

[函数代码]

        /// 

        /// Vertical mirror process.

        /// 

        /// Source image.

        /// 

        public static WriteableBitmap MirrorYProcess(WriteableBitmap src)////20 垂直镜像

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap mirrorImage = new WriteableBitmap(w,h);

            byte[] temp = src.PixelBuffer.ToArray();

            byte[] tempMask = new byte[w * h * 4];

            for (int i = 0; i < w; i++)

            {

                for (int j = 0; j < h; j++)

                {

                    tempMask[i * 4 + j * w * 4] = temp[i * 4 + (h - 1 - j) * w * 4];

                    tempMask[i * 4 + 1 + j * w * 4] = temp[i * 4 + 1 + (h - 1 - j) * w * 4];

                    tempMask[i * 4 + 2 + j * w * 4] = temp[i * 4 + 2 + (h - 1 - j) * w * 4];

                    tempMask[i * 4 + 3 + j * w * 4] = temp[i * 4 + 3 + (h - 1 - j) * w * 4];

                }

            }          

            Stream sTemp = mirrorImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

            sTemp.Write(tempMask, 0, w * 4 * h);

            return mirrorImage;

            }

            else

            {

                return null;

            }             

        }

[图像效果]

Fig.1原图                                Fig.2效果图


demo 下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=17&extra=page%3D2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章