def stereoSGBM(self, img_left, img_right):
stereo = cv2.StereoSGBM(minDisparity=0, numDisparities=self._max_disparity, SADWindowSize=self._sad_winsize, uniquenessRatio=self._uniqueness_ratio, P1=self._P1, P2=self._P2, speckleWindowSize=self._speckle_winsize, speckleRange=self._speckle_range)
disp_left = stereo.compute(img_left, img_right)
disp_left_visual = np.zeros((img_left.shape[0], img_left.shape[1]), np.uint8)
disp_left_visual = cv2.normalize(disp_left, alpha=0, beta=self._max_disparity, norm_type=cv2.cv.CV_MINMAX, dtype=cv2.cv.CV_8U)
img_foreground, disp_foreground = self.extractForeground(disp_left_visual, img_left, self._fgnd_disp_thresh)
return (disp_left_visual, disp_foreground, img_foreground)
python类normalize()的实例源码
HandRecognition.py 文件源码
项目:hand-gesture-recognition-opencv
作者: mahaveerverma
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def hand_capture(frame_in,box_x,box_y):
hsv = cv2.cvtColor(frame_in, cv2.COLOR_BGR2HSV)
ROI = np.zeros([capture_box_dim*capture_box_count,capture_box_dim,3], dtype=hsv.dtype)
for i in xrange(capture_box_count):
ROI[i*capture_box_dim:i*capture_box_dim+capture_box_dim,0:capture_box_dim] = hsv[box_y[i]:box_y[i]+capture_box_dim,box_x[i]:box_x[i]+capture_box_dim]
hand_hist = cv2.calcHist([ROI],[0, 1], None, [180, 256], [0, 180, 0, 256])
cv2.normalize(hand_hist,hand_hist, 0, 255, cv2.NORM_MINMAX)
return hand_hist
# 2. Filters and threshold
def compute_hist(im):
hist = cv2.calcHist([im], [0, 1, 2], None, [8, 8, 8],
[0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist).flatten()
return hist
def __init__(self,frame,rect,method='m'):
r,c,h,w=rect
roi = frame[r:r+h, c:c+w]
mask = cv2.inRange(roi, np.array((0.)), np.array((255.)))
roi_hist = cv2.calcHist([roi],[0],mask,[16],[0,255])
roi_hist=cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
plotRects(frame,[rect])
cv2.waitKey(0)
cv2.destroyAllWindows()
self.roi_hist=roi_hist
self.track_window=tuple(rect)
self.m=method
self.frame=frame
def calHist(frame,rect):
r,c,h,w=rect
roi = frame[r:r+h, c:c+w]
mask = cv2.inRange(roi, np.array((0.)), np.array((255.)))
roi_hist = cv2.calcHist([roi],[0],mask,[255],[0,255])
roi_hist=cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
return roi_hist
def simplest_cb(img, percent):
assert img.shape[2] == 3
assert percent > 0 and percent < 100
half_percent = percent / 200.0
channels = cv2.split(img)
out_channels = []
for channel in channels:
assert len(channel.shape) == 2
# find the low and high precentile values (based on the input percentile)
height, width = channel.shape
vec_size = width * height
flat = channel.reshape(vec_size)
assert len(flat.shape) == 1
flat = np.sort(flat)
n_cols = flat.shape[0]
low_val = flat[math.floor(n_cols * half_percent)]
high_val = flat[math.ceil( n_cols * (1.0 - half_percent))]
print "Lowval: ", low_val
print "Highval: ", high_val
# saturate below the low percentile and above the high percentile
thresholded = apply_threshold(channel, low_val, high_val)
# scale the channel
normalized = cv2.normalize(thresholded, thresholded.copy(), 0, 255, cv2.NORM_MINMAX)
out_channels.append(normalized)
return cv2.merge(out_channels)
def hist_lines(image, start, end):
scale = 4
height = 1080
result = np.zeros((height, 256 * scale, 1))
hist = cv2.calcHist([image], [0], None, [256], [start, end])
cv2.normalize(hist, hist, 0, height, cv2.NORM_MINMAX)
hist = np.int32(np.around(hist))
for x, y in enumerate(hist):
cv2.rectangle(result, (x * scale, 0), ((x + 1) * scale, y), (255), -1)
result = np.flipud(result)
return result
def NDIimage(self, arg_debug= False):
G= self.imgRGB_G.astype('float')
R= self.imgRGB_R.astype('float')
NDIimage= 128*((G-R)/(G+R)+1)
NDIimage= cv2.normalize(NDIimage, NDIimage, 0, 255, cv2.NORM_MINMAX)
if arg_debug:
cv2.imwrite('Debug/debug_NDIimage.jpg', NDIimage)
return NDIimage
def ExGimage(self, arg_debug= False):
print 'ExGimage'
R_star= self.imgRGB_R.astype('float')/255
G_star= self.imgRGB_G.astype('float')/255
B_star= self.imgRGB_B.astype('float')/255
ExGimage= (2*G_star-R_star- B_star)/(G_star+ B_star+ R_star)
ExGimage= cv2.normalize(ExGimage, ExGimage, 0, 255, cv2.NORM_MINMAX)
if arg_debug:
cv2.imwrite('Debug/debug_ExGimage.jpg', ExGimage)
#print ExGimage
return ExGimage
def process_frame(self):
super().process_frame()
if self.cur_frame_number == self.ground_truth_frame_numbers[self.gt_frame_ix]:
# we have struck upon a frame we can evaluate against ground truth
gt_file_path = os.path.join(self.ground_truth_folder, self.ground_truth_frame_filenames[self.gt_frame_ix])
gt_mask = cv2.imread(gt_file_path, cv2.IMREAD_GRAYSCALE)
self.gt_frame_ix += 1 # advance for next hit
test_mask = self.mask.copy()
test_mask[test_mask < MaskLabel.PERSISTENCE_LABEL.value] = 0
test_mask[test_mask >= MaskLabel.PERSISTENCE_LABEL.value] = 1
gt_mask[gt_mask == 255] = 1
test_mask = test_mask.astype(np.int8) # to allow subtraction
errors = test_mask - gt_mask
false_positives = errors.copy()
false_negatives = errors.copy()
false_positives[false_positives == -1] = 0
false_negatives[false_negatives == 1] = 0
n_fp = false_positives.sum()
n_fn = -false_negatives.sum()
penalty_map = cv2.filter2D(gt_mask, cv2.CV_32FC1, self.smoothing_kernel)
cv2.normalize(penalty_map, penalty_map, 0, 1.0, cv2.NORM_MINMAX)
weighted_fn = (penalty_map[false_negatives == -1]).sum()
penalty_map = penalty_map.max() - penalty_map # invert
weighted_fp = (penalty_map[false_positives == 1]).sum()
self.cum_fp += n_fp
self.cum_fn += n_fn
self.cum_wfn += weighted_fn
self.cum_wfp += weighted_fp
self.tested_frame_coutner += 1
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 _pre_processing(self, img):
self.input_image_origin = img
self.input_image = deepcopy(img)
self.input_image = cv2.resize(self.input_image, (self.image_size, self.image_size))
self.input_image = cv2.normalize(self.input_image, self.input_image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
self.input_image = [self.input_image]
self.input_image = np.array(self.input_image)
self.input_image = self.input_image.astype(np.float32)
self.input_image = self.input_image.reshape(-1, self.image_size, self.image_size, self.num_channels)
def process(state, W, H):
state = cv2.resize(state, (W, H))
state = cv2.cvtColor(state, cv2.COLOR_RGB2GRAY)
#cv2.imwrite('test.png', state)
#state = cv2.normalize(state, state, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
state = np.reshape(state, [W, H, 1])
return state
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 motion_detection(t_minus, t_now, t_plus):
delta_view = delta_images(t_minus, t_now, t_plus)
retval, delta_view = cv2.threshold(delta_view, 16, 255, 3)
cv2.normalize(delta_view, delta_view, 0, 255, cv2.NORM_MINMAX)
img_count_view = cv2.cvtColor(delta_view, cv2.COLOR_RGB2GRAY)
delta_count = cv2.countNonZero(img_count_view)
dst = cv2.addWeighted(screen,1.0, delta_view,0.6,0)
delta_count_last = delta_count
return delta_count
def motion_detection(t_minus, t_now, t_plus):
delta_view = delta_images(t_minus, t_now, t_plus)
retval, delta_view = cv2.threshold(delta_view, 16, 255, 3)
cv2.normalize(delta_view, delta_view, 0, 255, cv2.NORM_MINMAX)
img_count_view = cv2.cvtColor(delta_view, cv2.COLOR_RGB2GRAY)
delta_count = cv2.countNonZero(img_count_view)
dst = cv2.addWeighted(screen,1.0, delta_view,0.6,0)
delta_count_last = delta_count
return delta_count
def motion_detection(t_minus, t_now, t_plus):
delta_view = delta_images(t_minus, t_now, t_plus)
retval, delta_view = cv2.threshold(delta_view, 16, 255, 3)
cv2.normalize(delta_view, delta_view, 0, 255, cv2.NORM_MINMAX)
img_count_view = cv2.cvtColor(delta_view, cv2.COLOR_RGB2GRAY)
delta_count = cv2.countNonZero(img_count_view)
dst = cv2.addWeighted(screen,1.0, delta_view,0.6,0)
delta_count_last = delta_count
return delta_count
def compute_flow(impath1, impath2, outdir,
fbcodepath=os.getenv("HOME") + '/fbcode'):
stem = os.path.splitext(os.path.basename(impath1))[0]
deepmatch_cmd = os.path.join(fbcodepath,
'_bin/experimental/deeplearning/dpathak' +
'/video-processing/deepmatch/deepmatch')
call([deepmatch_cmd, impath1, impath2, '-out',
os.path.join(outdir, stem + '_sparse.txt'), '-downscale', '2'])
img1 = cv2.imread(impath1).astype(float)
M = np.zeros((img1.shape[0], img1.shape[1]), dtype=np.float32)
filt = np.array([[1., -1.]]).reshape((1, -1))
for c in range(3):
gx = convolve2d(img1[:, :, c], filt, mode='same')
gy = convolve2d(img1[:, :, c], filt.T, mode='same')
M = M + gx**2 + gy**2
M = M / np.max(M)
with open(os.path.join(outdir, '_edges.bin'), 'w') as f:
M.tofile(f)
epicflow_command = os.path.join(fbcodepath,
'_bin/experimental/deeplearning/dpathak' +
'/video-processing/epicflow/epicflow')
call([epicflow_command, impath1, impath2,
os.path.join(outdir, '_edges.bin'),
os.path.join(outdir, stem + '_sparse.txt'),
os.path.join(outdir, 'flow.flo')])
flow = read_flo(os.path.join(outdir, 'flow.flo'))
hsv = np.zeros_like(img1).astype(np.uint8)
hsv[..., 1] = 255
mag, ang = cv2.cartToPolar(flow[..., 0].astype(float),
flow[..., 1].astype(float))
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
hsv[..., 0] = ang * 180 / np.pi / 2
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(os.path.join(outdir, stem + '_flow.png'), bgr)
def run_deepmatch(imname1, imname2):
command = os.getenv("HOME") + '/fbcode/_bin/experimental/' + \
'deeplearning/dpathak/video-processing/deepmatch/deepmatch'
call([command, imname1, imname2,
'-out', os.getenv("HOME") + '/local/data/trash/tmp.txt',
'-downscale', '2'])
with open(os.getenv("HOME") + '/local/data/trash/tmp.txt', 'r') as f:
lines = f.readlines()
lines = [x.strip().split(' ') for x in lines]
vals = np.array([[float(y) for y in x] for x in lines])
x = ((vals[:, 0] - 8.) / 16.).astype(int)
y = ((vals[:, 1] - 8.) / 16.).astype(int)
U = np.zeros((int(np.max(y)) + 1, int(np.max(x)) + 1))
U[(y, x)] = vals[:, 2] - vals[:, 0]
V = np.zeros((int(np.max(y)) + 1, int(np.max(x)) + 1))
V[(y, x)] = vals[:, 3] - vals[:, 1]
img1 = cv2.imread(imname1)
U1 = cv2.resize(U, (img1.shape[1], img1.shape[0]))
V1 = cv2.resize(V, (img1.shape[1], img1.shape[0]))
mag, ang = cv2.cartToPolar(U1, V1)
print(np.max(mag))
hsv = np.zeros_like(img1)
hsv[..., 1] = 255
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return bgr
def __MR_fill_superpixel_with_saliency(self,labels,saliency_score):
sa_img = labels.copy().astype(float)
for i in range(sp.amax(labels)+1):
mask = labels == i
sa_img[mask] = saliency_score[i]
return cv2.normalize(sa_img,None,0,255,cv2.NORM_MINMAX)