110,535
社区成员
发帖
与我相关
我的任务
分享
public Point GetImageContains(Bitmap p_SourceBitmap, Bitmap p_PartBitmap, int p_Float,int pointnums)
{
Point P_res = new Point(-1, -1);
int Height_S = p_SourceBitmap.Height;
int Width_S = p_SourceBitmap.Width;
int Height_P = p_PartBitmap.Height;
int Width_P = p_PartBitmap.Width;
//原图转换成 byte[]
BitmapData _SourceData = p_SourceBitmap.LockBits(new Rectangle(0, 0, p_SourceBitmap.Width, p_SourceBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] DataSource = new byte[_SourceData.Stride * p_SourceBitmap.Height];
Marshal.Copy(_SourceData.Scan0, DataSource, 0, DataSource.Length);
p_SourceBitmap.UnlockBits(_SourceData);
//子图转换成 byte[]
BitmapData _PartData = p_PartBitmap.LockBits(new Rectangle(0, 0, p_PartBitmap.Width, p_PartBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] DataPart = new byte[_PartData.Stride * p_PartBitmap.Height];
Marshal.Copy(_PartData.Scan0, DataPart, 0, DataPart.Length);
p_PartBitmap.UnlockBits(_PartData);
int index=0;
for (int b = 0; b < Height_S - Height_P; b++)
for (int a = 0; a < Width_S - Width_P; a++)
{
//5点 分别为左上 右上 中间 左下 右下
index = (b * Width_S + a) * 4;
bool b1 = ScanColor(DataSource[index], DataSource[index + 1], DataSource[index + 2], DataPart[0], DataPart[1], DataPart[2], p_Float);
if (!b1) continue;
Point tempp = new Point(a, b);
int p2 = (Width_P - 1) * 4;
b1 = ScanColor(DataSource[index + p2], DataSource[index + 1 + p2], DataSource[index + 2 + p2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
tempp = new Point(a, b + Width_P);
p2 = ((Height_P / 2) * Width_P + (Width_P / 2)) * 4;
int p3 = ((b + (Height_P / 2)) * Width_S + (a + (Width_P / 2))) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
p2 = ((Height_P - 1) * Width_P) * 4;
p3 = ((b + (Height_P - 1)) * Width_S + a) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
p2 = (Height_P * Width_P - 1) * 4;
p3 = ((b + Height_P) * Width_S + (a + Width_P) - 1) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
else
{
//5点匹配 则匹配其余的点
if (ImgDb(index, DataSource, DataPart, p_Float, pointnums))
{
P_res = new Point(a, b);
break;
}
else
P_res = new Point(-1, -1);
}
}
return P_res;
}
private bool ImgDb(int index, byte[] DataImage, byte[] DataPart, int p_Float, int pointnums)
{
int tempint = pointnums;
for (int i = index; i < (DataPart.Length + index); i+=4)
{
bool b3 = ScanColor(DataImage[index], DataImage[index + 1], DataImage[index + 2], DataPart[i - index], DataPart[i - index + 1], DataPart[i - index + 2], p_Float);
if (!b3 && tempint > 0)
{
tempint--;
}
else if (!b3)
return false;
}
return true;
}
private static bool ScanColor(int r1,int g1,int b1,int r2,int g2,int b2, int p_Float)
{
return (r1 <= (r2 + p_Float) && (r1 >= r2 - p_Float)) && ((g1 <= g2 + p_Float) && g1 >= (g2 - p_Float)) && (b1 <= (b2 + p_Float) && (b1 >= b2 - p_Float));
}
public Point FindImage3(Bitmap sourceBitmap, Bitmap partBitmap, int tolerantError, int maxErrorPoints)
{
int w0 = sourceBitmap.Width, h0 = sourceBitmap.Height, w = partBitmap.Width, h = partBitmap.Height;
var bmp1 = sourceBitmap.LockBits(new Rectangle(0, 0, w0, h0), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
var data1 = new byte[bmp1.Stride * bmp1.Height];
Marshal.Copy(bmp1.Scan0, data1, 0, bmp1.Stride * bmp1.Height);
sourceBitmap.UnlockBits(bmp1);
var bmp2 = partBitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
var data2 = new byte[bmp2.Stride * bmp2.Height];
Marshal.Copy(bmp2.Scan0, data2, 0, bmp2.Stride * bmp2.Height);
partBitmap.UnlockBits(bmp2);
int[] rgb = { 2, 1, 0 };
equals_ equals = new equals_(delegate(Point pt1_, Point pt2_)
{
int p1 = pt1_.Y * bmp1.Stride + pt1_.X * 4, p2 = pt2_.Y * bmp2.Stride + pt2_.X * 4;
return AllCompleted(rgb, data1, data2, p1, p2, tolerantError);
});
ptRP ptRP_ = new ptRP(delegate(int x_, int y_) { return new Point(x_, y_); });
var keyPts = new[] { ptRP_(0, 0), ptRP_(w - 1, 0), ptRP_(w / 2, h / 2), ptRP_(0, h - 1), ptRP_(w - 1, h - 1) };
isMatch_ isMatch = new isMatch_(delegate(Point p0_)
{
if (AnyCompleted(keyPts, p0_, equals))
return false;
int tolerant = maxErrorPoints;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
if (!equals(p0_ + (Size)ptRP_(x, y), ptRP_(x, y)) && (tolerant-- <= 0))
return false;
return true;
});
for (int y = 0; y < h0 - h; y++)
for (int x = 0; x < w0 - w; x++)
if (isMatch(ptRP_(x, y)))
return ptRP_(x, y);
return ptRP_(-1, -1);
}
delegate bool equals_(Point pt1, Point pt2);
delegate Point ptRP(int x_, int y_);
delegate bool isMatch_(Point p);
private bool AllCompleted(int[] rgbints, byte[] data1, byte[] data2,int p1,int p2, int tolerantError)
{
foreach (int i in rgbints)
if (!(Math.Abs(data1[p1 + i] - data2[p2 + i]) <= tolerantError))
return false;
return true;
}
private bool AnyCompleted(Point[] keyPts, Point p0_, equals_ equals)
{
foreach (Point i in keyPts)
if (!equals(i + (Size)p0_, i))
return true;
return false;
}
根据4楼大神的代码 改成 2.0 可用的
差不了很多 比我原来的快多了
谢谢大神 准备结贴
public static Point FindImage(Bitmap sourceBitmap, Bitmap partBitmap, int tolerantError, int maxErrorPoints)
{
int w0 = sourceBitmap.Width, h0 = sourceBitmap.Height, w = partBitmap.Width, h = partBitmap.Height;
var bmp1 = sourceBitmap.LockBits(new Rectangle(0, 0, w0, h0), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
var data1 = new byte[bmp1.Stride*bmp1.Height];
Marshal.Copy(bmp1.Scan0, data1, 0, bmp1.Stride*bmp1.Height);
sourceBitmap.UnlockBits(bmp1);
var bmp2 = partBitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
var data2 = new byte[bmp2.Stride*bmp2.Height];
Marshal.Copy(bmp2.Scan0, data2, 0, bmp2.Stride*bmp2.Height);
partBitmap.UnlockBits(bmp2);
int[] rgb = {2,1,0};
Func<Point, Point, bool> equals = (pt1, pt2) =>
{
int p1 = pt1.Y*bmp1.Stride + pt1.X*4, p2 = pt2.Y*bmp2.Stride + pt2.X*4;
return rgb.All(i => Math.Abs(data1[p1 + i] - data2[p2 + i]) <= tolerantError);
};
Func<int, int, Point> pt = (x, y) => new Point(x, y);
var keyPts = new[] {pt(0, 0), pt(w - 1, 0), pt(w/2, h/2), pt(0, h - 1), pt(w - 1, h - 1)};
Func<Point, bool> isMatch = p0 =>
{
if (keyPts.Any(p => !equals(p + (Size) p0, p)))
return false;
int tolerant = maxErrorPoints;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
if (!equals(p0 + (Size) pt(x, y), pt(x, y)) && (tolerant-- <= 0))
return false;
return true;
};
for (int y = 0; y < h0 - h; y++)
for (int x = 0; x < w0 - w; x++)
if (isMatch(pt(x, y)))
return pt(x, y);
return pt(-1, -1);
}
public Point FindImage(Bitmap p_SourceBitmap, Bitmap p_PartBitmap, int p_Float, int pointnums)
{
int tempnums = pointnums;
for (int a = 0; a < p_SourceBitmap.Height - p_PartBitmap.Height; a++)
{
for (int b = 0; b < p_SourceBitmap.Width - p_PartBitmap.Width; b++)
{
Color c1 = p_SourceBitmap.GetPixel(b, a);
Color c1_ = p_PartBitmap.GetPixel(0, 0);
if (!ScanColor(c1, c1_, p_Float))
continue;
c1 = p_SourceBitmap.GetPixel(b + p_PartBitmap.Width, a);
c1_ = p_PartBitmap.GetPixel(p_PartBitmap.Width - 1, 0);
if (!ScanColor(c1, c1_, p_Float))
continue;
c1 = p_SourceBitmap.GetPixel(b + p_PartBitmap.Width / 2 + 1, a + p_PartBitmap.Height / 2 + 1);
c1_ = p_PartBitmap.GetPixel(p_PartBitmap.Width / 2, p_PartBitmap.Height / 2);
if (!ScanColor(c1, c1_, p_Float))
continue;
c1 = p_SourceBitmap.GetPixel(b, a + p_PartBitmap.Height);
c1_ = p_PartBitmap.GetPixel(0, p_PartBitmap.Height-1);
if (!ScanColor(c1, c1_, p_Float))
continue;
c1 = p_SourceBitmap.GetPixel(b + p_PartBitmap.Width, a + p_PartBitmap.Height);
c1_ = p_PartBitmap.GetPixel(p_PartBitmap.Width-1, p_PartBitmap.Height-1);
if (!ScanColor(c1, c1_, p_Float))
continue;
bool t = true;
tempnums = pointnums;
for (int c = 0; c < p_PartBitmap.Height; c++)
{
for (int d = 0; d < p_PartBitmap.Width; d++)
{
c1 = p_SourceBitmap.GetPixel(b + d, a + c);
c1_ = p_PartBitmap.GetPixel(d, c);
if (!ScanColor(c1, c1_, p_Float))
{
if (tempnums <= 0)
{
t = false; break;
}
else
tempnums--;
}
}
if (!t) break;
}
if (t)
{
return new Point(b, a);
}
}
}
return new Point(-1, -1);
}
上面的代码 是可以的
求改写private bool ImgDb(int index, byte[] DataImage, byte[] DataPart, int p_Float, int pointnums)
{
int tempint = pointnums;
for (int i = index; i < (DataPart.Length + index); i+=4)
{
bool b3 = ScanColor(DataImage[i], DataImage[i+ 1], DataImage[i+ 2], DataPart[i - index], DataPart[i - index + 1], DataPart[i - index + 2], p_Float);
if (!b3 && tempint > 0)
{
tempint--;
}
else if (!b3)
return false;
}
return true;
}