我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用cv2.calcBackProject()。
def deal(self,frame): frame=frame.copy() track_window=self.track_window term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) roi_hist=self.roi_hist dst = cv2.calcBackProject([frame],[0],roi_hist,[0,180],1) if self.m=='m': ret, track_window_r = cv2.meanShift(dst, track_window, term_crit) x,y,w,h = track_window_r img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2) elif self.m=='c': ret, track_window_r = cv2.CamShift(dst, track_window, term_crit) pts = cv2.boxPoints(ret) pts = np.int0(pts) img2 = cv2.polylines(frame,[pts],True, 255,2) rectsNew=[] center1=(track_window[0]+track_window[2]//2,track_window[1]+track_window[3]//2) center2=(track_window_r[0]+track_window_r[2]//2,track_window_r[1]+track_window_r[3]//2) img2 = cv2.line(img2,center1,center2,color=0) rectsNew=track_window_r # x,y,w,h = track_window # img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2) cv2.imshow('img2',img2) cv2.waitKey(0) cv2.destroyAllWindows() return rectsNew
def hand_threshold(frame_in,hand_hist): frame_in=cv2.medianBlur(frame_in,3) hsv=cv2.cvtColor(frame_in,cv2.COLOR_BGR2HSV) hsv[0:int(cap_region_y_end*hsv.shape[0]),0:int(cap_region_x_begin*hsv.shape[1])]=0 # Right half screen only hsv[int(cap_region_y_end*hsv.shape[0]):hsv.shape[0],0:hsv.shape[1]]=0 back_projection = cv2.calcBackProject([hsv], [0,1],hand_hist, [00,180,0,256], 1) disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (morph_elem_size,morph_elem_size)) cv2.filter2D(back_projection, -1, disc, back_projection) back_projection=cv2.GaussianBlur(back_projection,(gaussian_ksize,gaussian_ksize), gaussian_sigma) back_projection=cv2.medianBlur(back_projection,median_ksize) ret, thresh = cv2.threshold(back_projection, hsv_thresh_lower, 255, 0) return thresh # 3. Find hand contour