c# 关于找图问题 -------问题代码 求修改 求解 F5 已烂

失落的神庙 2013-09-13 02:18:14
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));
}



以上代码有 写错的地方 貌似是 ImgDb 方法 求解
...全文
245 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落的神庙 2013-09-16
  • 打赏
  • 举报
回复
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 可用的 差不了很多 比我原来的快多了 谢谢大神 准备结贴
jshi123 2013-09-16
  • 打赏
  • 举报
回复
2.0没有linq,要改成循环判断。 理论上linq可能比直接用for循环慢一点,因为要多create一个enumerator,不过这点差异应该很小的。 还有一些其它可以提高性能的方法,比如LockBits的时候,用位图原来的PixelFormat,避免转换、用unsafe指针避免下标检查等,不过那样程序可读性就降低了,而且性能提高是很有限的。要提高效率的话,应该想办法优化查找算法。
失落的神庙 2013-09-16
  • 打赏
  • 举报
回复
引用 10 楼 jiaoshiyao 的回复:
[quote=引用 8 楼 wawd74520 的回复:]
如果你的原图是100*100的 你要找的图片是99*99的 即是说这两张图片是一样的 只是宽和高有一个像素的变化 这两张图片你在对比的时候 你的就行不通了 不知道你的错误点数是怎么判断的 有一个相似度?[/quote]这个是最基础的 看看代码就知道了 不知道linq 中 All Any 方法 用2.0改写出来 效率怎么样
jiaoshiyao 2013-09-16
  • 打赏
  • 举报
回复
引用 8 楼 wawd74520 的回复:
如果你的原图是100*100的 你要找的图片是99*99的 即是说这两张图片是一样的 只是宽和高有一个像素的变化 这两张图片你在对比的时候 你的就行不通了 不知道你的错误点数是怎么判断的 有一个相似度?
失落的神庙 2013-09-16
  • 打赏
  • 举报
回复
经测试4楼代码 比源代码 快速 稳定 准确 不过我的用2.0开发 Func 刚刚百度才得知 用法 以及linq 用法
失落的神庙 2013-09-16
  • 打赏
  • 举报
回复
引用 6 楼 jiaoshiyao 的回复:
你是怎么对比的 找图片。。没实现出来 例如我要找相似度为80的怎么找。。。
255*(1-0.8) 参数 tolerantError maxErrorPoints为允许错误点的个数
hangang7403 2013-09-16
  • 打赏
  • 举报
回复
顶。。。。。。
jiaoshiyao 2013-09-16
  • 打赏
  • 举报
回复
你是怎么对比的 找图片。。没实现出来 例如我要找相似度为80的怎么找。。。
失落的神庙 2013-09-16
  • 打赏
  • 举报
回复
引用 4 楼 jshi123 的回复:

	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);
	}
jshi123 2013-09-15
  • 打赏
  • 举报
回复

	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);
	}
失落的神庙 2013-09-13
  • 打赏
  • 举报
回复
还是用像素来做 算了 我脑子不好使 求大神改写一下 把速度提高下
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);
        }
上面的代码 是可以的 求改写
失落的神庙 2013-09-13
  • 打赏
  • 举报
回复
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;
        }
失落的神庙 2013-09-13
  • 打赏
  • 举报
回复
求解

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧