简单的图像融合

来源:互联网 发布:长沙网站关键词优化 编辑:程序博客网 时间:2024/05/01 20:29

含笑:不上上次的图像融合,一个小程序。

界面用了PictureBox1和PictureBox2存载两幅原始图片,用PictureBox3显示融合后的图片。

效果:

简单的图像融合

主要算法代码如下,有兴趣的同学自己研究去。

首先添加命名空间:using System.Drawing.Imaging;

            int BPP = 4;
            Bitmap bgImage = new Bitmap(pictureBox1.BackgroundImage);
            Bitmap fgImage = new Bitmap(pictureBox2.BackgroundImage);
            int Width = bgImage.Width;
            int Height = bgImage.Height;
            BitmapData bgData = bgImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData fgData = fgImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            unsafe
            {

                byte* bg = (byte*)bgData.Scan0;
                byte* fg = (byte*)fgData.Scan0;

                int offset = bgData.Stride - Width * 4;

                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        if (fg[3] != 0)
                        {
                            bg[0] = (byte)((fg[0] - bg[0]) * 150 / 255 + bg[0]);
                            bg[1] = (byte)((fg[1] - bg[1]) * 150/ 255 + bg[1]);
                            bg[2] = (byte)((fg[2] - bg[2]) * 150 / 255 + bg[2]);

                        }
                        bg += BPP;
                        fg += BPP;
                    }//X
                    bg += offset;
                    fg += offset;

                }//Y
                bgImage.UnlockBits(bgData);
                fgImage.UnlockBits(fgData);

                Bitmap dstImage = (Bitmap)bgImage.Clone();

                bgImage.Dispose();
                fgImage.Dispose();
                pictureBox3.BackgroundImage = dstImage;

            }

        }
    }


0 0
原创粉丝点击