def extract(img):
return np.ravel(cv2.calcHist([img],[1],None,[126],[0,256]))
python类calcHist()的实例源码
def extract(img):
return np.ravel(cv2.calcHist([img],[2],None,[126],[0,256]))
def extract(img):
return np.ravel(cv2.calcHist([img],[2],None,[127],[0,256]))
def extract(img):
return np.ravel(cv2.calcHist([img],[2],None,[126],[0,256]))
def BlockLBPH(img,minValue,maxValue,normed=True):
'''????????LBP?????'''
#?????bin???
histSize=[maxValue-minValue+1]
ranges=[minValue,maxValue+1]
result=cv2.calcHist(img,[0],None,histSize,ranges)
#???
if normed:
result=result/(int)(img.shape[0]*img.shape[1])
return result.reshape(1,-1)
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_roi_hist(self, faces_rects, frame):
faces_roi_hists = []
for (x, y, w, h) in faces_rects:
roi = frame[y:y+h, x:x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0], mask, [180], [0,180])
roi_hist = cv2.normalize(roi_hist,roi_hist, 0, 255, cv2.NORM_MINMAX)
faces_roi_hists.append(roi_hist)
return faces_roi_hists
def getLBP(img):
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
radius = 1
n_points = 8 * radius
lbpImage = (local_binary_pattern(img2, n_points, radius)).astype(int)**(1.0/radius)
# block processing:
lbpImages = block_view(lbpImage, ( int(lbpImage.shape[0] / 2), int(lbpImage.shape[1] / 4)))
count = 0
LBP = np.array([]);
for i in range(lbpImages.shape[0]): # for each block:
for j in range(lbpImages.shape[1]):
count += 1
# plt.subplot(4,2,count)
# plt.imshow(lbpImages[i,j,:,:],cmap = cm.Greys_r)
# plt.subplot(4,2,count+4*2/2)
# print count*2+1
LBPt = cv2.calcHist([lbpImages[i,j,:,:].astype('uint8')], [0], None, [8], [0, 256])
LBP = np.append(LBP, LBPt[:,0]);
# plt.plot(LBPt)
# plt.show()
Fnames = ["LBP"+str(i).zfill(2) for i in range(len(LBP))]
return normalize(LBP).tolist(), Fnames
def calc_hist(image):
mask = cv2.inRange(image, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
hist = cv2.calcHist([image],[0],mask,[180],[0,180])
#hist = cv2.calcHist(image,[0,1],None,[10,10],[0,180,0,255])
cv2.normalize(hist,hist,0,1,norm_type=cv2.NORM_MINMAX)
return hist
def histogram(self, image, mask=None):
#Extract a 3D color histogram from the masked region of the image, using
#the supplied number of bins per channel, then normalize the histogram
new_im = imcrop(image)
hist = cv2.calcHist([new_im], [0, 1, 2], mask, self.bins,[0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist).flatten()
return hist
def get_histogram(image, histogram_scale):
h = cv2.calcHist([image], [0], None, [histogram_scale], [0, 256]).flatten()
return h/256