// A basic frame comparision for new object location finder for webcam vision // // Cem Aykan // www.acddv.com // cemaykan@acddv.com using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Text; namespace GeminiImageTester { //This FindLocation method will compare two BMP's frame0, frame1 to see if frame1 has a new //object in it. If it finds a object it will return the location of the new object. Values //it can return for the location of the object is on the x axis and are: // -"None" if the two frames are identical // -"Right" if object is on the right side // -"Left" if object is on the left side // -"Middle" if object is on the middle of the frame class GeminiBotObjectFinder { private int slice1 = 0; private int slice2 = 0; private int slice3 = 0; public string FindLocation(object sender, Bitmap BMPFrame0, Bitmap BMPFrame1) { BitmapData f0data = BMPFrame0.LockBits(new Rectangle(0, 0, BMPFrame0.Width, BMPFrame0.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData f1data = BMPFrame1.LockBits(new Rectangle(0, 0, BMPFrame1.Width, BMPFrame1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stridef0 = f0data.Stride; System.IntPtr Scan0f0 = f0data.Scan0; int stridef1 = f1data.Stride; System.IntPtr Scan0f1 = f1data.Scan0; unsafe { byte* p0 = (byte*)(void*)Scan0f0; byte* p1 = (byte*)(void*)Scan0f1; for (int i = 0; i < f0data.Height; i++) { for (int j = 0; j < f0data.Width * 3; j++) { if ((p0[0] - p1[0]) < (byte)(100)) { /* Do nothing for now */ } else { //Calculation of which slice the image is located on if (j < f0data.Width) this.slice1++; else if ((j < f0data.Width * 2) & (j > f0data.Width)) this.slice2++; else if ((j < f0data.Width * 3) & (j > f0data.Width * 2)) this.slice3++; } p0++; p1++; } p0 += stridef0 - f0data.Width * 3; p1 += stridef1 - f1data.Width * 3; } } BMPFrame0.UnlockBits(f0data); BMPFrame1.UnlockBits(f1data); return DecideLocation(); } private string DecideLocation() { string result = "None"; if ((this.slice1 == this.slice2) & (this.slice2 == this.slice3)) result = "None"; else if ((this.slice1 >= this.slice2) & (this.slice1 >= this.slice3)) result = "Left"; else if ((this.slice2 >= this.slice1) & (this.slice2 >= this.slice3)) result = "Middle"; else if ((this.slice3 >= this.slice1) & (this.slice3 >= this.slice2)) result = "Right"; return result; } } }