def match_hists(hists):
matches = {}
idx = 0
for i in range(len(hists)):
for j in range(len(hists[i])):
matches['{}_{}'.format(i,j)] = idx
idx += 1
for i in range(len(hists)):
for j in range(i+1, len(hists)):
for k in range(len(hists[i])):
corrs = []
for l in range(len(hists[j])):
corr = cv2.compareHist(hists[i][k], hists[j][l], cv2.cv.CV_COMP_INTERSECT)
corrs.append(corr)
corrs = np.array(corrs)
idxs = np.argsort(corrs)
if corrs[idxs[-1]] > 0.80:
matches['{}_{}'.format(j, idxs[-1])] = matches['{}_{}'.format(i, k)]
return matches
python类compareHist()的实例源码
def predict(img):
'''??????????'''
if len(_histograms)==0:
print 'model is not build'
return
img=cv2.resize(img,(48,48))
lbP_img=recognition.CircularLBP(img,radius=_radius,neighbors=_neighbors)
lbph_pre=recognition.LBPH(lbP_img,int(math.pow(2,_neighbors)),grid_x=_grid_x,grid_y=_grid_y)
minDist=sys.float_info.max
minClass=-1
for index in range(len(_histograms)):
dist=cv2.compareHist(_histograms[index],lbph_pre,cv2.HISTCMP_CHISQR)
if dist<minDist:
minDist=dist
minClass=_labels[index]
print 'label:%d distance:%f'%(minClass,minDist)
return minClass,minDist,lbP_img,lbph_pre
def main():
target_im = cv2.imread(sys.argv[1])
target_hist = cv2.calcHist([target_im], [0], None, [256], [0, 256])
for i in list:
comparing_im = cv2.imread(i)
comparing_hist = cv2.calcHist([comparing_im], [0], None, [256], [0, 256])
diff = cv2.compareHist(target_hist, comparing_hist, 0)
if diff > float(sys.argv[2]):
print i,
print diff
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_CORREL)
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_CHISQR)
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_INTERSECT)
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_HELLINGER)
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_BHATTACHARYYA)
def compare(self, h1, h2):
return cv2.compareHist(h1, h2, cv2.HISTCMP_KL_DIV)
def are_similar(self, first, second):
result = 0
for i in xrange(3):
hist1 = cv2.calcHist([first], [i], None, [256], [0,256])
hist2 = cv2.calcHist([second], [i], None, [256], [0,256])
result += cv2.compareHist(hist1, hist2, self.get_technique())
return result / 3
def matches(self,frame,rects):
cal=lambda x:Match.calHist(frame,x)
hists=list(map(cal,rects))
print(list(map(lambda x:cv2.compareHist(x,self.hist,cv2.HISTCMP_BHATTACHARYYA),hists)))
def gray_histogram_cmp_corr(self):
self.threshold = .9
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [0, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def gray_histogram_cmp_bhatta(self):
self.threshold = .07
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [1, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [1, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_BHATTACHARYYA)
self.assertGreater(self.measure, self.threshold)
def gray_histogram_cmp_corr(self):
self.threshold = .9
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [0, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def gray_histogram_cmp_bhatta(self):
threshold = .07
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [1, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [1, 256])
measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_BHATTACHARYYA)
self.assertGreater(measure, threshold)
def Harris_Corner(self):
self.threshold = 0.999999999999
temp_i = self.image_i.copy()
temp1_i = self.image_i.copy()
gray_i = cv2.cvtColor(temp_i, cv2.COLOR_BGR2GRAY)
gray_i = numpy.float32(gray_i)
dst_i = cv2.cornerHarris(gray_i,2,3,0.025)
dst_i = cv2.dilate(dst_i,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_i[dst_i<0.01*dst_i.max()]=[0,0,0]
temp_i[dst_i>=0.01*dst_i.max()]=[255,255,255]
temp1_i[dst_i>0.01*dst_i.max()]=[0,0,255]
hist_i = cv2.calcHist([temp_i], [0], None, [256], [0, 256])
temp_j = self.image_j.copy()
temp1_j = self.image_j.copy()
gray_j = cv2.cvtColor(temp_j, cv2.COLOR_BGR2GRAY)
gray_j = numpy.float32(gray_j)
dst_j = cv2.cornerHarris(gray_j,2,3,0.025)
dst_j = cv2.dilate(dst_j,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_j[dst_j<0.01*dst_j.max()]=[0,0,0]
temp_j[dst_j>=0.01*dst_j.max()]=[255,255,255]
temp1_j[dst_j>0.01*dst_j.max()]=[0,0,255]
hist_j = cv2.calcHist([temp_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
print self.measure
cv2.imshow('Input X',temp1_i)
cv2.waitKey(0)
cv2.imshow('Input Y',temp1_j)
cv2.waitKey(0)
def Canny_edge(self):
self.threshold = .999999999999999
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
edges_i = cv2.Canny(gray_i,100,200)
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
edges_j = cv2.Canny(gray_j,100,200)
hist_i = cv2.calcHist([edges_i], [0], None, [256], [0, 256])
hist_j = cv2.calcHist([edges_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
print self.measure
# cv2.imshow(self.image_i)
# cv2.imshow(self.image_j)
def gray_histogram_cmp_corr(self):
self.threshold = .9
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [0, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def gray_histogram_cmp_bhatta(self):
self.threshold = .07
gray_i = cv2.cvtColor(self.image_i, cv2.COLOR_BGR2GRAY)
hist_i = cv2.calcHist([gray_i], [0], None, [256], [1, 256])
gray_j = cv2.cvtColor(self.image_j, cv2.COLOR_BGR2GRAY)
hist_j = cv2.calcHist([gray_j], [0], None, [256], [1, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_BHATTACHARYYA)
self.assertGreater(self.measure, self.threshold)
def Harris_Corner(self):
self.threshold = 0.999999999999
temp_i = self.image_i.copy()
temp1_i = self.image_i.copy()
gray_i = cv2.cvtColor(temp_i, cv2.COLOR_BGR2GRAY)
gray_i = numpy.float32(gray_i)
dst_i = cv2.cornerHarris(gray_i,2,3,0.025)
dst_i = cv2.dilate(dst_i,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_i[dst_i<0.01*dst_i.max()]=[0,0,0]
temp1_i[dst_i>0.01*dst_i.max()]=[0,0,255]
hist_i = cv2.calcHist([temp_i], [0], None, [256], [0, 256])
temp_j = self.image_j.copy()
temp1_j = self.image_j.copy()
gray_j = cv2.cvtColor(temp_j, cv2.COLOR_BGR2GRAY)
gray_j = numpy.float32(gray_j)
dst_j = cv2.cornerHarris(gray_j,2,3,0.025)
dst_j = cv2.dilate(dst_j,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_j[dst_j<0.01*dst_j.max()]=[0,0,0]
temp1_j[dst_j>0.01*dst_j.max()]=[0,0,255]
hist_j = cv2.calcHist([temp_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def rgb_histogram(self):
self.threshold = 0.999999999999999
hist_i = cv2.calcHist([self.image_i], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist_j = cv2.calcHist([self.image_j], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist_flatten_i = hist_i.flatten()
hist_flatten_j = hist_j.flatten()
self.measure = cv2.compareHist(hist_flatten_i, hist_flatten_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def __get_uniq_faces_curr_frame(self, frame_id, faces_roi_hists_prev, faces_roi_hists):
faces_prev = len(faces_roi_hists_prev)
faces_curr = len(faces_roi_hists)
logger.info("[{0}] Face Similarity: Prev: {1}, Curr: {2}".format(frame_id,
faces_prev,
faces_curr))
# First Time
if faces_prev == 0:
return faces_curr
# Current frame has more faces than prev frame
#if faces_curr > faces_prev:
# return faces_curr - faces_prev
uniq_faces_curr_frame = 0
# Perform Image Histogram Similarity
# For each histogram in current frame
for rh1 in faces_roi_hists:
match_found = False
# For each histogram in previous frame
for rh2 in faces_roi_hists_prev:
#print "\nrh1 {0}: {1}".format(type(rh1),np.shape(rh1))
#print "\nrh2 {0}: {1}".format(type(rh2),np.shape(rh2))
corr = cv2.compareHist(rh1, rh2, cv2.HISTCMP_CORREL)
logger.info("[{0}] Similarity Metrics: {1}".format(frame_id, corr))
if corr >= 0.35:
# Match Found, can break the loop for this histogram in current frame
match_found = True
break;
# Add to unique face count, if no match found for this histogram in current frame
if not match_found:
uniq_faces_curr_frame += 1
logger.info("[{0}] Total Unique Faces in Current Frame: {1}".format(frame_id, uniq_faces_curr_frame))
return uniq_faces_curr_frame