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

wawd74520 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 方法 求解
...全文
295 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
  • 打赏
  • 举报
回复
求解
源码链接: https://pan.quark.cn/s/0f7c75cb3aad ### MIPI Video Mode 与 Command Mode 的差异 #### 一、引言 MIPI (Mobile Industry Processor Interface) 是一种用于连接移动设备中处理器及其外围设备的标准化接口。MIPI 接口支持多种协议,其中包含 DSI (Display Serial Interface) 和 DCS (Display Control Interface) 等协议。在 MIPI 接口的应用中,主要涉及两种工作模式:Video Mode(视频模式)和 Command Mode(命令模式)。本文旨在系统性地阐述这两种模式的工作机制、特性以及实际应用环境。 #### 二、LCD RAM 概念说明 在进一步探讨 MIPI 的两种模式之前,有必要对文中提及的“LCD RAM”概念进行明确。实际上,“LCD RAM”并非一个通用术语,而是本文作者用来描述 LCD 控制器中用于存储显示数据的内存区域。LCD(Liquid Crystal Display,液晶显示屏)通常配备一个控制 IC(Integrated Circuit,集成电路),该控制 IC 可能内置 RAM 以缓存显示数据。 #### 三、MIPI Video Mode(视频模式) **定义:** - 视频模式是一种类似于传统 RGB 接口的工作模式,它要主机持续不断地向显示器传输刷新数据。 - 在这种模式下,数据和控制信号以报文的形式通过 MIPI 总线进行传输。 - 显示器本身无需配备帧缓冲器,因为主机会周期性地刷新屏幕。 **特点:** 1. **实时性高:** 主机需要不...
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 在电磁模拟技术中,CST(Computer Simulation Technology)是一种被广泛采纳的软件工具,它主要用于电磁场、微波、天线以及射频系统的设计工作。本资料将详细分析CST软件中离散端口的具体配置方法,这些方法对于提升仿真结果的精确度和专业水准具有决定性作用。离散端口在CST软件中扮演着模拟信号输入或输出的重要角色,它们构成了仿真模型不可或缺的部分。在配置离散端口时,一个核心的原则是保证端口的方向与网格线保持一致,这是因为这样做能够有效降低计算过程中产生的误差,并确保仿真数据的有效性。如果未能遵循这一指导原则,可能会引发未知的计算问题,进而导致仿真结果失去可靠性。 在CST软件中配置离散端口,通常需要借助“Pick Points”这一功能。通过选择“Pick Edge Center”选项,端口将被设定在模型边缘的中心位置上。然而,这种做法并不总是能够确保端口与网格线保持平行。在某些特定情形下,模型的几何构造可能不允许直接选取一个与网格线平行的边作为端口的安装位置。 为了克服这一挑战,可以采用多种不同的策略。如果模型本身已经包含一条与馈电口平行的边,那么可以直接利用这条边来建立端口,此时CST软件会自动调整端口使其与网格线对齐。另一种可选的方法是,当模型不具备现成的平行边时,用户可以手动构建一个几何结构,比如一个立方体,并使其边缘与馈电口平行。通过这种方式,新建立的几何结构的边缘就可以作为端口的位置,从而确保端口与网格线的平行关系。 在实施上述操作时,必须关注端口尺寸的合理性和物理意义的一致性。端口的尺寸应当依据实际天线馈电部分的尺寸进行适当调整,过大的端口或...
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 【使用TensorFlow进行图像识别】 图像识别作为计算机视觉领域的关键任务之一,其核心在于通过算法解析和理解图像所包含的信息。在此资源中,我们将集中探讨如何借助功能强大的深度学习框架TensorFlow来执行手写数字识别。手写数字识别构成了众多实际应用的基础,例如自动支票处理、光学字符识别(OCR)等场景。 TensorFlow是由Google创建的一个开源库,它主要用于数值运算和机器学习,尤其在深度学习方面表现卓越。其核心优势在于可以构建并训练复杂的神经网络架构,并且在多种硬件环境中实现高效执行,涵盖CPU和GPU平台。 在此实践项目中,我们将运用TensorFlow来构建一个卷积神经网络(CNN)模型,这种架构是处理图像数据的理想选择。CNNs通过模仿人脑视觉皮层的运作机制,能够自主地提取图像中的关键特征,进而达成识别目标。在手写数字识别的特定情境下,这些特征可能涉及笔画的几何形态、走向以及相互间的连接模式。 对于CNN的基础结构,我们需要具备相应的认知,其通常由卷积层、池化层、全连接层以及激活函数等部分组成。卷积层借助滤波器(亦称卷积核)对图像进行扫描,以捕捉局部特征;池化层则用于降低数据维度,同时保留核心信息;全连接层将特征向量映射至各类别的概率分布;而激活函数如ReLU则通过引入非线性元素,使模型能够学习更为复杂的模式。 在此案例中,建议采用MNIST数据集,这是一个广泛用于手写数字识别的标准测试集。该数据集包含60,000个训练样本和10,000个测试样本,每个样本均为28x28像素的灰度图像,代表0到9这十个数字中的某一个。为了训练模型,必须首先加载数据,并...

111,129

社区成员

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

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

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