QQ登录

只需一步,快速开始

登录 | 立即注册 | 找回密码
微像素首页 图像处理 图像基础 查看内容
订阅

文章

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

发表时间:2016-5-10 00:20作者:bean 阅读(1146) 评论: 0

导读: 本文详细介绍了图像基本变换--- 平移、旋转、缩放、仿射变换、镜像的相关知识,并 给出了完整程序DEMO的下载链接,跟大家分享一下,希望大家喜欢!

图像平移变换函数

[算法说明]

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

  如果把坐标原点(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


鲜花

握手

雷人

路过

鸡蛋
上一篇:图像基本变换---二值图像距离变换(欧氏距离+棋盘距离+城市街区距离)下一篇:图像基本变换---图像二值化(包含OSTU/迭代法/统计法/双峰法/P分位法/最大熵法)

最新评论

    联系我们
  • QQ:3243218172
  • 邮箱:3243218172@qq.com
  • QQ交流群:567648913
    手机扫描二维码