def accumulate_frames(camera, averaged_image, method='accumulate', alpha='1'): """ Obtain an averaged frame from a webcam, with :param camera: cv2.VideoCapture() Camera object created with cv2.VideoCapture('camera_index') :param alpha: float [0:1] defines persistence of background. In other words, how long before a frame is forgotten (1 = never) :param color : string defines output pixel data possible values: 'RGB', 'BGR', 'avg', 'r', 'g', 'b', 'BGR' - no transformation, image given as BGR color coding 'RGB' - transforms color coding into RGB 'sum' - averages all colors and returns single value per pixel 'r' - returns only red channel 'g' - returns only green channel 'b' - returns only blue channel :return: np.array array of pixels with data as defined by color method """ _, frame = camera.read() # avg_img = np.float32(frame) if method == 'accumulate': cv2.accumulate(frame, averaged_image) elif method == 'accumulateWeighted': cv2.accumulateWeighted(frame, averaged_image, alpha) elif method == 'accumulateProduct': cv2.accumulateProduct(frame, averaged_image, alpha) elif method == 'accumulateSquare': cv2.accumulateSquare(frame, averaged_image, alpha) else: raise ValueError( 'Unknown accumulation method, please use one of: \n\taccumulate \n\taccumulateSquare \n\taccumulateProduct, \n\taccumulateWeighted' ) return averaged_image
def test_average_frames(): # experimental p = PROFILES[Names.DJ] fs = grabframes(url_dj) first_frame = fs[0] f = np.zeros((first_frame.shape[0], first_frame.shape[1], 3), 'float32') [cv2.accumulateSquare(x.astype('float32') / len(fs), f) for x in fs[1:]] f = (f / f.max()) * 256 f = f.astype('uint8')
def blur_Radial(source, size, xm, ym, low_high=(-5, 5), path=None, plot=None): filter_V = np.zeros((size, size)) filter_V[:, int((size - 1) / 2)] = np.ones(size) filter_V = filter_V / size high_ = low_high[1] low_ = low_high[0] sm_dims = high_ - low_ + 1 sm = np.zeros((sm_dims, sm_dims)) image_RGB = cv2.cvtColor(source, cv2.COLOR_BGR2RGB) sum = sumsq = None cnt = 0 for dx in range(low_, high_ + 1): for dy in range(low_, high_ + 1): polar_image = linear_2_Polar(source, xm + dx, ym + dy) polar_rad = cv2.filter2D(polar_image, -1, filter_V) if sum is None: sum = np.zeros(polar_rad.shape, np.float32) if sumsq is None: sumsq = np.zeros(polar_rad.shape, np.float32) dx2 = cv2.accumulate(polar_rad, sum) dy2 = cv2.accumulateSquare(polar_rad, sumsq) cnt = cnt + 1 xx = dx + high_ yy = dy + high_ if not (path is None) and Path(path).exists(): fname = path + 'polar_rad_%s_%s.png' % (xx, yy) cv2.imwrite(fname, polar_rad) polar_rad = polar_2_Linear(source, polar_rad, xm + dx, ym + dy) if dx == 0 and dy == 0: polar_rad_0_0 = polar_rad sm[dx + high_, dy + high_] = similarity(source, polar_rad) sumsq = cv2.multiply(sumsq, cnt) var_image = cv2.multiply(sum, sum) var_image = cv2.subtract(sumsq, var_image) var_image = cv2.divide(var_image, cnt * (cnt - 1)) c1, c2, c3 = cv2.split(var_image) cc = (c1 + c2 + c3) / 3 ydata = cv2.reduce(cc, 0, cv2.REDUCE_SUM, dtype=-1).flatten() idx = 0 for yd in ydata: print('%d %d' % (idx, yd)) idx = idx + 1 xdata = np.arange(len(ydata)) resd = find_inflections(xdata, ydata) result = resd['inflection'] first_valley = resd['xvalleyes'] pupil_radii = (first_valley[0] + first_valley[1]) / 2.0 if plot: f1, ax1 = plt.subplots(1) ax1.set_aspect('equal') ax1.imshow(image_RGB) ax1.set_title("Pupil Results") c = patches.Circle((xm, ym), pupil_radii, color='red', linewidth=2, fill=False) ax1.add_patch(c) plt.show() print(resd)
''' seam carving - http://en.wikipedia.org/wiki/Seam_carving ''' import cv2 import numpy as np #import hist img = cv2.imread('sofseam.jpg',0) im = cv2.imread('sofseam.jpg') rows,cols = img.shape dx = cv2.Sobel(img,cv2.CV_32F,1,0) dy = cv2.Sobel(img,cv2.CV_32F,0,1) dz = np.zeros(img.shape,np.float32) dx2 = cv2.accumulateSquare(dx,dz) dy2 = cv2.accumulateSquare(dy,dz) lap = cv2.sqrt(dz) #lap = cv2.convertScaleAbs(lap) t = np.zeros(img.shape,np.float32) t[0,:] = lap[0,:] for r in xrange(1,rows): for c in xrange(0,cols): i = lap.item(r,c) if c==0: j = min(t[r-1,c:c+2]) elif c==cols-1: j = min(t[r-1,c-1:c+1]) else:
cv2.namedWindow("sig2") cv2.namedWindow("detect") BGsample = 20 # number of frames to gather BG samples from at start of capture success, img = cap.read() width = cap.get(3) height = cap.get(4) if success: acc = np.zeros((height, width), np.float32) # 32 bit accumulator sqacc = np.zeros((height, width), np.float32) # 32 bit accumulator for i in range(20): a = cap.read() # dummy to warm up sensor # gather BG samples for i in range(BGsample): success, img = cap.read() frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.accumulate(frame, acc) cv2.accumulateSquare(frame, sqacc) # M = acc/float(BGsample) sqaccM = sqacc/float(BGsample) M2 = M*M sig2 = sqaccM-M2 # have BG samples now # calculate upper and lower bounds of detection window around mean. # coerce into 8bit image space for cv2.inRange compare detectmin = cv2.convertScaleAbs(M-sig2) detectmax = cv2.convertScaleAbs(M+sig2) # start FG detection key = -1 while(key < 0): success, img = cap.read() frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
numberOfIterations = 1 print("Number of loop iterations: " + str(numberOfIterations)) dstSW = np.zeros((height, width), np.float) xFimgY1 = mem_manager.cma_array( (height, width), np.uint8) #allocated physically contiguous numpy array xFimgY1[:] = imgY1[:] # copy source data xFdst = mem_manager.cma_array( (height, width), np.uint16) #allocated physically contiguous numpy array print("Start SW loop") startSW = time.time() for i in range(numberOfIterations): cv2.accumulateSquare(imgY1, dst=dstSW) #accumulateSquare on ARM stopSW = time.time() print("SW loop finished") print("Start HW loop") startPL = time.time() for i in range(numberOfIterations): xv2.accumulateSquare( xFimgY1, dst=xFdst ) #accumulateSquare offloaded to PL, working on physically continuous numpy arrays stopPL = time.time() print("HW loop finished") print("SW frames per second: ", ((numberOfIterations) / (stopSW - startSW))) print("PL frames per second: ", ((numberOfIterations) / (stopPL - startPL)))