Results 1 to 7 of 7

Thread: Reverse Image Searching (Perceptual Hashing)

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Reverse Image Searching (Perceptual Hashing)

    A while back i tinkered with this idea to create an easy to use perceptual hashing module
    that does the work for me to match one or more images with eachother.

    If you wonder what this is about then i ask you, have you ever used google image to look for a
    particulair image and you notice that google adds up similair images to your search.
    This is how they do it, ofcourse google has a more advanced way of doing this however i was certain
    that we can replicate this reverse image searching in vb.

    More details:
    http://en.wikipedia.org/wiki/Perceptual_hashing

    The way this is working is as follows, we take an image and we reduce the size, we prefer a small
    size simply because of speed, i tested ranges between 8x8 and 32x32 and it works well.

    Then we reduce the colors and take the average of those values, we form a sequence of this.
    Now lets say we have two images as follows:


    ImageA (3x3) = [111000111]

    ImageB (3x3) = [101010111]



    ImageA and ImageB have a difference of 2, by matching each value we count the differences and this is our value that tells us if theres a large difference or not. So the lower the better we have a match.

    I made rough test application and i made it work, best way to get fast results was using a threadpool.

    Name:  2LC8sx1.png
Views: 1064
Size:  15.6 KB

    I used the size of 32x32 with a threshold difference of 30, as you can see it did match two images
    that look similair to eachother.

    How to use this module:

    Code:
    Dim Threshold as Integer = 30
    Dim Dimension As new Size(32,32)
    Dim FileA As Integer() = Tools.Compute(Image.FromFile(FilenameA), Dimension.Width, Dimension.Height)
    Dim FileB As Integer() = Tools.Compute(Image.FromFile(FilenameB), Dimension.Width, Dimension.Height)
    
     If (Tools.Difference(FileA , FileB) <= Threshold) Then
               '// We have a good match...do something with it.
     End If

    Module:
    Code:
    Imports System.Drawing
    Public Class Tools
        '// //////////////////////////////////////////////////////////////
        '// Compute Difference
        Public Shared Function Difference(x As Integer(), y As Integer()) As Integer
            If (x.Length <> y.Length) Then Throw New Exception("Sequence length mismatch")
            Dim dvalue As Integer = 0, i As Integer
            For i = 0 To x.Length - 1
                If x(i) <> y(i) Then dvalue += 1
            Next
            Return dvalue
        End Function
        '// //////////////////////////////////////////////////////////////
        '// Compute Hash
        Public Shared Function Compute(image As Image, w As Integer, h As Integer) As Integer()
            Dim Values As Integer() = Tools.ReduceColor(Tools.ReduceSize(image, w, h))
            Return Tools.ToSequence(Values, Tools.Average(Values))
        End Function
        '// //////////////////////////////////////////////////////////////
        '// Reduce size
        Public Shared Function ReduceSize(image As Image, w As Integer, h As Integer) As Image
            Return image.GetThumbnailImage(w, h, AddressOf Tools.Callback, IntPtr.Zero)
        End Function
        Private Shared Function Callback() As Boolean
            Return False
        End Function
        '// //////////////////////////////////////////////////////////////
        '// Reduce color
        Public Shared Function ReduceColor(image As Image) As Integer()
            Using bm As New Bitmap(image)
                Dim Values As Integer() = New Integer(image.Width * image.Height - 1) {}
                For x As Integer = 0 To image.Width - 1
                    For y As Integer = 0 To image.Height - 1
                        Dim col As Color = bm.GetPixel(x, y)
                        Dim v As Integer = (col.R * 30 + col.G * 59 + col.B * 11) \ 100
                        Values(x * image.Width + y) = v
                    Next
                Next
                Return Values
            End Using
        End Function
        '// //////////////////////////////////////////////////////////////
        '// Average
        Public Shared Function Average(Values As Integer()) As Integer
            Dim Sum As Integer, i As Integer
            For i = 0 To Values.Length - 1
                Sum += Convert.ToInt32(Values(i))
            Next
            Return Sum \ Values.Length
        End Function
        '// //////////////////////////////////////////////////////////////
        '// Hash sequence
        Public Shared Function ToSequence(values As Integer(), average As Integer) As Integer()
            Dim sequence As Integer() = New Integer(values.Length - 1) {}
            For i As Integer = 0 To values.Length - 1
                sequence(i) = Convert.ToInt32(If(values(i) < average, 0, 1))
            Next
            Return sequence
        End Function
    End Class

    My code might not be as optimized or well commented as some like me to be...(im a slovenly coder :P)

    Anyway, i hope it helps you spark an idea out of this, comments or qeustions welcome

    - Barret
    Last edited by TheBarret; May 9th, 2015 at 09:06 AM.

  2. #2
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Reverse Image Searching (Perceptual Hashing)

    Woah... Barret, you can program anything! How about my idea?

    It's an image encoder/decoder, based off of base64, but using colored pixels in place of the base64's characters. The file size would be approximately 3 times smaller than if it was encoded in base64's characters, from what I calculated a week back.

    I've been wanting to give this idea a try, but I haven't been able to find the base64 algorithm in VB.NET that I could play with.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Reverse Image Searching (Perceptual Hashing)

    Quote Originally Posted by Peter Porter View Post
    Woah... Barret, you can program anything! How about my idea?

    It's an image encoder/decoder, based off of base64, but using colored pixels in place of the base64's characters. The file size would be approximately 3 times smaller than if it was encoded in base64's characters, from what I calculated a week back.

    I've been wanting to give this idea a try, but I haven't been able to find the base64 algorithm in VB.NET that I could play with.
    Ive done some random things, i really liked that Qr-code method, u know those square rectangles that contain in fact several smaller fields that have a correction field, so even if the qr-code is partially damaged or un-readable this field will correct itself.
    Those only use black and white, with colors we have a huge range of values we can utilize. Now ive done some searching, and there is even a programming language that consists of colors, its brilliant piece of software, its called Piet, refered as Piet Mondrian.

    http://en.wikipedia.org/wiki/Esoteri..._language#Piet

    Im pretty sure we can do something intresting

  4. #4
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Reverse Image Searching (Perceptual Hashing)

    I haven't had any luck finding the algorithm, but I'm sure it probably be way over my head.

    I've coded a Base64 decoder (Convert.FromBase64String), and I'm sure I can code and encoder. How about if we keep it simple at first, like having an image encoded to based64 characters, then translated into set colored pixels. For decoding, the pixels would be translated back into Base64 characters, then decoded into an image.

  5. #5
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Reverse Image Searching (Perceptual Hashing)

    Quote Originally Posted by TheBarret View Post
    Anyway, i hope it helps you spark an idea out of this, comments or qeustions welcome
    Is it possible to do a reverse image search of a drawn portrait?

    It's funny, but up until you posted your code, I never heard of reverse image searching. Didn't know Google offered this service.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Reverse Image Searching (Perceptual Hashing)

    Quote Originally Posted by Peter Porter View Post
    I haven't had any luck finding the algorithm, but I'm sure it probably be way over my head.

    I've coded a Base64 decoder (Convert.FromBase64String), and I'm sure I can code and encoder. How about if we keep it simple at first, like having an image encoded to based64 characters, then translated into set colored pixels. For decoding, the pixels would be translated back into Base64 characters, then decoded into an image.
    i have sent you a source of a project ive done a while ago, maybe it helps your idea to conceive a plan of attack.

  7. #7
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Reverse Image Searching (Perceptual Hashing)

    Quote Originally Posted by TheBarret View Post
    i have sent you a source of a project ive done a while ago, maybe it helps your idea to conceive a plan of attack.
    Thanks, Barret, but the link doesn't work for me.

    I'll try to code this encoder, and start a new thread as soon as I have something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width