Buying Find me the fastest Python image difference method

Discussion in 'Programming' started by EricBruggema, May 3, 2014.

  1. #1
    Hi there,

    I'm looking for someone that can create a 'function' (def) in Python! this function must calculate the difference between 2 images, it must return the difference in percents! (0 - 100).

    As i'm no expert, i cannot find the fastest method available and realy want to add it to my motion detection video & timelapse script for my Raspberry Pi project.

    I'm offering $10.- for the fastest function available! You can use anything, if i need to install any python libraries its no problem. Current method processes an image in 0.20th of a second (8x8px) and is 90% correct.

    Like te job? just post the code here, who's the fastest with posting the fastest code will receive my $10 by paypal.

    Current function i'm using:
    
    def avg(values):
      if type(values) == type(1):
          return values
      return sum(values)//len(values)
    
    def hamming_distance(a_list, b_list):
      """Return the hamming distance calculated from list a and b"""
      return sum(item1 != item2 for item1, item2 in zip(a_list, b_list))
    
    def num_pairs(num_items):
      """Return the number of pairs that can be drawn from a list of num_items items."""
      return num_items * (num_items - 1) / 2
    
    def compareLoadImage(image):
      """Load image for comparing, resize and grayscale"""
      image1 = image.resize((motionShotWidth, motionShotHeight), Image.BILINEAR)
      grayscale_pixels = map(avg, list(image1.getdata()))
      pixel_avg = avg(grayscale_pixels)
      return [(pixel > pixel_avg) for pixel in grayscale_pixels]
    
    def compare(image1, image2): 
      """Return the hamming distance between to loaded, resized and getting grayscaled pixel data"""
      return ((64 - hamming_distance(compareLoadImage(image1), compareLoadImage(image2))) * 100) // 64
    
    Code (markup):
    0.20s

    Hope someone will get me a faster method, usable writen!
     
    EricBruggema, May 3, 2014 IP
  2. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    When comparing images break each image down into sub-images and do the same comparisons you're doing in parallel.

    so if you have image1: 64x64 pixels and image2: 64x64 pixels.

    If you have 4 processors/cores you'd spawn 4 processors/threads..
    Then break each image down into 4 images.

    image1-1: 16x16 pixels image2-1: 16x16 pixels
    image1-2: 16x16 pixels image2-2: 16x16 pixels
    image1-3: 16x16 pixels image2-3: 16x16 pixels
    image1-4: 16x16 pixels image2-4: 16x16 pixels

    Then spawn 4 processes/threads.

    process1( compare(image1-1, image2-1) );
    process2( compare(image1-2, image2-2) );
    process3( compare(image1-3, image2-3) );
    process4( compare(image1-4, image2-4) );

    This specific example should give you a theoretical max speedup of 4x. So, if originally it took .2 seconds to compare image1 and image2, it could now take .2/4 = .05 seconds. Now, there will be a little overhead for spawning processes so you might see something like .05seconds -.08 seconds? All depends.
     
    sn3twork, May 3, 2014 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    I use the Raspberry Pi, only one core ;) and the images are fetched from the picamera in real time
     
    EricBruggema, May 3, 2014 IP
  4. sn3twork

    sn3twork Active Member

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Do the differences need to be computed in real time?

    The overall idea is you need reduce the amount of data you are processing.

    I'd say with your lower powered hardware use lower sample rate from your camera... I assume this will solve your application's overall problem of needing a faster compare function, because if you're comparing less it's effectively the same thing... so grab less images per second, so that you have to do less compares.
    Or you can sample from your camera at the same rate that you are now, but sample at a lower resolution.
     
    sn3twork, May 4, 2014 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    Yes, it has to be in real time, thats the main problem as the RPI has just a 700mhz processor, that isn't very fast!. At this point i'm just looking for the fastest method around, someone told me by using opencv (wich i already have installed) but how, is the big question! ;) others say PIL library. I just need someone that will help me find the fastest working method, possible! :)
     
    EricBruggema, May 4, 2014 IP