title: Using MinHash to find similarities between 2 images
body: <p>I am using MinHash algorithm to find similar images between images.
  I have run across this post, <a href="https://stackoverflow.com/questions/2168830/how-can-i-recognize-slightly-modified-images"><code>How can I recognize slightly modified images?</code></a> which pointed me to <a href="http://www.robots.ox.ac.uk/~vgg/publications/papers/chum08a.pdf" rel="nofollow noreferrer">MinHash</a> algorithm.</p>
  
  <p>I was using a C# implementation from this blog post, <a href="http://blogs.msdn.com/spt/archive/2008/06/10/set-similarity-and-min-hash.aspx" rel="nofollow noreferrer"><code>Set Similarity and Min Hash</code></a>.</p>
  
  <p>But while trying to use the implementation, I have run into 2 problems.</p>
  
  <ul>
  <li>What value should I set <code>universe</code> value to?</li>
  <li>When passing image byte array to <code>HashSet</code>, it only contains distinct byte values; thus comparing values from 1 ~ 256.</li>
  </ul>
  
  <p><strong>What is this <code>universe</code> in MinHash? <br />
  And what can I do to improve the C# MinHash implementation?</strong></p>
  
  <p>Since <a href="http://msdn.microsoft.com/en-us/library/bb495294.aspx" rel="nofollow noreferrer"><code>HashSet&lt;byte&gt;</code></a> contains values upto 256, similarity value always come out to 1.</p>
  
  <p>Here is the source that uses the C# MinHash implementation from <a href="http://blogs.msdn.com/spt/archive/2008/06/10/set-similarity-and-min-hash.aspx" rel="nofollow noreferrer"><code>Set Similarity and Min Hash</code></a>: </p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var imageSet1 = GetImageByte(@".\Images\01.JPG");
          var imageSet2 = GetImageByte(@".\Images\02.TIF");
          //var app = new MinHash(256);
          var app = new MinHash(Math.Min(imageSet1.Count, imageSet2.Count));
          double imageSimilarity = app.Similarity(imageSet1, imageSet2);
          Console.WriteLine("similarity = {0}", imageSimilarity);
      }
  
      private static HashSet&lt;byte&gt; GetImageByte(string imagePath)
      {
          using (var fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
          using (var br = new BinaryReader(fs))
          {
              //List&lt;int&gt; bytes = br.ReadBytes((int)fs.Length).Cast&lt;int&gt;().ToList();
              var bytes = new List&lt;byte&gt;(br.ReadBytes((int) fs.Length).ToArray());
              return new HashSet&lt;byte&gt;(bytes);
          }
      }
  }
  </code></pre>