天天看點

c# 照片疊加和擷取圖像邊緣

最近company在做有關AI相關的事情,大概意思是通過訓練一系列樣本來達到可以自動判讀圖斑變化。能力有限隻能做些比較邊緣的工作,比如生成訓練樣本後,需要對樣本做一個人工的評價。

待評價的圖檔有三樣,前期瓦片、後期瓦片以及樣本(前後變化部分的圖檔)。樣本的制作也參與過一部分,但是由于其他安排沒有進行下去,廢話不多說,先介紹如何進行評價(評價依據):

1、先擷取樣本圖檔的邊界

2、将邊界圖檔分别和前期後期疊加,切換對比做出判斷

接下來進行處理:

前期、後期、變化的圖檔:

c# 照片疊加和擷取圖像邊緣

擷取樣本圖檔的邊界:

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);

        }
           

效果圖:

c# 照片疊加和擷取圖像邊緣

邊界圖分别和前期、後期疊加:

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# 照片疊加和擷取圖像邊緣

代碼參考:

c#圖像處理-邊緣檢測