圖像基本變換--- 平移、旋轉、縮放、仿射變換、鏡像

圖像平移變換函數

[算法說明]

  圖像平移就是使圖像沿水平方向和垂直方向移動。

  如果把座標原點(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

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