.NET 圖片像素比較

來源:互聯網
上載者:User

標籤:logs   its   rect   offset   try   ati   out   off   sharp   

        /// <summary>        /// 映像比較.用於找出兩副圖片之間的差異位置        /// </summary>        public class ImageComparer        {            /// <summary>            /// 映像顏色            /// </summary>            [StructLayout(LayoutKind.Explicit)]            private struct ICColor            {                [FieldOffset(0)]                public byte B;                [FieldOffset(1)]                public byte G;                [FieldOffset(2)]                public byte R;            }            /// <summary>            /// 按20*20大小進行分塊比較兩個映像.            /// </summary>            /// <param name="bmp1"></param>            /// <param name="bmp2"></param>            /// <returns></returns>            public static List<Rectangle> Compare(Bitmap bmp1, Bitmap bmp2)            {                return Compare(bmp1, bmp2, new Size(20, 20));            }            /// <summary>            /// 比較兩個映像            /// </summary>            /// <param name="bmp1"></param>            /// <param name="bmp2"></param>            /// <param name="block"></param>            /// <returns></returns>            public static List<Rectangle> Compare(Bitmap bmp1, Bitmap bmp2, Size block)            {                List<Rectangle> rects = new List<Rectangle>();                PixelFormat pf = PixelFormat.Format24bppRgb;                BitmapData bd1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, pf);                BitmapData bd2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, pf);                try                {                    unsafe                    {                        int w = 0, h = 0;                        while (h < bd1.Height && h < bd2.Height)                        {                            byte* p1 = (byte*)bd1.Scan0 + h * bd1.Stride;                            byte* p2 = (byte*)bd2.Scan0 + h * bd2.Stride;                            w = 0;                            while (w < bd1.Width && w < bd2.Width)                            {                                //按塊大小進行掃描                                for (int i = 0; i < block.Width; i++)                                {                                    int wi = w + i;                                    if (wi >= bd1.Width || wi >= bd2.Width) break;                                    for (int j = 0; j < block.Height; j++)                                    {                                        int hj = h + j;                                        if (hj >= bd1.Height || hj >= bd2.Height) break;                                        ICColor* pc1 = (ICColor*)(p1 + wi * 3 + bd1.Stride * j);                                        ICColor* pc2 = (ICColor*)(p2 + wi * 3 + bd2.Stride * j);                                        // if (Math.Abs(pc1->R - pc2->R)>10 || pc1->G != pc2->G || pc1->B != pc2->B)                                        if (Math.Abs(pc1->R - pc2->R) > 100 || Math.Abs(pc1->G - pc2->G) > 100 || Math.Abs(pc1->B - pc2->B) > 100)                                        {                                            //當前塊有某個象素點顏色值不相同.也就是有差異.                                            int bw = Math.Min(block.Width, bd1.Width - w);                                            int bh = Math.Min(block.Height, bd1.Height - h);                                            rects.Add(new Rectangle(w, h, bw, bh));                                            goto E;                                        }                                    }                                }                            E:                                w += block.Width;                            }                            h += block.Height;                        }                    }                }                finally                {                    bmp1.UnlockBits(bd1);                    bmp2.UnlockBits(bd2);                }                return rects;            }        }

  

.NET 圖片像素比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.