Python 2.7 - How to compare two image? -
in python 2.7, want compare 2 image same, how this? please show me step step. thanks!
there many ways do. using opensource library, opencv, scikit learn, tensorflow.
to compare 2 images, can template matching in opencv
import cv2 import numpy np matplotlib import pyplot plt img = cv2.imread('img.jpg', 0) img2 = img.copy() template = cv2.imread('img2.jpg', 0) w, h = template.shape[::-1] methods = ['cv2.tm_ccoeff', 'cv2.tm_ccoeff_normed'] meth in methods: img = img2.copy() method = eval(meth) res = cv2.matchtemplate(img, template, method) min_val, max_val, min_loc, max_loc = cv2.minmaxloc(res) if method in [cv2.tm_sqdiff or cv2. tm_sqdiff_normed]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img, top_left, bottom_right, 255,2) plt.subplot(121), plt.imshow(res) plt.title('matching result'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(img,cmap = 'gray') plt.title('detected point'), plt.xticks([]), plt.yticks([]) plt.suptitle(meth) plt.show()
import cv2 import numpy np base = cv2.imread('test4.jpg') test1 = cv2.imread('test3.jpg') test2 = cv2.imread('test5.jpg') rows,cols = base.shape[:2] basehsv = cv2.cvtcolor(base,cv2.color_bgr2hsv) test1hsv = cv2.cvtcolor(test1,cv2.color_bgr2hsv) test2hsv = cv2.cvtcolor(test2,cv2.color_bgr2hsv) halfhsv = basehsv[rows/2:rows-1,cols/2:cols-1].copy() # take lower half of base image testing hbins = 180 sbins = 255 hrange = [0,180] srange = [0,256] ranges = hrange+srange # ranges = [0,180,0,256] ranges=none histbase = cv2.calchist(basehsv,[0,1],none,[180,256],ranges) cv2.normalize(histbase,histbase,0,255,cv2.norm_minmax) histhalf = cv2.calchist(halfhsv,[0,1],none,[180,256],ranges) cv2.normalize(histhalf,histhalf,0,255,cv2.norm_minmax) histtest1 = cv2.calchist(test1hsv,[0,1],none,[180,256],ranges) cv2.normalize(histtest1,histtest1,0,255,cv2.norm_minmax) histtest2 = cv2.calchist(test2hsv,[0,1],none,[180,256],ranges) cv2.normalize(histtest2,histtest2,0,255,cv2.norm_minmax) in xrange(5): base_base = cv2.comparehist(histbase,histbase,i) base_half = cv2.comparehist(histbase,histhalf,i) base_test1 = cv2.comparehist(histbase,histtest1,i) base_test2 = cv2.comparehist(histbase,histtest2,i) print "method: {0} -- base-base: {1} , base-test1: {2}, base_test2: {3}".format(i,base_base,base_test1,base_test2)
Comments
Post a Comment