Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:33:47+00:00 2026-06-04T04:33:47+00:00

I am looking for an Earth Mover’s distance(or Fast EMD) implementation in python. Any

  • 0

I am looking for an Earth Mover’s distance(or Fast EMD) implementation in python.
Any clues on where to find it, I have looked enough on the web.
I want to use it in an image retrieval project that I am doing.
Thanks.

EDIT:
I found a very nice solution using the pulp libararies.
This page also has the instruction required to set it up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:33:48+00:00Added an answer on June 4, 2026 at 4:33 am

    There is an excellent implementation in OpenCv for Python. The name of the function is CalcEMD2 and a simple code to compare histograms of two images would look like this:

    #Import OpenCv library
    from cv2 import *
    
    ### HISTOGRAM FUNCTION #########################################################
    def calcHistogram(src):
        # Convert to HSV
        hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
        cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    
        # Extract the H and S planes
        size = cv.GetSize(src)
        h_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
        s_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
        cv.Split(hsv, h_plane, s_plane, None, None)
        planes = [h_plane, s_plane]
    
        #Define numer of bins
        h_bins = 30
        s_bins = 32
    
        #Define histogram size
        hist_size = [h_bins, s_bins]
    
        # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
        h_ranges = [0, 180]
    
        # saturation varies from 0 (black-gray-white) to 255 (pure spectrum color)
        s_ranges = [0, 255]
    
        ranges = [h_ranges, s_ranges]
    
        #Create histogram
        hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    
        #Calc histogram
        cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    
        cv.NormalizeHist(hist, 1.0)
    
        #Return histogram
        return hist
    
    ### EARTH MOVERS ############################################################
    def calcEM(hist1,hist2,h_bins,s_bins):
    
        #Define number of rows
        numRows = h_bins*s_bins
    
        sig1 = cv.CreateMat(numRows, 3, cv.CV_32FC1)
        sig2 = cv.CreateMat(numRows, 3, cv.CV_32FC1)    
    
        for h in range(h_bins):
            for s in range(s_bins): 
                bin_val = cv.QueryHistValue_2D(hist1, h, s)
                cv.Set2D(sig1, h*s_bins+s, 0, cv.Scalar(bin_val))
                cv.Set2D(sig1, h*s_bins+s, 1, cv.Scalar(h))
                cv.Set2D(sig1, h*s_bins+s, 2, cv.Scalar(s))
    
                bin_val = cv.QueryHistValue_2D(hist2, h, s)
                cv.Set2D(sig2, h*s_bins+s, 0, cv.Scalar(bin_val))
                cv.Set2D(sig2, h*s_bins+s, 1, cv.Scalar(h))
                cv.Set2D(sig2, h*s_bins+s, 2, cv.Scalar(s))
    
        #This is the important line were the OpenCV EM algorithm is called
        return cv.CalcEMD2(sig1,sig2,cv.CV_DIST_L2)
    
    ### MAIN ########################################################################
    if __name__=="__main__":
        #Load image 1
        src1 = cv.LoadImage("image1.jpg")
    
        #Load image 1
        src2 = cv.LoadImage("image2.jpg")
    
        # Get histograms
        histSrc1= calcHistogram(src1)
        histSrc2= calcHistogram(src2)
    
        # Compare histograms using earth mover's
        histComp = calcEM(histSrc1,histSrc2,30,32)
    
        #Print solution
        print(histComp)
    

    I tested a code very similar to the previous code with Python 2.7 and Python(x,y). If you want to learn more about Earth Mover’s and you want to see an implementation using OpenCV and C++, you can read “Chapter 7: Histograms an Matching” of the book “Learning OpenCV” by Gary Bradski & Adrain Kaebler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for java code (or a library) that calculates the earth mover's distance
I am looking for some kind of earth model that at run time can
I am looking to build a static KML (Google Earth markup) file which displays
I am looking for an example that renders an interactive earth similar to the
I'm looking for an implementation of the LOGO programming language that supports 'dynaturtles' -
I am looking to develop a commercial game using the sky map (google earth)
I have a simple infinite for loop looking like this: set_time_limit (0); for (;;)
Looking at Facebook I have noticed that images vary in size from 100x100 to
I'm looking for something similar to google maps' LatLngBounds in Google Earth API ,
Looking for a good set of base objects to start a website up in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.