image - Python code for Earth mover's Distance -


i looking earth mover's distance(or fast emd) implementation in python. clues on find it, have looked enough on web. want use in image retrieval project doing. thanks.

edit: found nice solution using pulp libararies. page has instruction required set up.

there excellent implementation in opencv python. name of function calcemd2 , simple code compare histograms of 2 images this:

#import opencv library cv2 import *  ### histogram function ######################################################### def calchistogram(src):     # convert hsv     hsv = cv.createimage(cv.getsize(src), 8, 3)     cv.cvtcolor(src, hsv, cv.cv_bgr2hsv)      # extract h , 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 0 (~0 deg red) 180 (~360 deg red again */     h_ranges = [0, 180]      # saturation varies 0 (black-gray-white) 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) 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)      h in range(h_bins):     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 important line opencv em algorithm 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")      # 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 code similar previous code python 2.7 , python(x,y). if want learn more earth mover's , want see implementation using opencv , c++, can read "chapter 7: histograms matching" of book "learning opencv" gary bradski & adrain kaebler.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -