def shonv(img): num_bins = 10 num_slices = 10 img = ip.normalize(img) # img = img.astype(float) # cast to float array # kx, ky = cv2.getDerivKernels(1,1,1) # kx = kx[::-1].transpose() / 2 # ky = ky / 2 # gx = cv2.filter2D(img, -1, kx) # gy = cv2.filter2D(img,-1,ky) gx = cv2.Sobel(img, cv2.CV_32F, 1, 0) gy = cv2.Sobel(img, cv2.CV_32F, 0, 1) azi = np.arctan2(gy,gx) azi[azi<0] += 2*np.pi zen = np.arctan(np.sqrt(gx*gx+gy*gy)) slice_size = img.shape[0] / num_slices hists = [] for i in range(0, num_slices): azi_slice = azi[i*slice_size:i*slice_size+slice_size-1, :] zen_slice = zen[i*slice_size:i*slice_size+slice_size-1, :] H, xedges, yedges = np.histogram2d(azi_slice.ravel(), zen_slice.ravel(), bins=num_bins) hists.append(np.hstack(H)) return np.hstack(hists)
def shog(img): ''' Horizontally sliced histograms of Oriented Gradients ''' img = ip.normalize(img) # img = img.astype(float) num_slices = 10 num_bins = 16 slice_size = img.shape[0] / num_slices hists = [] # kx, ky = cv2.getDerivKernels(1,1,1) # kx = kx[::-1].transpose() # gx = cv2.filter2D(img, -1, kx, ) # gy = cv2.filter2D(img,-1, ky, cv2.CV_32F) gx = cv2.Sobel(img, cv2.CV_32F, 1, 0) gy = cv2.Sobel(img, cv2.CV_32F, 0, 1) mag, ang = cv2.cartToPolar(gx, gy) bins = np.int32(num_bins*ang/(2*np.pi)) for i in range(0, num_slices): bin_slice = bins[i*slice_size:i*slice_size+slice_size-1, :] mag_slice = mag[i*slice_size:i*slice_size+slice_size-1, :] hist = np.bincount(bin_slice.ravel(), mag_slice.ravel(), num_bins) hists.append(hist) # hists.append(hog(img[i*slice_size:i*slice_size+slice_size-1, :])) return np.hstack(hists)
def chog(img): ''' Hand Contour Based Histograms of Oriented Gradients ''' img = ip.normalize(img) num_contours = 10 num_bins = 16 con = ip.hand_contour(img, num_contours) features = np.array([]) mid_x = img.shape[1] / 2 mid_y = img.shape[0] / 2 for i in range(1,num_contours+1): con_img = img.copy() con_img[con!=i] = 0 gx = cv2.Sobel(con_img, cv2.CV_32F, 1, 0) gy = cv2.Sobel(con_img, cv2.CV_32F, 0, 1) mag, ang = cv2.cartToPolar(gx, gy) bins = np.int32(num_bins*ang/(2*np.pi)) bin_cells = bins[:mid_y,:mid_x], bins[mid_y:,:mid_x], bins[:mid_y,mid_x:], bins[mid_y: mid_x:] mag_cells = mag[:mid_y,:mid_x], mag[mid_y:,:mid_x], mag[:mid_y,mid_x:], mag[mid_y: mid_x:] hists = [np.bincount(b.ravel(), m.ravel(), num_bins) for b, m in zip(bin_cells, mag_cells)] features = np.concatenate((features, np.hstack(hists))) return features
def hod(img): ''' Contour based Histograms of Depth Data ''' img = ip.normalize(img) num_contours = 10 # number of contours to be used for each hand num_bins = 20 # number of depth bins for the histograms con = ip.hand_contour(img, num_contours) features = np.array([]) # initialize features for i in range(1,num_contours+1): # get features for each contour hist = np.histogram(img[con==i], bins=num_bins, range=(0, 255)) # get hist of depth values for contour features = np.concatenate((features, hist[0])) # update features return features
def show(self, wait=0): grey_val = 40 try: l, r = self.get_hands() l = ip.normalize(l) r = ip.normalize(r) grey = self.get_greyscale() grey_mask = np.ones( (grey.shape[0], grey.shape[1], 3), 'uint8' ) * 40 l_x1 = self._boxes['left'][0] l_y1 = self._boxes['left'][1] l_x2 = self._boxes['left'][2] l_y2 = self._boxes['left'][3] r_x1 = self._boxes['right'][0] r_y1 = self._boxes['right'][1] r_x2 = self._boxes['right'][2] r_y2 = self._boxes['right'][3] l[l==0] = 40 r[r==0] = 40 grey_mask[l_y1:l_y2, l_x1:l_x2, 1] = l grey_mask[r_y1:r_y2, r_x1:r_x2, 0] = r if self._predicted != None: if self._predicted[0] != self._label[0]: l_color = np.zeros( (l.shape[0], l.shape[1], 3 ), 'uint8' ) l_color[:,:,2] = l l = l_color else: l_color = np.zeros( (l.shape[0], l.shape[1], 3 ), 'uint8' ) l_color[:,:,1] = l l = l_color if self._predicted[1] != self._label[1]: r_color = np.zeros( (r.shape[0], r.shape[1], 3 ), 'uint8' ) r_color[:,:,2] = r r = r_color else: r_color = np.zeros( (r.shape[0], r.shape[1], 3 ), 'uint8' ) r_color[:,:,1] = r r = r_color if l.any(): cv2.imshow('Left Hand', l) cv2.moveWindow('Left Hand', 900, 0) if r.any(): cv2.imshow('Right Hand', r) cv2.moveWindow('Right Hand', 700, 0) cv2.imshow('Segmented Hands', grey_mask) cv2.imshow('Depth Data', grey) cv2.waitKey(wait) except KeyboardInterrupt: sys.exit(0) except Exception as e: print e
def get_greyscale(self): if self._type == 'grey': return self._data else: return ip.normalize(self._data)