. NET Picture Pixel comparison

Source: Internet
Author: User

        <summary>///image comparison. Used to find the difference between two images////</summary> public class Imagecomparer {//<summary>///Image Color///</summary> [StructLayout (layou  TKIND.EXPLICIT)] Private struct Iccolor {[FieldOffset (0)] public byte                B                [FieldOffset (1)] public byte G;            [FieldOffset (2)] public byte R;            }///<summary>///by 20*20 size to compare two images. </summary>//<param name= "BMP1" ></param>//<param name= "BMP2" ></p aram>//<returns></returns> public static list<rectangle> Compare (Bitmap bmp1            , Bitmap bmp2) {return Compare (BMP1, BMP2, New Size (20, 20)); }///<summary>///Compare two images///&Lt;/summary>//<param name= "BMP1" ></param>//<param name= "BMP2" &GT;&LT;/PARAM&G            T <param name= "block" ></param>///<returns></returns> public static list& Lt 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) {//scan by block size 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-&GT;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) {                                            The current block has a pixel point with a different color value. 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 Picture Pixel comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.