c# 照片叠加和获取图像边缘

最近company在做有关AI相关的事情,大概意思是通过训练一系列样本来达到可以自动判读图斑变化。能力有限只能做些比较边缘的工作,比如生成训练样本后,需要对样本做一个人工的评价。

待评价的图片有三样,前期瓦片、后期瓦片以及样本(前后变化部分的图片)。样本的制作也参与过一部分,但是由于其他安排没有进行下去,废话不多说,先介绍如何进行评价(评价依据):

1、先获取样本图片的边界

2、将边界图片分别和前期后期叠加,切换对比做出判断

接下来进行处理:

前期、后期、变化的图片:

获取样本图片的边界:

        private Bitmap GetPicBound(string blackPath)
        {
            //获取边界
            var image = Image.FromFile(blackPath);
            Color c1 = new Color();
            Color c2 = new Color();
            Color c3 = new Color();
            Color c4 = new Color();
            int i, j;
            int rr, gg, bb;
            int r1, r2, r3, r4, fxr, fyr;
            int g1, g2, g3, g4, fxg, fyg;
            int b1, b2, b3, b4, fxb, fyb;
            Bitmap box1 = new Bitmap(image);
            var save = new Bitmap(box1.Width, box1.Height);

            for (i = 0; i <= image.Width - 2; i++)
            {
                for (j = 0; j <= image.Height - 2; j++)
                {

                    c1 = box1.GetPixel(i, j);
                    c2 = box1.GetPixel(i + 1, j + 1);
                    c3 = box1.GetPixel(i + 1, j);
                    c4 = box1.GetPixel(i, j + 1);
                    r1 = c1.R;
                    r2 = c2.R;
                    r3 = c3.R;
                    r4 = c4.R;
                    fxr = r1 - r2;
                    fyr = r3 - r4;
                    rr = Math.Abs(fxr) + Math.Abs(fyr) + 128;
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    g1 = c1.G;
                    g2 = c2.G;
                    g3 = c3.G;
                    g4 = c4.G;
                    fxg = g1 - g2;
                    fyg = g3 - g4;
                    gg = Math.Abs(fxg) + Math.Abs(fyg) + 128;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    b1 = c1.B;
                    b2 = c2.B;
                    b3 = c3.B;
                    b4 = c4.B;
                    fxb = b1 - b2;
                    fyb = b3 - b4;
                    bb = Math.Abs(fxb) + Math.Abs(fyb) + 128;
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    Color cc = new Color();
                    if (bb == 128)
                        cc = Color.Transparent;
                    else
                        cc = Color.FromArgb(rr, gg, bb);//只保留边界
                    save.SetPixel(i, j, cc);

                }
            }

            return save;// BitmapToBlack(box1, 0.8);

        }

效果图:

边界图分别和前期、后期叠加:

        private Image getDJImage(Bitmap frontImage, Bitmap background)
        {
            try
            {
                float fImage = 0.8f;//透明度

                int iwidth = background.Width > frontImage.Width ? background.Width : frontImage.Width;
                int iheight = background.Height > frontImage.Height ? background.Height : frontImage.Height;
                Bitmap mixImage2 = new Bitmap(iwidth, iheight);

                Graphics g = Graphics.FromImage(mixImage2);
                float[][] colormatrix ={
                                        new float[]{1,0,0,0,0},//代表了R
                                        new float[]{0,1,0,0,0},//代表了G
                                        new float[]{0,0,1,0,0},//代表了B
                                        new float[]{0,0,0,fImage,0},//代表了A
                                        new float[]{0,0,0,0,1}
                                        };
                ColorMatrix cm = new ColorMatrix(colormatrix);
                ImageAttributes imageAtt = new ImageAttributes();
                imageAtt.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(background, new Point(0, 0));
                g.DrawImage(frontImage, new Rectangle(0, 0, frontImage.Width, frontImage.Height), 0, 0, frontImage.Width, frontImage.Height, GraphicsUnit.Pixel, imageAtt);
                Image imgDJ = mixImage2;
                return imgDJ;
            }
            catch (Exception ex)
            {
                WriteLogs("叠加照片失败:" + ex.Message + ex.StackTrace);
                MessageBox.Show(ex.Message);
                return null;
            }
        }

代码参考:

c#图像处理-边缘检测

猜你喜欢

转载自blog.csdn.net/fangyu723/article/details/108239139