我已经从图像中识别出了人物。为此原因:
我将所有已识别的斑点(图像)保存在列表中
Bitmap bpt1 = new Bitmap(@"C:\2\torec1.png", true);
Bitmap bpt2 = new Bitmap(@"C:\2\torec2.png", true);
List<Bitmap> toRecognize = new List<Bitmap>();
toRecognize.Add(bpt1);
toRecognize.Add(bpt2);我在字典里有一个已知字母的图书馆。
Bitmap le = new Bitmap(@"C:\2\e.png", true);
Bitmap lg = new Bitmap(@"C:\2\g.png", true);
Bitmap ln = new Bitmap(@"C:\2\n.png", true);
Bitmap li = new Bitmap(@"C:\2\i.png", true);
Bitmap ls = new Bitmap(@"C:\2\s.png", true);
Bitmap lt = new Bitmap(@"C:\2\t.png", true);
var dict = new Dictionary<string, Bitmap>();
dict.Add("e", le);
dict.Add("g", lg);
dict.Add("n", ln);
dict.Add("i", li);
dict.Add("s", ls);
dict.Add("t", lt);然后,我用图片库创建了一个新列表:
var target = dict.ToList();并对图像进行比较:(targetindex.Key,targetindex.Value)
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (CompareMemCmp(toRecognize[i], target[j].Value) == true)
{
textBox3.AppendText("Found!" + Environment.NewLine);
textBox2.AppendText(target[j].Key); //Letter is found - save it!
}
else {textBox3.AppendText("Don't match!" + Environment.NewLine); }
}
}1.删除
2.从性能的角度看,我使用的方法是否可以容忍?我计划同时记录10-20张图像(平均每个字母数为8),字母库将由英文字母(26大写+ 26小写)、特殊字母(~10)和数字(10)组成。
因此,我有需要识别的80+字母和由~70+字符组成的模式库。演出会不会达到一个好的水平?
建设性的批评欣然接受。;)
发布于 2012-02-19 14:04:40
问题1:
删除
问题2:
那得看情况。
首先,如果性能还不够,你的瓶颈是什么?
我怀疑这是CompareMemCmp()函数..。你能加快速度吗?
如果不是,假设您的循环的每一次迭代似乎都独立于前面的迭代,那么您可以尝试并行运行它。
要做到这一点,请看一下Framework4.0的任务并行库方法,特别是Parallel.For。
编辑:
如果我们讨论的是图像之间的完美匹配,您可以尝试使用字典查找来加快速度。
首先,您可以为Bitmap构建一个包装类,该类可以有效地用作Dictionary<>密钥,如下所示:
class BitmapWrapper
{
private readonly int hash;
public Bitmap Image { get; private set; }
public BitmapWrapper(Bitmap img)
{
this.Image = img;
this.hash = this.ComputeHash();
}
private int ComputeHash()
{
// you could turn this code into something unsafe to speed-up GetPixel
// e.g. using lockbits etc...
unchecked // Overflow is fine, just wrap
{
int h = 17;
for (int x = 0; x < this.Image.Size.Width; x++)
for (int y = 0; y < this.Image.Size.Height; y++)
h = h * 23 + this.Image.GetPixel(x, y).GetHashCode();
return h;
}
}
public override int GetHashCode()
{
return this.hash;
}
public override bool Equals(object obj)
{
var objBitmap = obj as Bitmap;
if (obj == null)
return false;
// use CompareMemCmp in case of hash collisions
return Utils.CompareMemCmp(this.Image, objBitmap);
}
}这个类计算受this answer启发的this answer方法中的哈斯代码(但您可以只使用前像素或每个像素)。当然,通过包含不安全的代码(类似于CompareMemCmp方法),这一点可以得到改进。
一旦你有了这个类,你就可以像这样建立一个查找字典:
Bitmap le = new Bitmap(@"C:\2\e.png", true);
Bitmap lg = new Bitmap(@"C:\2\g.png", true);
...
var lookup = new Dictionary<string, Bitmap>();
lookup.Add(new BitmapWrapper(le), "e");
lookup.Add(new BitmapWrapper(lg), "g");
...然后搜索将是简单的:
foreach(var imgToRecognize in toRecognize)
{
string letterFound;
if(lookup.TryGetValue(new BitmapWrapper(imgToRecognize), out letterFound))
{
textBox3.AppendText("Found!" + Environment.NewLine);
textBox2.AppendText(letterFound); //Letter is found - save it!
}
else
textBox3.AppendText("Don't match!" + Environment.NewLine);
}这种方法的性能当然取决于散列计算,但它们可以节省大量的CompareMemCmp()调用。
发布于 2012-02-19 18:28:59
如果C#是正确的工具,这取决于您的图像有多大。哈希表是一种很好的方法,但您需要比较整个图像,然后才能检查是否匹配。Xoring图像非常快,但是您需要对所有图像进行xor,直到找到匹配的图像,这是非常低效率的。
一个更好的方法是选择一个指纹,它的设计方式,你只需要读取最小数量的数据。例如,如果您的图像将为每个图像生成不同的值,则可以在中间生成垂直行的哈希代码。如果不适应这种方法,直到您找到一个算法,您不需要将图像作为一个整体来读取,而只需要读取几个字节,直到您能够将图像分配给正确的桶。这只在输入数据只包含字典中的图像时才有效。否则,这将只是一种概率方法。
https://stackoverflow.com/questions/9349788
复制相似问题