如何从我的数据库中查找特定图像 [英] How Do I Find A Specific Image From My Database

查看:65
本文介绍了如何从我的数据库中查找特定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  button1_Click(对象发​​件人,EventArgs e)
{

SqlConnection cn = new SqlConnection( @ Data Source = SAZ-PC\SQLEXPRESS; Initial Catalog = Voted; Integrated Security = True);
SqlCommand cmd1 = new SqlCommand( select来自Regdmem的FINGERPRINT,cn);
cn.Open();
字节 [] barrImg =(字节 [])cmd1.ExecuteScalar();
foreach byte fp in barrImg)
{
Byte [] bytes = File.ReadAllBytes( D:\\Image.bmp);
bool cmp = barrImg.SequenceEqual(bytes);

if (cmp == true
{
Form3 f3 = new Form3();
f3.Show();
this .Hide();

}
else
{
Application.Exit();
}
}

cn.Close();
}





我有一个数据库,其中有一列名为FINGERPRINT的列存储了多个图像另一方面,我有一个存储在我的驱动器中的图像(D:\\Image.bmp)..所以我的观点是在数据库中搜索我的驱动器图像,如果我的图像存在于数据库中转到下一个表单否则退出..请帮助:(

解决方案

说实话,我认为你正在做一些 巨大的 这里的错误。

忽略更改代码以使用数据库中多行的机制 - 这非常简单,但会使一些大小的数据传输成为可能,并且可能会很慢不使用它 - 我认为这不会在实践中发挥作用。



问题是,除非两张图像 相同 到最后一个微小的细节,这将失败。唯一的方法 - 绝对的,肯定的,绝对的,唯一的方式 - 你将g等等,如果它们是相同的图像。不是同一个手指的两个不同图像,而是相同的图像。位置,角度或光线的微小,不明显的变化将使两个图像大不相同!因此,如果你想用它来检测指纹,那么这是完全错误的做法,你真的,真的,不应该浪费你的时间。



如果你必须比较图像才能找到相同的图像,那么首先要做的就是不要检索任何图像!相反,使用MD5或SHA(最好是后者,但MD5在此应用程序中被破坏并不重要)并将其与图像数据一起存储在单独的列中。然后,您只获取所有哈希值列和ID,并将其与您要为其检查的图像计算的哈希值进行比较 - 只有在找到匹配项时,才会检索实际图像数据并直接进行比较。这样可以减少您提取的数据量并与近乎重要的数据进行比较,从而使您的代码更快!

  using (SqlConnection cn =  new  SqlConnection( @ 数据源= SAZ-PC\SQLEXPRESS;初始目录=已投票;集成安全性=真))
{
使用(SqlCommand cmd1 = new SqlCommand( 选择ID,来自Regdmem的HashValue,cn))
{
cn.Open();
使用(SqlDataReader reader = cmd1.ExecuteReader())
{
while (reader.Read())
{
int id =( int )reader [ ID];
byte [] hash =( byte [])reader [ HashValue];
// 比较这里的哈希值,如果匹配,则检索
< span class =code-comment> // 来自其ID的图像数据并进行比较。
// 如果他们不这样做,请忽略它并尝试下一个。

}
}
}
}


保存图像其他列中的哈希值

private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection cn = new SqlConnection(@"Data Source=SAZ-PC\SQLEXPRESS;Initial Catalog=Voted;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("select FINGERPRINT from Regdmem ", cn);
            cn.Open();
            Byte[] barrImg = (Byte[])cmd1.ExecuteScalar();
            foreach (byte fp in barrImg)
            {
                Byte[] bytes = File.ReadAllBytes("D:\\Image.bmp");
                bool cmp = barrImg.SequenceEqual(bytes);

                if (cmp == true)
                {
                    Form3 f3 = new Form3();
                    f3.Show();
                    this.Hide();

                }
                else
                {
                    Application.Exit();
                }
            }

            cn.Close();
            }



I have a database in which there is a column named "FINGERPRINT" in that column multiple of images are stored and on the other hand i have an image stored in my drive("D:\\Image.bmp") .. so my point is to search my drive image in database if my image exist in the database go to next form else exit.. please help :(

解决方案

To be honest, I think you are making some huge mistakes here.
Ignoring mechanics of changing your code to work with more than a single row in your database - which is pretty trivial, but will make for some serious sized data transfers and probably be so slow you can't use it - I don't think this is going to work in practice.

The problem is that unless the two images are identical down to the last tiny detail this will fail. And the only way - the absolutely, positively, definitely, only way - you will get that is if they are the same image. Not two different images of the same finger, but the same images. A tiny, unnoticeable change in position, angle or lighting will make the two images wildly different! So if you want to use this to detect fingerprints, then this is totally the wrong approach, and you really, really, shouldn't waste any more of your time on it.

If you must compare the images to just find the same image, then the first thing to do is not to retrieve any images! Instead, use MD5 or SHA (preferably the later, but it doesn't matter that MD5 is broken for this application) and store that as well as the image data, in a separate column. You then fetch all hash value columns and ids only, and compare that against the hash value you calculate for the image you want to check - only when you find a match do you retrieve the actual image data and compare it directly. This reduces the amount of data you are fetching and comparing to a near-trivial amount and will make your code a lot faster!

using (SqlConnection cn = new SqlConnection(@"Data Source=SAZ-PC\SQLEXPRESS;Initial Catalog=Voted;Integrated Security=True"))
    {
    using (SqlCommand cmd1 = new SqlCommand("select ID, HashValue from Regdmem ", cn))
        {
        cn.Open();
        using (SqlDataReader reader = cmd1.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int)reader["ID"];
                byte[] hash = (byte[])reader["HashValue"];
                // Compare the hashes here, and if them match, retrieve
                //    the image data from it's ID and compare those.
                //    If they don't, ignore it and try the next.

                }
            }
        }
    }


save image Hash value in other column


这篇关于如何从我的数据库中查找特定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆