Skip to main content
 首页 » 编程设计

C# 图像识别性能——比较图像列表和图像字典

2024年02月23日15thcjp

我已经设法从图像中识别字符。为此:

我将所有可识别的 Blob (图像)保存在列表中

        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(); 

并进行图像比较:(target[index].Key, target[index].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 多个字符的模式库。性能是否会处于良好水平?

建设性的批评欣然接受。 ;)

请您参考如下方法:

问题 1:

[删除]

问题 2:

视情况而定。
首先,如果性能不够,你的瓶颈是什么?
我怀疑是 CompareMemCmp()功能...所以你能加快速度吗?

如果不是,鉴于循环的每次迭代似乎都独立于之前的迭代,您可以尝试并行运行它。
为此,请查看框架 4.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);  
    } 
} 

此类计算 ComputeHash 中的 hascode受 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()电话。