我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用cv2.NORM_MINMAX。
def optical_flow(one, two): """ method taken from (https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4) """ one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY) two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY) hsv = np.zeros((120, 320, 3)) # set saturation hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1] # obtain dense optical flow paramters flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None, pyr_scale=0.5, levels=1, winsize=15, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) # convert from cartesian to polar mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) # hue corresponds to direction hsv[:,:,0] = ang * (180/ np.pi / 2) # value corresponds to magnitude hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # convert HSV to int32's hsv = np.asarray(hsv, dtype= np.float32) rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB) return rgb_flow
def depth_callback(self, ros_image): try: inImg = self.bridge.imgmsg_to_cv2(ros_image) except CvBridgeError, e: print e inImgarr = np.array(inImg, dtype=np.uint16) # inImgarr = cv2.GaussianBlur(inImgarr, (3, 3), 0) # cv2.normalize(inImgarr, inImgarr, 0, 1, cv2.NORM_MINMAX) self.outImg, self.num_fingers = self.process_depth_image(inImgarr) # outImg = self.process_depth_image(inImgarr) # rate = rospy.Rate(10) self.num_pub.publish(self.num_fingers) # self.img_pub.publish(self.bridge.cv2_to_imgmsg(self.outImg, "bgr8")) # rate.sleep() cv2.imshow("Hand Gesture Recognition", self.outImg) cv2.waitKey(3)
def writeOpticalFlowImage(self, index, optical_flow): filename = "flow_" + str(index) + ".png" output_path = os.path.join(self.optical_flow_output_directory, filename) # create hsv image shape_optical_flow = optical_flow.shape[:-1] shape_hsv = [shape_optical_flow[0], shape_optical_flow[1], 3] hsv = np.zeros(shape_hsv, np.float32) # set saturation to 255 hsv[:,:,1] = 255 # create colorful illustration of optical flow mag, ang = cv2.cartToPolar(optical_flow[:,:,0], optical_flow[:,:,1]) 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) cv2.imwrite(output_path, bgr)
def detectTemplateMatching(self, img): self.templateMatchingCurrentTime = cv2.getTickCount() duration = (self.templateMatchingCurrentTime - self.templateMatchingStartTime)/cv2.getTickFrequency() if duration > settings.templateMatchingDuration or self.trackedFaceTemplate[2] == 0 or self.trackedFaceTemplate[3] == 0: self.foundFace = False self.isTemplateMatchingRunning = False return faceTemplate = self.getSubRect(img, self.trackedFaceTemplate) roi = self.getSubRect(img, self.trackedFaceROI) match = cv2.matchTemplate(roi, faceTemplate, cv2.TM_SQDIFF_NORMED) cv2.normalize(match, match, 0, 1, cv2.NORM_MINMAX, -1) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(match) foundTemplate = ( minLoc[0] + self.trackedFaceROI[0], minLoc[1] + self.trackedFaceROI[1], self.trackedFaceTemplate[2], self.trackedFaceTemplate[3]) self.trackedFaceTemplate = foundTemplate self.trackedFace = self.scaleRect(self.trackedFaceTemplate, img, 2) self.trackedFaceROI = self.scaleRect(self.trackedFace, img, 2)
def create_cascade_neg_data(): img = cv2.imread(FLAGS.negatives_spritesheet) img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) height, width, _ = img.shape c = 0 txt = "" for y in xrange(0, height, FLAGS.image_size): for x in xrange(0, width, FLAGS.image_size): cv2.imwrite(FLAGS.output_dir + "/negatives/" + str(c) + ".png", img[y:y+FLAGS.image_size, x:x+FLAGS.image_size]) txt += "negatives/" + str(c) + ".png" + "\n" c += 1 with open(FLAGS.output_dir + "/negatives.info", 'w') as file: file.write(txt) return c # ========================================== #
def im_normalize(im, lo=0, hi=255, dtype='uint8'): return cv2.normalize(im, alpha=lo, beta=hi, norm_type=cv2.NORM_MINMAX, dtype={'uint8': cv2.CV_8U, \ 'float32': cv2.CV_32F, \ 'float64': cv2.CV_64F}[dtype])
def optical_flow(one, two): """ method taken from https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4 input: image_current, image_next (RGB images) calculates optical flow magnitude and angle and places it into HSV image """ one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY) two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY) hsv = np.zeros((120, 320, 3)) # set saturation hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1] # obtain dense optical flow paramters flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None, pyr_scale=0.5, levels=1, winsize=10, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) # convert from cartesian to polar mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) # hue corresponds to direction hsv[:,:,0] = ang * (180/ np.pi / 2) # value corresponds to magnitude hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # convert HSV to int32's hsv = np.asarray(hsv, dtype= np.float32) rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB) return rgb_flow
def blur_image(self, save=False, show=False): if self.part is None: psf = self.PSFs else: psf = [self.PSFs[self.part]] yN, xN, channel = self.shape key, kex = self.PSFs[0].shape delta = yN - key assert delta >= 0, 'resolution of image should be higher than kernel' result=[] if len(psf) > 1: for p in psf: tmp = np.pad(p, delta // 2, 'constant') cv2.normalize(tmp, tmp, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) # blured = np.zeros(self.shape) blured = cv2.normalize(self.original, self.original, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) blured[:, :, 0] = np.array(signal.fftconvolve(blured[:, :, 0], tmp, 'same')) blured[:, :, 1] = np.array(signal.fftconvolve(blured[:, :, 1], tmp, 'same')) blured[:, :, 2] = np.array(signal.fftconvolve(blured[:, :, 2], tmp, 'same')) blured = cv2.normalize(blured, blured, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) blured = cv2.cvtColor(blured, cv2.COLOR_RGB2BGR) result.append(np.abs(blured)) else: psf = psf[0] tmp = np.pad(psf, delta // 2, 'constant') cv2.normalize(tmp, tmp, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) blured = cv2.normalize(self.original, self.original, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) blured[:, :, 0] = np.array(signal.fftconvolve(blured[:, :, 0], tmp, 'same')) blured[:, :, 1] = np.array(signal.fftconvolve(blured[:, :, 1], tmp, 'same')) blured[:, :, 2] = np.array(signal.fftconvolve(blured[:, :, 2], tmp, 'same')) blured = cv2.normalize(blured, blured, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) blured = cv2.cvtColor(blured, cv2.COLOR_RGB2BGR) result.append(np.abs(blured)) self.result = result if show or save: self.__plot_canvas(show, save)
def test_features(): from atx.drivers.android_minicap import AndroidDeviceMinicap cv2.namedWindow("preview") d = AndroidDeviceMinicap() # r, h, c, w = 200, 100, 200, 100 # track_window = (c, r, w, h) # oldimg = cv2.imread('base1.png') # roi = oldimg[r:r+h, c:c+w] # hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) # mask = cv2.inRange(hsv_roi, 0, 255) # roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0,180]) # cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) # term_cirt = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while True: try: w, h = d._screen.shape[:2] img = cv2.resize(d._screen, (h/2, w/2)) cv2.imshow('preview', img) hist = cv2.calcHist([img], [0], None, [256], [0,256]) plt.plot(plt.hist(hist.ravel(), 256)) plt.show() # if img.shape == oldimg.shape: # # hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # # ret, track_window = cv2.meanShift(hsv, track_window, term_cirt) # # x, y, w, h = track_window # cv2.rectangle(img, (x, y), (x+w, y+h), 255, 2) # cv2.imshow('preview', img) # # cv2.imshow('preview', img) cv2.waitKey(1) except KeyboardInterrupt: break cv2.destroyWindow('preview')
def to_binary_mask(mask, t=0.00001): mask = inverse_preprocessing(mask) ### Threshold the RGB image - This step increase sensitivity mask[mask > t] = 255 mask[mask <= t] = 0 ### To grayscale and normalize mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) mask_gray = cv2.normalize(src=mask_gray, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) ### Auto binary threshold (thresh, mask_binary) = cv2.threshold(mask_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) return mask_binary
def process_output(self, disparity): cv8uc = cv2.normalize(disparity, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) if self.args.preview: cv2.imshow("disparity", cv8uc) cv2.waitKey(0) cv2.imwrite(os.path.join(self.args.folder, self.args.output), cv8uc)
def prep_img_save(img, b=5): return cv2.normalize(cv2.copyMakeBorder(img, b, b, b, b, cv2.BORDER_CONSTANT, value=0), 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
def normalize(im): return cv2.normalize(im, np.zeros(im.shape), 0, 255, norm_type=cv2.NORM_MINMAX)
def calculate_flow(self, frame_a, frame_b): previous_frame = cv2.cvtColor(frame_a, cv2.COLOR_BGR2GRAY) next_frame = cv2.cvtColor(frame_b, cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback( previous_frame, next_frame, None, 0.5, 3, 15, 3, 5, 1.2, 0 ) # Change here horz = cv2.normalize(flow[..., 0], None, 0, 255, cv2.NORM_MINMAX) vert = cv2.normalize(flow[..., 1], None, 0, 255, cv2.NORM_MINMAX) horz = horz.astype('uint8') vert = vert.astype('uint8') # Change here too cv2.imshow('Horizontal Component', horz) cv2.imshow('Vertical Component', vert) k = cv2.waitKey(0) & 0xff if k == ord('s'): # Change here cv2.imwrite('opticalflow_horz.pgm', horz) cv2.imwrite('opticalflow_vert.pgm', vert) cv2.destroyAllWindows()
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 __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 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)
def normalize(img_in): img_out = np.zeros(img_in.shape) cv2.normalize(img_in, img_out, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) return img_out # Gradient Direction
def getImage(path): """ Args: path: path to image Returns: image at path """ img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) return cv2.normalize(img, None, 0.0, 1.0, cv2.NORM_MINMAX, cv2.CV_32F) # ============================================================= #
def create_mask(model, x, keep_prob, src): """ object detection via sliding windows Args: model: tensorflow model which is used for detection x: input data placeholder keep_prob: keep probability placeholder (dropout) src: image to apply the detection Returns: image mask scaled between 0 and 255 """ global sess height, width = src.shape mask = np.zeros((height,width), np.float32) input_size = (FLAGS.input_size, FLAGS.input_size) min_window_size = (FLAGS.min_window_size, FLAGS.min_window_size) max_window_size = (FLAGS.max_window_size, FLAGS.max_window_size) for windows, coords in utils.slidingWindow(src, FLAGS.step_size, input_size, FLAGS.scale_factor, min_window_size, max_window_size): feed = {x:windows, keep_prob:1.0} out = sess.run(model, feed_dict = feed) for i in range(0, len(out)): out_scaled = cv2.resize(np.reshape(out[i], [FLAGS.label_size,FLAGS.label_size]), coords[i].size(), interpolation=cv2.INTER_CUBIC) mask[coords[i].y : coords[i].y2(), coords[i].x : coords[i].x2()] += out_scaled # image processing mask = cv2.normalize(mask, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) # cv2.imwrite(FLAGS.output_dir + FLAGS.test + '_mask_' + str(FLAGS.step_size) + '_' + str(datetime.datetime.now()) + '.png', mask) return mask # ============================================================= #
def trackObjects(self): for area in self.trackedAreasList: # Template matching gray = cv2.cvtColor(self.processedFrame, cv2.COLOR_BGR2GRAY) templ = area.getGrayStackAve() cc = cv2.matchTemplate(gray, templ, cv2.TM_CCOEFF_NORMED) cc = cc * cc * cc * cc _, cc = cv2.threshold(cc, 0.1, 0, cv2.THRESH_TOZERO) cc8 = cv2.normalize(cc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) mask = np.zeros_like(cc8) # Search match within template region mcorn = area.getEnlargedCorners(0) # If not 0, enalrge the search cv2.rectangle(mask, mcorn[0], mcorn[1], 255, -1) _, _, _, mx = cv2.minMaxLoc(cc8, mask) # kp = area.getKalmanPredict() # area.updateWindow(kp) # area.setTemplate(self.processedFrame) # Prevent large spatial jumps (c, r, _, _) = area.getcrwh() jump = 10 if abs(c - mx[0]) < jump and abs(r - mx[1]) < jump: # area.setKalmanCorrect(mx) area.updateWindow(mx) else: # area.setKalmanCorrect((c, r)) area.updateWindow((c, r)) area.setTemplate(self.processedFrame) # Show the template stack if self.showTemplate is True: cv2.imshow('Stack: '+str(area), area.getStack()) else: try: cv2.destroyWindow('Stack: '+str(area)) except: pass # Show the matching results if self.showMatch is True: cv2.rectangle(cc8, mcorn[0], mcorn[1], 255, 1) cv2.circle(cc8, mx, 5, 255, 1) cv2.imshow('Match: '+str(area), cc8) else: try: cv2.destroyWindow('Match: '+str(area)) except: pass # Draw the tracked area on the image corn = area.getCorners() cv2.rectangle(self.workingFrame, corn[0], corn[1], (0, 255, 0), 1) # self.showFrame() # raw_input('wait')
def sColorBalance(img_hsv, porcentaje): ''' Funcion encarganda de: Separar los canales HSV de la imagen (split) Ordenar los valores de pixeles y seleccionar los "cuantiles" de la matriz ordenada Obtener los valores max y min de la matriz ordenanda para cada canal HSV (a partir del porcentaje de saturacion) Saturar la imagen para los valores max y min de cada canal Todo esto con el fin de que los colores HSV ocupen el mayor rango posible [0,255] aplicando una transformacion afin a cada canal ''' assert img_hsv.shape[2] == 3 assert porcentaje > 0 and porcentaje < 100 mitad_porcentaje = porcentaje/200.0 canales = cv2.split(img_hsv) #Separa los canales en HSV salida_canales = [] for canal in canales: assert len(canal.shape) == 2 # find the low and high precentile values (based on the input percentile) filas,columnas = canal.shape vec_tam = columnas*filas flat = canal.reshape(vec_tam) assert len(flat.shape) == 1 flat = np.sort(flat) n_cols = flat.shape[0] #Seleccion de los valores minimos y maximos de cada canal HSV de la imagen. Seria el stretching bajo_val = flat[math.floor(n_cols*mitad_porcentaje)] #Calcula el valor bajo del arreglo ordenado de la matriz (img) de entrada para cada canal alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje))] #Calcula el valor alto del arreglo ordenado de la matriz (img) de entrada para cada canal Alternativa: alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje)-1)] #Los valores alto y bajo para cada canal HSV. El orden de impresion es Hue, Saturation, Value print "Lowval: ", alto_val print "Highval: ", bajo_val # saturate below the low percentile and above the high percentile thresholded = apply_threshold(canal,bajo_val,alto_val) # scale the canal normalized = cv2.normalize(thresholded,thresholded.copy(), 0, 255, cv2.NORM_MINMAX) salida_canales.append(normalized) norm_tile = np.tile(normalized[:,:,np.newaxis],(1,1,3)) return cv2.merge(salida_canales)
def sColorBalance(img_hsv, porcentaje): ''' Funcion encarganda de: Separar los canales HSV de la imagen (split) Ordenar los valores de pixeles y seleccionar los "cuantiles" de la matriz ordenada Obtener los valores max y min de la matriz ordenanda para el canal V (a partir del porcentaje de saturacion) Saturar la imagen para los valores max y min de cada canal Todo esto con el fin de que los colores de la imagen recuperada ocupen el mayor rango posible [0,255] aplicando una transformacion solo al canal V ''' assert img_hsv.shape[2] == 3 assert porcentaje > 0 and porcentaje < 100 mitad_porcentaje = porcentaje/200.0 hueOri,satOri,valOri = cv2.split(img_hsv) #Separa los canales en HSV filas,columnas,canales = img_hsv.shape cv2.imshow("h", hueOri) cv2.imshow("s", satOri) cv2.imshow("v", valOri) salida_canales = [] canal = valOri assert len(canal.shape) == 2 print canal # find the low and high precentile values (based on the input percentile) filas,columnas = canal.shape vec_tam = columnas*filas flat = canal.reshape(vec_tam) assert len(flat.shape) == 1 flat = np.sort(flat) n_cols = flat.shape[0] #Seleccion de los valores minimos y maximos de cada canal HSV de la imagen. Seria el stretching bajo_val = flat[math.floor(n_cols*mitad_porcentaje)] #Calcula el valor bajo del arreglo ordenado de la matriz (img) de entrada para cada canal alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje))] #Calcula el valor alto del arreglo ordenado de la matriz (img) de entrada para cada canal Alternativa: alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje)-1)] #Los valores alto y bajo para cada canal HSV. El orden de impresion es Hue, Saturation, Value print "Lowval: ", alto_val print "Highval: ", bajo_val # saturate below the low percentile and above the high percentile thresholded = apply_threshold(canal,bajo_val,alto_val) # scale the canal normalized = cv2.normalize(thresholded,thresholded.copy(), 0, 255, cv2.NORM_MINMAX) print normalized cv2.imshow("Madfe", normalized) salida_canales.append(normalized) img_merge = cv2.merge((hueOri,satOri,normalized)) return img_merge
def sColorBalance(img, porcentaje): ''' Funcion encarganda de: Separar los canales RGB de la imagen (split) Ordenar los valores de pixeles y seleccionar los "cuantiles" de la matriz ordenada Obtener los valores max y min de la matriz ordenanda para cada canal RGB (a partir del porcentaje de saturacion) Saturar la imagen para los valores max y min de cada canal Todo esto con el fin de que los colores RGB ocupen el mayor rango posible [0,255] aplicando una transformacion afin a cada canal ''' assert img.shape[2] == 3 #assert porcentaje > 0 and porcentaje < 100 mitad_porcentaje = porcentaje/200.0 canales = cv2.split(img) #Separa los canales en RGB salida_canales = [] for canal in canales: assert len(canal.shape) == 2 # find the low and high precentile values (based on the input percentile) filas,columnas = canal.shape vec_tam = columnas*filas flat = canal.reshape(vec_tam) assert len(flat.shape) == 1 flat = np.sort(flat) n_cols = flat.shape[0] #Seleccion de los valores minimos y maximos de cada canal RGB de la imagen. Seria el stretching bajo_val = flat[math.floor(n_cols*mitad_porcentaje)] #Calcula el valor bajo del arreglo ordenado de la matriz (img) de entrada para cada canal alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje))] #Calcula el valor alto del arreglo ordenado de la matriz (img) de entrada para cada canal Alternativa: alto_val = flat[math.ceil(n_cols*(1-mitad_porcentaje)-1)] #Los valores alto y bajo para cada canal RGB. El orden de impresion es Blue, Green, Red print "Lowval: ", alto_val print "Highval: ", bajo_val # saturate below the low percentile and above the high percentile thresholded = apply_threshold(canal,bajo_val,alto_val) # scale the canal normalized = cv2.normalize(thresholded,thresholded.copy(), 0, 255, cv2.NORM_MINMAX) salida_canales.append(normalized) return cv2.merge(salida_canales)
def writeOpticalFlow(path,filename,w,h,c): count=0 try: cap = cv2.VideoCapture(path+'/'+filename) ret, frame1 = cap.read() if frame1==None: return count frame1 = cv2.resize(frame1, (w,h)) prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) if not os.path.isdir("../dataset/of_images"): os.mkdir("of_images") folder = '../dataset/of_images'+'/'+filename+'/' dir = os.path.dirname(folder) os.mkdir(dir) while(1): ret, frame2 = cap.read() if frame2 is None: break count+=1 if count%5==0: print (filename+':' +str(c)+'-'+str(count)) frame2 = cv2.resize(frame2, (w,h)) next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0) horz = cv2.normalize(flow[...,0], None, 0, 255, cv2.NORM_MINMAX) vert = cv2.normalize(flow[...,1], None, 0, 255, cv2.NORM_MINMAX) horz = horz.astype('uint8') vert = vert.astype('uint8') cv2.imwrite(folder+'h'+str(count)+'_'+filename+'.jpg',horz,[int(cv2.IMWRITE_JPEG_QUALITY), 90]) cv2.imwrite(folder+'v'+str(count)+'_'+filename+'.jpg',vert,[int(cv2.IMWRITE_JPEG_QUALITY), 90]) prvs = next cap.release() cv2.destroyAllWindows() return count except Exception as e: return count
def writeOpticalFlow(path,filename,w,h,c): count=0 try: cap = cv2.VideoCapture(path+'/'+filename) ret, frame1 = cap.read() if frame1==None: return count frame1 = cv2.resize(frame1, (w,h)) prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) folder = './of_images'+'/'+filename+'/' dir = os.path.dirname(folder) os.mkdir(dir) while(1): ret, frame2 = cap.read() if frame2==None: break count+=1 if count%5==0: print (filename+':' +str(c)+'-'+str(count)) frame2 = cv2.resize(frame2, (w,h)) next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0) horz = cv2.normalize(flow[...,0], None, 0, 255, cv2.NORM_MINMAX) vert = cv2.normalize(flow[...,1], None, 0, 255, cv2.NORM_MINMAX) horz = horz.astype('uint8') vert = vert.astype('uint8') cv2.imwrite(folder+'h'+str(count)+'_'+filename+'.jpg',horz,[int(cv2.IMWRITE_JPEG_QUALITY), 90]) cv2.imwrite(folder+'v'+str(count)+'_'+filename+'.jpg',vert,[int(cv2.IMWRITE_JPEG_QUALITY), 90]) prvs = next cap.release() cv2.destroyAllWindows() return count except Exception,e: print e return count
def main(_): image_path = FLAGS.test csv_path = os.path.splitext(image_path)[0] + ".csv" # --------- load classifier ------- # cascade = cv2.CascadeClassifier(FLAGS.cascade_xml) model, x, keep_prob = get_nn_classifier() # ---------- object detection ------------# print 'starting detection of ' + FLAGS.test + '...' img = utils.getImage(image_path) img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) delta = [-2, -1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95, 0.99, 0.995, 0.999, 0.9995, 0.9999] start = time.time() candidates = cascade.detectMultiScale(img, scaleFactor=FLAGS.scaleFactor, minNeighbors=FLAGS.minNeighbors, maxSize=(FLAGS.max_window_size,FLAGS.max_window_size)) detected = nn_classification(candidates, img, model, x, keep_prob, delta) elapsed = (time.time() - start) print 'detection time: %d' % elapsed # ------------- evaluation --------------# ground_truth_data = utils.get_ground_truth_data(csv_path) for j in xrange(0, len(delta)): detected[j] = [Rect(x, y, w, h) for (x,y,w,h) in detected[j]] tp, fn, fp = utils.evaluate(ground_truth_data, detected[j]) # ----------------output ----------------# # image output """ img_out = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) for (x,y,w,h) in detected[j]: cv2.rectangle(img_out, (x-w/2,y-h/2),(x+w/2,y+h/2), [0,255,0], 3) for c in ground_truth_data: cv2.circle(img_out, (c[0], c[1]), 3, [0,0,255],3) output_file = "out" + '_' + str(datetime.datetime.now()) cv2.imwrite(FLAGS.output_dir + output_file + '.png', img_out) """ # csv output with open(FLAGS.output_dir + FLAGS.out + '.csv', 'ab') as file: writer = csv.writer(file, delimiter=',') writer.writerow([FLAGS.test, str(elapsed), str(len(ground_truth_data)), delta[j], FLAGS.minNeighbors, FLAGS.scaleFactor, str(len(detected[j])), str(tp), str(fp), str(fn)])
def main(): image_path = FLAGS.test csv_path = os.path.splitext(image_path)[0] + ".csv" # ------------ load classifier ---------- # cascade = cv2.CascadeClassifier(FLAGS.cascade_xml) # -------------- open image --------------# img = utils.getImage(image_path) img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) # ---------- object detection ------------# print 'starting detection of ' + FLAGS.test + '...' start = time.time() detected = cascade.detectMultiScale(img, scaleFactor=FLAGS.scaleFactor, minNeighbors=FLAGS.minNeighbors, maxSize=(FLAGS.max_window_size, FLAGS.max_window_size)) elapsed = (time.time() - start) print 'detection time: %d' % elapsed # ------------- evaluation --------------# detected = [Rect(x, y, w, h) for (x,y,w,h) in detected] ground_truth_data = utils.get_ground_truth_data(csv_path) tp, fn, fp = utils.evaluate(ground_truth_data, detected) # ----------------output ----------------# # image output """ img_out = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) for c in ground_truth_data: cv2.circle(img_out, (c[0], c[1]), 3, [0,0,255],3) for r in detected: cv2.rectangle(img_out, (r.x, r.y), (r.x2(), r.y2()), [0,255,0], 2) output_file = "out" + '_' + str(datetime.datetime.now()) cv2.imwrite(FLAGS.output_dir + output_file + '.png', img_out) """ # csv output with open(FLAGS.output_dir + 'results.csv', 'ab') as file: writer = csv.writer(file, delimiter=',') writer.writerow([FLAGS.test, str(elapsed),str(len(ground_truth_data)), str(FLAGS.scaleFactor), str(FLAGS.minNeighbors), str(len(detected)), str(tp), str(fp), str(fn)])