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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bravebean/article/details/51374077
————————————————
版权声明:本文为CSDN博主「bravebean」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/bravebean/article/details/51374077

图像平移变换函数
[算法说明]

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

  如果把坐标原点(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
————————————————
版权声明:本文为CSDN博主「bravebean」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/bravebean/article/details/51374077

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值