コード例 #1
0
ファイル: opticflow.py プロジェクト: Ripley6811/Nikcut
def test_vector_flow(a, b, c, max_angle=pi/32):
    '''Tests a contiguous set of matches. The difference in angle between a-b
    and b-c must be less than 'max_angle'. Default is difference of 5 degrees.
    '''
    try:
        ab = array([b['x'] - a['x'], b['y'] - a['y']])
        bc = array([c['x'] - b['x'], c['y'] - b['y']])
    except ValueError:
        ab = b-a
        bc = c-b

    vec = array([cv2.cartToPolar(r_[ab[0]], r_[ab[1]])[1].flatten(),
                 cv2.cartToPolar(r_[bc[0]], r_[bc[1]])[1].flatten()])
    mins = min(vec,0)
    vec -= mins
    vec = max(vec,0)
    vec[vec>pi] = 2*pi - vec[vec>pi]

    gdvecs = vec < max_angle
    divisor = sqrt(sum(ab[:2]**2,0))
    nonzero = divisor != 0.
    scalings = sqrt(sum(bc[:2]**2,0))/divisor
#    print 'scales', scalings
    meanscale = mean(sqrt(sum(bc[:2,nonzero]**2,0))/sqrt(sum(ab[:2,nonzero]**2,0)))
#    print 'scale mean', meanscale
    stdscale = std(sqrt(sum(bc[:2,nonzero]**2,0))/sqrt(sum(ab[:2,nonzero]**2,0)))
#    print 'scale std', stdscale
    gdscale = (scalings >= (meanscale-stdscale)) & (scalings <= (meanscale+stdscale))

    return gdvecs & gdscale
コード例 #2
0
    def run_simple(self):
        u'''Simple prediction'''
        if len(self.datapath) >= 2:
            # Use only two previous images
            af_img = io.imread(self.datapath[0])
            bf_img = io.imread(self.datapath[1])
            
            #af_img = io.imread(r'./viptrafficof_02.png')
            #bf_img = io.imread(r'./viptrafficof_03.png')

            # Convert to gray image
            af_gray = color.rgb2gray(af_img)
            bf_gray = color.rgb2gray(bf_img)

            # Calculate density flow
            # Small -> WHY?
            flow = cv2.calcOpticalFlowFarneback(bf_gray, af_gray, \
                0.5, 6, 20, 10, 5, 1.2, 0)
            print  flow.shape, flow[:, :, 0].min(), flow[:, :, 1].max()  
            self.before = bf_gray
            self.after = af_gray
            #self.result = self.current
            self.result = transform(af_img, flow)
            
            # Color code the result for better visualization of optical flow. 
            # Direction corresponds to Hue value of the image. 
            # Magnitude corresponds to Value plane
            
            mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
            hsv = np.zeros_like(af_img)
            hsv[...,1] = 255
            hsv[...,0] = ang*180/np.pi/2
            hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
            self.optical = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)        
コード例 #3
0
ファイル: pixellete.py プロジェクト: sudhargk/video-annotator
def gradient(image,bin_n=16):
	imageDim = np.prod(image.shape[:2])
	gx = cv2.Sobel(image, cv2.CV_32F, 1, 0)
	gy = cv2.Sobel(image, cv2.CV_32F, 0, 1)
	mag, ang = cv2.cartToPolar(gx, gy)
	grad = np.hstack((mag.reshape(imageDim,3),ang.reshape(imageDim,3)))    
	return grad
コード例 #4
0
ファイル: classify.py プロジェクト: hargup/freezing-batman
def hog(img, no_bins=16, orient=False):
    """
    Calculates the normalised histogram of oriented gradient of a grayscale image.

    Parameters
    ----------

    no_bins: no of bins to be used

    orient: If set to True, the function expects a
    2D array of angles of gradient (in radians) corrosponding
    to each pixel
    """

    if not orient:
        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(no_bins * ang / (2 * np.pi))
    elif orient:
        bins = np.int32(no_bins * img / (2 * np.pi))

    hist = cv2.calcHist([bins.astype("float32")], [0], None, [16], [0, 16])
    hist = np.hstack(hist)

    return (hist * 1000) / (img.shape[0] * img.shape[1])
コード例 #5
0
def getGradientInfo(img):
	sobelHorizontal = cv2.Sobel(img, ddepth = cv2.cv.CV_32F, dx = 1, dy = 0, ksize = 3)
	sobelVertical = cv2.Sobel(img, ddepth = cv2.cv.CV_32F, dx = 0, dy = 1, ksize = 3)

	magnitude, angle = cv2.cartToPolar(sobelHorizontal, sobelVertical)

	# sobelHorizontal = cv2.convertScaleAbs(sobelHorizontal)
	# sobelVertical = cv2.convertScaleAbs(sobelVertical)

	# sobelHorizontal2 = cv2.convertScale(sobelHorizontal)
	# sobelVertical2 = cv2.convertScale(sobelVertical)

	# cv2.imshow("Threshold", cv2.addWeighted(sobelHorizontal, 0.5, sobelVertical, 0.5, 0))

	# Quiver Plot
	res = 10
	N, M = img.shape
	X, Y = np.meshgrid(np.arange(0, M, res), np.arange(0, N, res))

	f = figure(1)
	imshow(img, cmap=cm.gray)
	# quiver(X, Y, sobelHorizontal[::res,::res], sobelVertical[::res,::res], units = "xy")
	abc = cv2.addWeighted(sobelHorizontal, 0.5, sobelVertical, 0.5, 0)

	print(abc.shape)
	quiver(X, Y, cv2.transpose(sobelHorizontal)[::res,::res], cv2.transpose(sobelVertical)[::res,::res])
	f.show()

	return 
コード例 #6
0
ファイル: fcontrol.py プロジェクト: uhu99/kurzware
    def flow2hsv(self, flow):
        mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
        self.hsv[...,0] = ang*self.ca
        self.hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
        bgr = cv2.cvtColor(self.hsv,cv2.COLOR_HSV2BGR)

        return bgr
コード例 #7
0
ファイル: feature_extraction.py プロジェクト: fortjohnson/VPT
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
コード例 #8
0
ファイル: feature_extraction.py プロジェクト: fortjohnson/VPT
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)
コード例 #9
0
def wing_lengths(wing_masks, wing_paths):
    left_wing_length = right_wing_length = 0
    height, width = wing_masks.shape[:2]

    for path, name in zip(wing_paths, ['left', 'right']):
        i, j = np.where(path == 255)

        path_indices = [(y, x) for (y, x) in sorted(zip(i, j), key=lambda ind: ind[0]) if y > 0 and y < (height - 1) and x > 0 and x < (width - 1)] 
        
        if name == 'left':
            cut_y, cut_x = [(y, x) for (y, x) in path_indices if np.sum(wing_masks[(y - 1):(y + 2), (x - 1):(x + 1)]) >= (3 * 255)][0]
            wing = wing_masks[:cut_y, :cut_x]
            xv, yv = np.meshgrid(np.arange(0, cut_x), np.arange(0, cut_y))
        elif name == 'right':
            cut_y, cut_x = [(y, x) for (y, x) in path_indices if np.sum(wing_masks[(y - 1):(y + 2), x:(x + 2)]) >= (3 * 255)][0]
            wing = wing_masks[:cut_y, cut_x:]
            xv, yv = np.meshgrid(np.arange(cut_x, width), np.arange(0, cut_y))

        xv, yv = xv[np.where(wing == 255)], yv[np.where(wing == 255)]

        if xv.size != 0 and yv.size != 0:
            distance, _ = cv2.cartToPolar(xv.astype('float32') - cut_x, yv.astype('float32') - cut_y)

            index = np.argmax(distance)
            if name == 'left':
                left_wing_length = distance[index, 0]
            elif name == 'right':
                right_wing_length = distance[index, 0]

    return left_wing_length, right_wing_length
コード例 #10
0
ファイル: seg_features.py プロジェクト: jpbialas/backbone
def hog(my_map, seg):
    '''
        my_map: map holding the image to extract features from
        seg: Segmentation level to find features for
        RETURNS:
            HoG for each segment
            Names of those features
    '''
    bins = 16
    names = []
    for i in range(bins):
        names.append('hog {}'.format(i))
    p = os.path.join('features', "color_edge_shapefilewithfeatures003-00{}-{}.npy".format(my_map.name[-1], seg))
    
    if os.path.exists(p):
        data = np.load(p)
    else:
        pbar = custom_progress()
        bw_img = cv2.cvtColor(my_map.img, cv2.COLOR_RGB2GRAY)
        gx = cv2.Sobel(bw_img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(bw_img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        angles = ang/2.0*np.pi*255
        segs = my_map.segmentations[seg].ravel()
        n_segs = int(np.max(segs))+1
        data = np.zeros((n_segs, 16), dtype = 'uint8')
        for i in pbar(range(n_segs)):
            indices = np.where(segs == i)[0]
            data[i] = np.histogram(angles.ravel()[indices], 16)[0]
        np.save(p, data)
    return data, names
コード例 #11
0
ファイル: common.py プロジェクト: rdefeo/image_processing
def preprocess_item_hist(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)

    bin_n = 16
    bin = np.int32(bin_n*ang/(2*np.pi))
    bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)

    # transform to Hellinger kernel
    eps = 1e-7
    hist /= hist.sum() + eps
    hist = np.sqrt(hist)
    hist /= norm(hist) + eps

    # len(hist) = 64
    # type(hist) = <type 'numpy.ndarray'>
    # type(hist[0]) = <type 'numpy.float64'>
    # hist[0] = 0.0

    # print hist
    # [ 0.          0.          0.          0.          0.          0.          0.
    #   0.          0.          0.          0.          0.          0.          0.
    #   0.          0.          0.02500444  0.03224029  0.0348727   0.05486763
    #   0.05423923  0.05318612  0.09491311  0.13133056  0.07284845  0.0518625
    #   0.04501667  0.08354647  0.07588132  0.03017876  0.0360361   0.0199627   0.
    #   0.          0.          0.          0.          0.          0.          0.
    #   0.          0.          0.          0.          0.          0.          0.
    #   0.          0.21109739  0.23844386  0.2609863   0.28829302  0.29204482
    #   0.23182723  0.18321263  0.20312054  0.18725011  0.26777668  0.24209061
    #   0.25903133  0.27651589  0.25722604  0.23205954  0.20312469]
    return hist
コード例 #12
0
def process_optical_flow(video_path):
    cap = cv2.VideoCapture(video_path)
    ret, frame1 = cap.read()
    frame1 = imutils.resize(frame1, width=800)
    prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    prvs = cv2.GaussianBlur(prvs, (21, 21), 0)
    hsv = np.zeros_like(frame1)
    hsv[..., 1] = 255

    while True:
        ret, frame2 = cap.read()
        frame2 = imutils.resize(frame2, width=800)
        next_f = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        next_f = cv2.GaussianBlur(next_f, (21, 21), 0)

        # flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 1, 3, 15, 3, 5, 1)
        flow = cv2.calcOpticalFlowFarneback(prvs, next_f, 0.5, 3, 15, 3, 5, 1.2, 0)

        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        hsv[..., 0] = ang * 180 / np.pi / 2
        hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
        rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

        cv2.imshow('frame2', rgb)
        k = cv2.waitKey(30) & 0xff
        if k == ord('q'):
            break
        # elif k == ord('s'):
        #     cv2.imwrite('opticalfb.png', frame2)
        #     cv2.imwrite('opticalhsv.png', rgb)
        prvs = next_f

    cap.release()
    cv2.destroyAllWindows()
コード例 #13
0
ファイル: Farneback.py プロジェクト: kmakantasis/CV-Tools
    def CalculateOpticalFlow(self, prev, nxt):
        """               
            Function definition
            +++++++++++++++++++
            
            .. py:function:: CalculateOpticalFlow(prev, nxt)
            
                Computes a dense optical flow using the Gunnar Farneback’s algorithm using two subsequent
                frames.

                :param numpy_array prev: the first frame of the two subsequent frames.
                :param numpy_array nxt: the second frame of the two subsequent frames.
                             
        """
        
        hsv = np.zeros((prev.shape[0], prev.shape[1], 3))
        hsv[...,1] = 255
        
        flow = cv2.calcOpticalFlowFarneback(prev,nxt, 0.5, 3, 15, 3, 5, 1.2, 0)
        self.flow = flow

        mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1], angleInDegrees=1)
        hsv[...,0] = ang/2
        hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
        hsv = np.array(hsv, dtype=np.uint8)
        self.motion_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
        self.magnitude_image = hsv[...,2]
        self.direction_image = ang
コード例 #14
0
ファイル: testFilters.py プロジェクト: bchappet/dnfpy
def test_optFlowPyrLK():

    # Parameters for lucas kanade optical flow
    lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))



    cap = cv2.VideoCapture(0)
    ret,frame = cap.read()
    old_col = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    hsv = np.zeros_like(frame)
    hsv[...,1] = 255

    while(1):
        ret,frame = cap.read()
        frame_col = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        flow = cv2.calcOpticalFlowFarneback(old_col,frame_col, 0.5, 3, 15, 3, 5, 1.2, 0)
        old_col = frame_col.copy()


        mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
        hsv[...,0] = ang*180/np.pi/2
        hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
        rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)

        cv2.imshow('flow',rgb)
        


    cap.release()
コード例 #15
0
def dense_opt_flow(n_stills): #dense optical flow
    cap = cv2.VideoCapture("test2.avi")

    ret, frame1 = cap.read()
    prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    hsv = np.zeros_like(frame1)
    hsv[...,1] = 255

    while(1):
        ret, frame2 = cap.read()
        next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

        flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

        mag, ang = cv2.cartToPolar(flow[...,0], 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.imshow('frame2',bgr)
        k = cv2.waitKey(1) & 0xff
        if k == 27:
            break
        elif k == ord('s'):
            cv2.imwrite('opticalfb.png',frame2)
            cv2.imwrite('opticalhsv.png',bgr)
        prvs = next

    cap.release()
    cv2.destroyAllWindows()
コード例 #16
0
ファイル: svm.py プロジェクト: rcchen/cs221-project
def preprocess_hog(digits):
    samples = []
    for img in digits:
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        # bin_n = 16
        bin_n = 1024
        bin = np.int32(bin_n*ang/(2*np.pi))
        n_rows = 2
        n_cols = 2
        bin_h = SZ / n_rows
        bin_w = SZ / n_cols
        bin_cells = list()
        mag_cells = list()
        for i in xrange(n_rows):
            for j in xrange(n_cols):
                bin_cells.append(bin[ (i-1)*bin_h : i*bin_h, (j-1)*bin_w : j*bin_w])
                mag_cells.append(mag[ (i-1)*bin_h : i*bin_h, (j-1)*bin_w : j*bin_w])
        # bin_w = SZ / 2
        # bin_cells = [bin[:bin_w,:bin_w], bin[bin_w:,:bin_w], bin[:bin_w,bin_w:], bin[bin_w:,bin_w:]]
        # mag_cells = [mag[:bin_w,:bin_w], mag[bin_w:,:bin_w], mag[:bin_w,bin_w:], mag[bin_w:,bin_w:]]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)

        # transform to Hellinger kernel
        # eps = 1e-7
        # hist /= hist.sum() + eps
        # hist = np.sqrt(hist)
        # hist /= norm(hist) + eps

        samples.append(hist)
    return np.float32(samples)
コード例 #17
0
ファイル: test.py プロジェクト: beduffy/VideoUnderstanding
def optical_flow(video_path):
    cap = cv2.VideoCapture(video_path)

    ret, frame1 = cap.read()
    prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    hsv = np.zeros_like(frame1)
    hsv[...,1] = 255

    while(1):
        ret, frame2 = cap.read()
        next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

                                            #prev, next, scale, levels, winsize, ite, poly_n, poly_sigma
        flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 1, 4, 3, 5, 1.1, cv2.OPTFLOW_FARNEBACK_GAUSSIAN)
        # flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

        mag, ang = cv2.cartToPolar(flow[...,0], 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.imshow('optflow',bgr)
        cv2.imshow('orig', frame2)
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
        elif k == ord('s'):
            cv2.imwrite('opticalfb.png',frame2)
            cv2.imwrite('opticalhsv.png',bgr)
        prvs = next

    cap.release()
    cv2.destroyAllWindows()
コード例 #18
0
def histogramOfGradients(img):
    '''
        
    ---
    I:
    O:
    '''
    import numpy as np
    import cv2

    # Calculate Sobel derivatives of each cell in x/y direction
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)

    # Find magnitude/direction of gradient at each pixel
    mag, ang = cv2.cartToPolar(gx, gy)

    # Quantizing binvalues in (0...16)
    bins = np.int32(bin_n*ang/(2*np.pi))

    # Divide to 4 sub-squares
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]

    hists = [np.bincount(b.ravel(), m.ravel(), bin_n)
             for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)
    
    return hist
コード例 #19
0
def saliency_feature(img):
    img_orig = img
    img = cv2.resize(img, (64, 64))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # h = cv2.getOptimalDFTSize(img.shape[0])
    # w = cv2.getOptimalDFTSize(img.shape[1])
    # print "Resizing (%d, %d) to (%d, %d)" % (img.shape[0], img.shape[1], h, w)
    # h = (h - img.shape[0])/2.0
    # w = (w - img.shape[1])/2.0
    # img = cv2.copyMakeBorder(img, int(math.floor(h)), int(math.ceil(h)), int(math.floor(w)), int(math.ceil(w)), cv2.BORDER_CONSTANT, value=0)

    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
    A, P = cv2.cartToPolar(dft[:,:,0], dft[:,:,1])
    L = cv2.log(A)
    h_n = (1./3**2)*np.ones((3,3))
    R = L - cv2.filter2D(L, -1, h_n)
    S = cv2.GaussianBlur(cv2.idft(np.dstack(cv2.polarToCart(cv2.exp(R), P)), flags=cv2.DFT_REAL_OUTPUT)**2, (0,0), 8)
    S = cv2.resize(cv2.normalize(S, None, 0, 1, cv2.NORM_MINMAX), (img_orig.shape[1],img_orig.shape[0]))

    # cv2.namedWindow('tmp1', cv2.WINDOW_NORMAL)
    # cv2.imshow('tmp1', img_orig)
    # cv2.namedWindow('tmp', cv2.WINDOW_NORMAL)
    # cv2.imshow('tmp', S)
    # cv2.waitKey()

    return S
コード例 #20
0
ファイル: hogg_adaboost.py プロジェクト: shimaXX/workspace
    def calculate_integral_HOG(self, image):
        # X, Y方向に微分
        xsobel = np.zeros(image.shape)
        filters.sobel(image,1,xsobel) # 1はaxis=1のこと = x方向
        
        ysobel = np.zeros(image.shape)
        filters.sobel(image,0,ysobel) # 1はaxis=0のこと = y方向
        
        # 角度別の画像を生成しておく
        bins = np.zeros((N_BIN, image.shape[0], image.shape[1]))
         
        # X, Y微分画像を勾配方向と強度に変換
        Imag, Iang = cv2.cartToPolar(xsobel, ysobel, None, None, True) # outputs are magnitude, angle
        # 勾配方向を[0, 180)にする(181~360は0~180に統合する。x軸をまたいだ方向は考えない)
        Iang = (Iang>180)*(Iang-180) + (Iang<=180)*Iang 
        Iang[Iang==360] = 0; Iang[Iang==180] = 0 
        # 勾配方向を[0, 1, ..., 8]にする準備(まだfloat)
        Iang /= THETA
        # 勾配方向を強度で重みをつけて、角度別に投票する
        ind = 0

        for ind in xrange(N_BIN):
            bins[ind] += (np.int8(Iang) == ind)*Imag
        
        # 角度別に積分画像生成
        """ !ここの計算があやしい! """
        integrals = np.array([cv2.integral(bins[i]) for i in xrange(N_BIN)])
        
        return integrals
コード例 #21
0
ファイル: hog_project.py プロジェクト: dgaddy/6.819-project
def hog(img, asHlist=True):
    # Code from OpenCV examples.
    # http://docs.opencv.org/trunk/doc/py_tutorials/py_ml/py_svm/py_svm_opencv/py_svm_opencv.html
    bin_n = 9 # Number of bins
    #grid_n = (12,20)
    grid_n = (8,12)
    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(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = []
    mag_cells = []

    rows, cols = img.shape[:2]
    for i in xrange(grid_n[0]):
        for j in xrange(grid_n[1]):
            rs = slice(i*rows/grid_n[0], (i+1)*rows/grid_n[0])
            cs = slice(j*cols/grid_n[1], (j+1)*cols/grid_n[1])
            bin_cells.append(bins[rs, cs])
            mag_cells.append(mag[rs, cs])
    # bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    # mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    # [[bin1magSum,bin2magSum,...bin9magSum], [bin1magSum,bin2magSum,...,bin9magSum]]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    if asHlist:
        return hist / np.linalg.norm(hist)
    else:
        hists = np.reshape(hists,grid_n + (bin_n,))
        return hists / np.linalg.norm(hist)
コード例 #22
0
def solve_video():
    cap = cv2.VideoCapture('ir.mp4')
    ret, frame = cap.read()
    prev = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    prev = cv2.GaussianBlur(prev, (5, 5), .7).astype(np.float32).view(hmarray)
    hsv = np.zeros_like(frame)
    hsv[..., 1] = 255
    hm_u = hm.zeros_like(prev)
    hm_v = hm.zeros_like(prev)
    while True:
        if frame is None:
            break
        curr = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        curr = cv2.GaussianBlur(curr, (5, 5), .7).astype(np.float32).view(hmarray)
        hm_u, hm_v = hs_jacobi(prev, curr, hm_u, hm_v)
        hm_u.sync_host()
        hm_v.sync_host()
        mag, ang = cv2.cartToPolar(hm_u, hm_v)
        mag = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
        ang = ang*180/np.pi/2
        hsv[..., 0] = ang
        hsv[..., 2] = mag
        flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
        cv2.imshow('flow', flow)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        prev = curr

    cap.release()
コード例 #23
0
ファイル: sandbox.py プロジェクト: legokichi/py_sandbox
def optical_flow_dense():
    to_grayscale = lambda f: cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    params = dict(
        pyr_scale=0.5,
        levels=3,
        winsize=15,
        iterations=3,
        poly_n=5,
        poly_sigma=1.1,
        flags=0
    )

    frames = gen_frames()
    _frame = next(frames)
    p_frame = to_grayscale(_frame)
    hsv = np.zeros_like(_frame)
    hsv[...,1] = 255

    for frame in frames:
        glayed = to_grayscale(frame)
        # calculate optical flow
        flow = cv2.calcOpticalFlowFarneback(p_frame, glayed, None, **params) # type: np.ndarray?
        if flow is None:
            print("none")
            continue
        # optical flow's magnitudes and angles
        mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
        hsv[...,0] = ang*180/np.pi/2
        hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)  # magnitude to 0-255 scale
        frame = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
        p_frame = glayed.copy()
        yield frame
コード例 #24
0
    def process(frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = cv2.pyrDown(frame)

        if d['prev'] is None:
            d['prev'] = frame
        # Computes a dense optical flow
        # using the Gunnar Farneback’s algorithm
        flow = cv2.calcOpticalFlowFarneback(d['prev'],
                                            frame,
                                            pyr_scale=0.5,
                                            levels=1,
                                            winsize=10,
                                            iterations=1,
                                            poly_n=5,
                                            poly_sigma=1.1,
                                            flags=0)
        # Calculates the magnitude and angle of 2D vectors
        mag, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        # threshold it
        _, mag = cv2.threshold(mag, 3, 255, cv2.THRESH_TOZERO)
        # store the current frame
        d['prev'] = frame

        return mag
コード例 #25
0
def solve_single_image():
    frame0 = cv2.imread('images/frame0.png')
    frame1 = cv2.imread('images/frame1.png')
    im0 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY).astype(np.float32).view(hmarray)
    im1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY).astype(np.float32).view(hmarray)

    hm_u = hm.zeros_like(im0)
    hm_v = hm.zeros_like(im0)
    It = zeros_like(hm_u)
    Iy = zeros_like(hm_u)
    Ix = zeros_like(hm_u)
    denom = zeros_like(hm_u)
    new_u = zeros_like(hm_u)
    new_v = zeros_like(hm_u)
    hm_u, hm_v = hs_jacobi(im0, im1, hm_u, hm_v, Ix, Iy, It, denom, new_u, new_v)

    hm_u.sync_host()
    hm_v.sync_host()

    mag, ang = cv2.cartToPolar(hm_u, hm_v)
    mag = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    ang = ang*180/np.pi/2
    hsv = np.zeros_like(frame1)
    hsv[..., 1] = 255
    hsv[..., 0] = ang
    hsv[..., 2] = mag
    flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    cv2.imshow('flow', flow)
    cv2.waitKey()
コード例 #26
0
ファイル: app.py プロジェクト: skyli42/RiceKrispies
def hog(img):
	"""
	Computes the HOG [histogram of oriented gradients] for an image

	Parameters
	----------
	img: np.ndarray
		input image (greyscale)- shape should be (H, W)

	Returns
	-------
	Vector of shape (64, ) that describes the image using gradients

	"""
	gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
	gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
	mag, ang = cv2.cartToPolar(gx, gy)

	# quantizing binvalues in (0...16)
	bins = np.int32(bin_n*ang/(2*np.pi))
	print(bins)
	# Divide to 4 sub-squares
	bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
	mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
	hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)] #(4, 16)
	hist = np.hstack(hists) #(4, 16) --> (64,)
	return hist
コード例 #27
0
ファイル: tracking.py プロジェクト: TakaharuSuzuki/Tracking
def opticalFlowCalc(input, input_pre, input_hsv):
    flow = cv2.calcOpticalFlowFarneback(input, input_pre, 0.5, 3, 15, 3, 5, 1.2, 0)
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    input_hsv[...,0] = ang*180/np.pi/2
    input_hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    output = cv2.cvtColor(input_hsv,cv2.COLOR_HSV2BGR)
    return output, input
コード例 #28
0
def make_optFlow(fileName,BW):
    print "Now processing: "+fileName.split("/")[5]
    cap = cv2.VideoCapture(fileName)

    #Start reading frames
    ret, frame1 = cap.read()
    prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    hsv = np.zeros_like(frame1)
    hsv[...,1] = 255

    #Checking the number of frames in the video
    length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    x = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
    y = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))

    tmp = int(length / 30)

    if(tmp==0):
        tmp = 4
    valid_frames = [tmp*i for i in xrange(0,length/tmp)]

    #Open a videowriter object
    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    if(not(BW)):
        newFileName = "../../data/pre-process/optFlow_BW/"+fileName.split('/')[4]+"/"+fileName.split('/')[5].split('.')[0]+"_optFlow.avi"
        out = cv2.VideoWriter(newFileName ,fourcc, 20, (x,y))
    else:
        newFileName = "../../data/pre-process/optFlow_BW/"+fileName.split('/')[4]+"/"+fileName.split('/')[5].split('.')[0]+"_BW_optFlow.avi"
        out = cv2.VideoWriter(newFileName ,fourcc, 6, (x,y),0)

    frame_num = 0
    #Loop through all of the frames and calculate optical flow
    while(1):
        frame_num += 1
        ret, frame2 = cap.read()

        #If no more frames can be read then break out of our loop
        if(not(ret)):
            break
        if(frame_num in valid_frames):
            next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

            flow = cv2.calcOpticalFlowFarneback(prvs,next, 0.5, 3, 15, 3, 5, 1.2, 0)

            mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
            hsv[...,0] = ang*180/np.pi/2
            hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
            rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)

            if(not(BW)):
                out.write(rgb)
            else:
                bw = cv2.cvtColor(rgb,cv2.COLOR_RGB2GRAY)
                out.write(bw)

            prvs = next

    #Close the file
    cap.release()
    out.release()
コード例 #29
0
ファイル: utils.py プロジェクト: alvarouc/aided_objectTracker
 def demo(self):
     'Demo for Optical flow, draw flow vectors and write to video'
     
     with CaptureElement(self.video_path) as ce, WriteElement(self.video_path) as we:
         pbar = startBar("Computing flow vectors", len(ce.frames))
         hsv = np.zeros_like(ce.frames[0])
         hsv[...,1] = 255
         for n, img in enumerate(ce.frames):
             # First run
             if n < 1:
                 prev_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
             else:
                 current_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                 flow = cv2.calcOpticalFlowFarneback(prev_frame, current_frame, 0.5, 3, 15, 3, 5, 1.2, 0)
                 
                 # Reformatting for display
                 mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
                 hsv[...,0] = ang*180/np.pi/2
                 hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
                 rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
                 we.write_frame(rgb)
                 
                 prev_frame = current_frame.copy()
             pbar.update(n)
     pbar.finish()
コード例 #30
0
ファイル: filters.py プロジェクト: hmercuryg/video-analysis
    def _compare_frames(self, this_frame, prev_frame):
        flow = cv2.calcOpticalFlowFarneback(
            prev_frame, this_frame, pyr_scale=0.5, levels=3, winsize=2, iterations=3, poly_n=5, poly_sigma=1.2, flags=0
        )

        mag, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1])

        return mag
コード例 #31
0
def HOGBikin(image):
    # Read image
    try:
        visualise = True
        image = numpy.atleast_2d(image)
        height, width = image.shape

        gx = numpy.zeros(image.shape)
        gy = numpy.zeros(image.shape)

        #robert
        #gx[:, :-1] = numpy.diff(image, n=1, axis=1)
        #gy[:-1, :] = numpy.diff(image, n=1, axis=0)

        #prewitt
        #for i in range(height):  # 128
        #    for j in range(width):  # 64
        #        if (i == 0) or (i == height - 1):
        #            gy[i, j] = 0
        #        else:
        #            gy[i, j] = image[(i + 1), j] - image[(i - 1), j]

        #for i in range(height):  # 128
        #    for j in range(width):  # 64
        #        if (j == 0) or (j == width - 1):
        #            gx[i, j] = 0
        #        else:
        #            gx[i, j] = image[i, (j + 1)] - image[i, (j - 1)]

        #magnitude = sqrt(gx ** 2 + gy ** 2)
        #angle = arctan2(gy, (gx + 1e-15)) * (180 / pi)

        #sobel
        gx = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=5)
        gy = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=5)

        magnitude, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)

        cell_size = 8
        block_size = 2
        orientations = 9
        sx, sy = image.shape
        cx, cy = (cell_size, cell_size)
        bx, by = (block_size, block_size)

        n_cellsx = int(numpy.floor(sx // cx))  # number of cells in x
        n_cellsy = int(numpy.floor(sy // cy))  # number of cells in y
        # compute orientations integral images
        orientation_histogram = numpy.zeros((n_cellsx, n_cellsy, orientations))
        temp_angle = numpy.zeros(shape=(sx, sy))
        temp_mag = numpy.zeros(shape=(sx, sy))
        for i in range(n_cellsx):  #baris
            for j in range(n_cellsy):  #kolom

                for o in range(orientations):
                    temp_bins = 0
                    for ii in range(i * cell_size, (i + 1) * cell_size):
                        for jj in range(j * cell_size, (j + 1) * cell_size):
                            temp_angle[ii, jj] = numpy.where(
                                angle[ii, jj] < 180 / orientations * (o + 1),
                                angle[ii, jj], 0)
                            temp_angle[ii, jj] = numpy.where(
                                angle[ii, jj] >= 180 / orientations * o,
                                temp_angle[ii, jj], 0)
                            # select magnitudes for those orientations
                            cond2 = temp_angle[ii, jj] > 0
                            temp_mag[ii,
                                     jj] = numpy.where(cond2, magnitude[ii,
                                                                        jj], 0)
                            temp_bins += temp_mag[ii, jj]
                    #print("temp_angle cell baris",i," kolom ",j," bins ",o)
                    #print(temp_angle)
                    #print("temp_mag cell baris", i, " kolom ", j, " bins ", o)
                    #print(temp_mag)
                    orientation_histogram[i, j, o] = temp_bins
                    #print(orientation_histogram[i, j, o])

        hog_image = None

        if visualise:
            from skimage import draw

            radius = min(cx, cy) // 2 - 1
            orientations_arr = numpy.arange(orientations)
            # set dr_arr, dc_arr to correspond to midpoints of orientation bins
            orientation_bin_midpoints = (numpy.pi * (orientations_arr + .5) /
                                         orientations)
            dr_arr = radius * numpy.sin(orientation_bin_midpoints)
            dc_arr = radius * numpy.cos(orientation_bin_midpoints)
            hog_image = numpy.zeros((sx, sy), dtype=float)
            for r in range(n_cellsx):
                for c in range(n_cellsy):
                    for o, dr, dc in zip(orientations_arr, dr_arr, dc_arr):
                        centre = tuple([r * cx + cx // 2, c * cy + cy // 2])
                        rr, cc = draw.line(int(centre[0] - dc),
                                           int(centre[1] + dr),
                                           int(centre[0] + dc),
                                           int(centre[1] - dr))
                        hog_image[rr, cc] += orientation_histogram[r, c, o]

        n_blocksx = (n_cellsx - bx) + 1
        n_blocksy = (n_cellsy - by) + 1
        normalised_blocks = numpy.zeros(
            (n_blocksx, n_blocksy, bx, by, orientations))

        for x in range(n_blocksx):
            for y in range(n_blocksy):
                block = orientation_histogram[x:(x + bx), y:(y + by), :]
                #print(block.shape)
                eps = 1e-5
                normalised_blocks[
                    x,
                    y, :] = block / numpy.sqrt(numpy.sum(block**2) + eps**2)

        if visualise:
            return normalised_blocks.ravel(), hog_image
        else:
            return normalised_blocks.ravel()
    except:
        print(traceback.format_exc())
        pass
コード例 #32
0
def run_yolo_prediction(args): 
  model = tf.keras.models.load_model(model_path)
  net = cv2.dnn.readNet(weights_path, testing_cfg_path)
  classes = []
  with open(classes_path, "r") as f:
    classes = f.read().splitlines()

  # test with a video
  if args['video']:
    print(f"video used: {args['video']}")
    cap = cv2.VideoCapture(f"./detection/input/{args['video']}")
      
  # to save video
  if args['save']:
      fourcc = cv2.VideoWriter_fourcc(*'mp4v')
      if args['video']:
          out = cv2.VideoWriter(f"./detection/output/out_{args['video']}", fourcc, 30.0, (540,960))

  threshold = 0.5

  ret, frame1 = cap.read()
  prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
  hsv = np.zeros_like(frame1)
  hsv[...,1] = 255

  count = 0
  up_bool = False
  down_bool = False
  first_up = False
  frames = 0
  # good_back = 0
  # bad_back = 0
  num_labels = 0
  total_confidence = 0
  form = 100.0
  rep_form = []
  prvs_labels = []

  # Initialize text style
  height, width, _ = frame1.shape
  font = cv2.FONT_HERSHEY_SIMPLEX
  fontScale = 2
  fontColor = (255,255,255)
  lineType = 2
  bottomLeftCornerOfText = (10, int(height - (height / 40 + fontScale * height / 40)))
  formPosition = (10, int(height - height / 40))

  while True:
      _,img = cap.read()
      if img is None:
          break

      # 1. REP COUNTER
      next = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      
      # Every 10 frames
      if (frames % 10 == 0):
          flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) # What do these numbers represent?
          mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
          hsv[...,0] = ang*180/np.pi/2
          hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
          rgb1 = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
          rgb = rescale_frame(rgb1)
          rgb = rgb.reshape((1,256,256,3))

          prediction = model.predict(rgb, batch_size=1)
          predict_label = np.argmax(prediction, axis=-1)
          label = labels[int(predict_label)]
          if len(prvs_labels) >= 3:
            prvs_labels.pop(0)
          prvs_labels.append(label)
          
          # Identify first rep (two up frames in a row)
          if prvs_labels[-2:] == ['up', 'up'] and not first_up:
              first_up = True
          # First down within the new rep
          elif prvs_labels[-2:] == ['down', 'down'] and first_up:
              down_bool = True
          # First up within the new rep
          elif prvs_labels[0] == 'down' and 'down' not in prvs_labels[-2:] and down_bool:
              up_bool = True
          
          # Reset and start a new rep
          if up_bool and down_bool:
              rep_form.append(form)
              count += 1
              up_bool = False
              down_bool = False
              num_labels = 0
              total_confidence = 0
              # good_back = 0
              # bad_back = 0
              form = 100.0
      
      # Add count text
      cv2.putText(img, f'Count: {count}', bottomLeftCornerOfText, font, fontScale, fontColor, lineType)
      
      # Good form defined as more than 70% good labels within given rep
      # Good form defined as an average confidence score of more than 0.5 within given rep
      if (form >= 50):
        formColor = [0,255,0]
      else:
        formColor = [0,0,255]

      # Add form (%) text
      cv2.putText(img, f'Form: {form}%', formPosition, font, fontScale, formColor, lineType)

      # Add form summary text
      for i in range(len(rep_form)):
        # Add form for each completed rep
        x = 10
        y = int(height / 40 + i * fontScale * height / 40 * 0.7)
        if rep_form[i] >= 50:
          repColor = [0,255,0]
        else:
          repColor = [0,0,255]

        cv2.putText(img, f'Rep {i+1}: {rep_form[i]}%', (x, y), font, int(fontScale * 0.7), repColor, lineType)
      
      # 2. ROUNDED BACK DETECTION
      height, width, _ = img.shape
      blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0,0,0), swapRB=True, crop=False) #convert image to fit into the model
      net.setInput(blob) #fit image into the model
      output_layers_names = net.getUnconnectedOutLayersNames()
      layerOutputs = net.forward(output_layers_names) #forward pass to get outputs

      boxes = []
      confidences = []
      class_ids = []
      for output in layerOutputs: # for each box segment
          for detection in output: # for each object detected
              # detection[0:3] first 4 values represents the bounding boxes, detection[4] confidence scores
              scores = detection[5:]
              class_id = np.argmax(scores)
              confidence = scores[class_id]
              if confidence > threshold:
                  center_x = int(detection[0]*width)
                  center_y = int(detection[1]*height)
                  w = int(detection[2]*width)
                  h = int(detection[3]*height)

                  x = int(center_x - w/2)
                  y = int(center_y - h/2)

                  boxes.append([x, y, w, h])
                  confidences.append((float(confidence)))
                  class_ids.append(class_id)
      indexes = cv2.dnn.NMSBoxes(boxes, confidences, threshold, 0.4)
      if len(indexes)>0 and first_up:
          for i in indexes.flatten():
              x, y, w, h = boxes[i]
              label = str(classes[class_ids[i]])
              confidence_score = round(confidences[i],2)
              confidence = str(confidence_score)

              # set red color for rounded back
              if (label == 'rounded_back'):
                  # bad_back += 1
                  total_confidence += 1 - confidence_score
                  num_labels += 1
                  color = [0,0,255]
                  cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
                  cv2.putText(img, f'{label}', (x, y-35), font, 1, color, 2)
                  cv2.putText(img, f'{confidence}', (x, y-5), font, 1, color, 2)
              # set green color for straight back
              elif (label == 'straight_back'):
                  # good_back += 1
                  total_confidence += confidence_score
                  num_labels += 1
                  color = [0,255,0]
                  cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
                  cv2.putText(img, f'{label}', (x, y-35), font, 1, color, 2)
                  cv2.putText(img, f'{confidence}', (x, y-5), font, 1, color, 2)
              # rep form (percentage of frames with good labels)
              # form = round(good_back / (good_back + bad_back) * 100, 2)

              # rep form (average confidence of good back within the rep)
              form = round(total_confidence / num_labels * 100, 2)

      img = cv2.resize(img,(540,960))
      if args['save']:
          out.write(img)
    #   cv2.imshow('Image', img)
      key = cv2.waitKey(1)
      if key==27:
          break
      prvs = next
      frames += 1
  cap.release()
  cv2.destroyAllWindows()
コード例 #33
0
# img = cv2.imread('clouds.jpg',0)
#
# # create a CLAHE object (Arguments are optional).
# clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
# cl1 = clahe.apply(img)
#
# cv2.imwrite('clahe_2.jpg',cl1)
# cv2.imshow(mat,'clahe_2.jpg')
# img_he = cv2.imread('clahe_2.jpg',0)
# cv2.imshow('clahe_2.jpg')
img = cv2.imread('tir_v1_0_8bit/trees/00000001.png', 0)
# cv2.imshow("input",img)
equ = cv2.equalizeHist(img)
# res = np.hstack((equ)) #stacking images side-by-side
# cv2.imshow("hist eq",equ)
cv2.imwrite('res.jpg', equ)
dft = cv2.dft(np.float32(equ), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum = 20 * np.log(
    cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
mag, angl = cv2.cartToPolar(dft_shift[:, :, 0], dft_shift[:, :, 1])
print(angl)
print(mag)
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
コード例 #34
0
def getFarnebackOFVideo(srcVideoPath, grid_size):
    """
    Function to get the Farneback dense optical flow features for the video file
    and sample magnitude and angle features with a distance of grid_size between 
    two neighbours.
    Copied and editted from shot_detection.py script
    
    Parameters:
    ------
    srcVideoPath: str
        complete path of a video file
    grid_size: int
        distance between two consecutive sampling pixels.
    
    Returns:
    ------
    np.ndarray of dimension (N-1, 2 x (240/grid_size) x (320/grid_size))
    """
    # get the VideoCapture object
    cap = cv2.VideoCapture(srcVideoPath)

    # if the videoCapture object is not opened then return None
    if not cap.isOpened():
        print("Error reading the video file !!")
        return None


#    dimensions = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), \
#                  int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#    totalFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    frameCount = 0
    features_current_file = []

    ret, prev_frame = cap.read()
    assert ret, "Capture object does not return a frame!"
    prev_frame = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)

    # Iterate over the entire video to get the optical flow features.
    while (cap.isOpened()):
        frameCount += 1
        ret, curr_frame = cap.read()
        if not ret:
            break
        curr_frame = cv2.cvtColor(curr_frame, cv2.COLOR_BGR2GRAY)
        flow = cv2.calcOpticalFlowFarneback(prev_frame, curr_frame, None, 0.5,
                                            3, 15, 3, 5, 1.2, 0)
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        # stack sliced arrays along the first axis (2, (360/grid), (640/grid))
        sliced_flow = np.stack(( mag[::grid_size, ::grid_size], \
                                ang[::grid_size, ::grid_size]), axis=0)

        # For extracting only magnitude features, uncomment the following
        #sliced_flow = mag[::grid_size, ::grid_size]

        #feature.append(sliced_flow[..., 0].ravel())
        #feature.append(sliced_flow[..., 1].ravel())
        # saving as a list of float values (after converting into 1D array)
        #features_current_file.append(sliced_flow.ravel().tolist())   #slow at load time
        # convert to (1, 2x(H/grid)x(W/grid)) matrix.
        sliced_flow = np.expand_dims(sliced_flow.flatten(), axis=0)
        features_current_file.append(sliced_flow)
        prev_frame = curr_frame

    cap.release()
    #print "{}/{} frames in {}".format(frameCount, totalFrames, srcVideoPath)
    return np.array(features_current_file)  # (N-1, 1, 2x(H/grid)x(W/grid))
コード例 #35
0
ファイル: evaluation.py プロジェクト: Saimshaikh8297/eysip
def evaluation(img_akaze, img_circle):

    ##################################### PROGRAMMATIC ANALYSIS OF CHECK-POINTS #########################################

    # load the image and convert it to grayscale
    img_perfect = cv2.imread("team_id_2_comparison.png")

    coordinates = [[29.5, 250.0], [38.0, 385.0], [160.97999572753906, 417.5],
                   [114.12354278564453, 338.9093322753906], [88.5, 259.0],
                   [158.53448486328125, 202.6724090576172], [187.5, 38.5],
                   [261.2481384277344, 121.8302230834961], [270.5, 243.0],
                   [291.4565124511719, 422.2826232910156],
                   [387.043701171875, 360.78155517578125], [343.0, 274.5],
                   [362.0, 166.5]]

    feature_list = [636, 395, 1046, 500, 1605]

    # programmatic checkpoints
    circle_radius = 8
    check_list = []
    check_counter = 0

    for i in coordinates:
        i[0] = int(i[0])
        i[1] = int(i[1])

        roi = img_circle[i[1] - (3 * circle_radius):i[1] + (3 * circle_radius),
                         i[0] - (3 * circle_radius):i[0] + (3 * circle_radius)]
        roi = roi.reshape(int(roi.size / 3), 3)

        if [255, 255, 255] in roi.tolist():
            check_list.append(1)
            check_counter += 1

        else:
            check_list.append(0)

    check_result = ((check_counter / check_list.__len__()) * 100)

    print("Programmatic Analysis Result = ")
    print(check_result)

    ####################################### ANALYSIS USING FEATURE MATCHING ##################################################

    # load the image and convert it to grayscale
    gray1 = cv2.cvtColor(img_perfect, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(img_akaze, cv2.COLOR_BGR2GRAY)

    # initialize the AKAZE descriptor, then detect keypoints and extract
    # local invariant descriptors from the image
    sift = cv2.xfeatures2d.SIFT_create()
    surf = cv2.xfeatures2d.SURF_create()
    akaze = cv2.AKAZE_create()
    brisk = cv2.BRISK_create()
    orb = cv2.ORB_create()

    (akazekps1, akazedescs1) = akaze.detectAndCompute(gray1, None)
    (akazekps2, akazedescs2) = akaze.detectAndCompute(gray2, None)
    (siftkps1, siftdescs1) = sift.detectAndCompute(gray1, None)
    (siftkps2, siftdescs2) = sift.detectAndCompute(gray2, None)
    (surfkps1, surfdescs1) = surf.detectAndCompute(gray1, None)
    (surfkps2, surfdescs2) = surf.detectAndCompute(gray2, None)
    (briskkps1, briskdescs1) = brisk.detectAndCompute(gray1, None)
    (briskkps2, briskdescs2) = brisk.detectAndCompute(gray2, None)
    (orbkps1, orbdescs1) = orb.detectAndCompute(gray1, None)
    (orbkps2, orbdescs2) = orb.detectAndCompute(gray2, None)

    #print("No of KeyPoints:")
    #print("akazekeypoints1: {}, akazedescriptors1: {}".format(len(akazekps1), akazedescs1.shape))
    #print("akazekeypoints2: {}, akazedescriptors2: {}".format(len(akazekps2), akazedescs2.shape))
    #print("siftkeypoints1: {}, siftdescriptors1: {}".format(len(siftkps1), siftdescs1.shape))
    #print("siftkeypoints2: {}, siftdescriptors2: {}".format(len(siftkps2), siftdescs2.shape))
    #print("surfkeypoints1: {}, surfdescriptors1: {}".format(len(surfkps1), surfdescs1.shape))
    #print("surfkeypoints2: {}, surfdescriptors2: {}".format(len(surfkps2), surfdescs2.shape))
    #print("briskkeypoints1: {}, briskdescriptors1: {}".format(len(briskkps1), briskdescs1.shape))
    #print("briskkeypoints2: {}, briskdescriptors2: {}".format(len(briskkps2), briskdescs2.shape))
    #print("orbkeypoints1: {}, orbdescriptors1: {}".format(len(orbkps1), orbdescs1.shape))
    #print("orbkeypoints2: {}, orbdescriptors2: {}".format(len(orbkps2), orbdescs2.shape))

    # Match the fezatures
    bfakaze = cv2.BFMatcher(cv2.NORM_HAMMING)
    bf = cv2.BFMatcher(cv2.NORM_L2)
    akazematches = bfakaze.knnMatch(akazedescs1, akazedescs2, k=2)
    siftmatches = bf.knnMatch(siftdescs1, siftdescs2, k=2)
    surfmatches = bf.knnMatch(surfdescs1, surfdescs2, k=2)
    briskmatches = bf.knnMatch(briskdescs1, briskdescs2, k=2)
    orbmatches = bf.knnMatch(orbdescs1, orbdescs2, k=2)

    # Apply ratio test on AKAZE matches
    goodakaze = []
    for m, n in akazematches:
        if m.distance < 0.9 * n.distance:
            goodakaze.append([m])

    im3akaze = cv2.drawMatchesKnn(img_perfect,
                                  akazekps1,
                                  img_akaze,
                                  akazekps2,
                                  goodakaze[:100],
                                  None,
                                  flags=2)
    cv2.imshow("AKAZE matching", im3akaze)
    goodakaze = np.asarray(goodakaze)
    print("akaze")
    similarity_akaze = (goodakaze.shape[0] / feature_list[0]) * 100
    print(similarity_akaze)

    # Apply ratio test on SIFT matches
    goodsift = []
    for m, n in siftmatches:
        if m.distance < 0.9 * n.distance:
            goodsift.append([m])

    im3sift = cv2.drawMatchesKnn(img_perfect,
                                 siftkps1,
                                 img_akaze,
                                 siftkps2,
                                 goodsift[:],
                                 None,
                                 flags=2)
    cv2.imshow("SIFT matching", im3sift)
    goodsift = np.asarray(goodsift)
    print("sift")
    similarity_sift = (goodsift.shape[0] / feature_list[1]) * 100
    print(similarity_sift)

    # Apply ratio test on SURF matches
    goodsurf = []
    for m, n in surfmatches:
        if m.distance < 0.9 * n.distance:
            goodsurf.append([m])

    im3surf = cv2.drawMatchesKnn(img_perfect,
                                 surfkps1,
                                 img_akaze,
                                 surfkps2,
                                 goodsurf[:],
                                 None,
                                 flags=2)
    cv2.imshow("SURF matching", im3surf)
    goodsurf = np.asarray(goodsurf)
    print("surf")
    similarity_surf = (goodsurf.shape[0] / feature_list[2]) * 100
    print(similarity_surf)

    # Apply ratio test on ORB matches
    goodorb = []
    for m, n in orbmatches:
        if m.distance < 0.9 * n.distance:
            goodorb.append([m])
    im3orb = cv2.drawMatchesKnn(img_perfect,
                                orbkps1,
                                img_akaze,
                                orbkps2,
                                goodorb[:],
                                None,
                                flags=2)
    cv2.imshow("ORB matching", im3orb)
    goodorb = np.asarray(goodorb)
    print("orb")
    similarity_orb = (goodorb.shape[0] / feature_list[3]) * 100
    print(similarity_orb)

    # Apply ratio test on BRISK matches
    goodbrisk = []
    for m, n in briskmatches:
        if m.distance < 0.9 * n.distance:
            goodbrisk.append([m])

    im3brisk = cv2.drawMatchesKnn(img_perfect,
                                  briskkps1,
                                  img_akaze,
                                  briskkps2,
                                  goodbrisk[:],
                                  None,
                                  flags=2)
    cv2.imshow("BRISK matching", im3brisk)
    goodbrisk = np.asarray(goodbrisk)
    print("brisk")
    similarity_brisk = (goodbrisk.shape[0] / feature_list[4]) * 100
    print(similarity_brisk)
    features_result = (similarity_akaze + similarity_brisk + similarity_orb +
                       similarity_sift + similarity_surf) / 5
    print("Overall similarity using features: ")
    print()

    ######################################### HOG CORRELATION ###############################################

    bin_n = 16
    #img = cv2.imread("not_perfect_trajectory2.png")
    gx = cv2.Sobel(img_perfect, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img_perfect, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    # quantizing binvalues in (0...16)
    bins = np.int32(bin_n * ang / (2 * np.pi))
    # Divide to 4 sub-squares
    bin_cells = bins[:10, :10], bins[10:, :10], bins[:10, 10:], bins[10:, 10:]
    mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
    hists = [
        np.bincount(b.ravel(), m.ravel(), bin_n)
        for b, m in zip(bin_cells, mag_cells)
    ]
    hist1 = np.hstack(hists)

    rows, cols, _ = img_akaze.shape
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 0, 1)
    img_akaze = cv2.warpAffine(img_akaze, M, (cols, rows))
    gx = cv2.Sobel(img_akaze, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img_akaze, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    # quantizing binvalues in (0...16)
    bins = np.int32(bin_n * ang / (2 * np.pi))
    # Divide to 4 sub-squares
    bin_cells = bins[:10, :10], bins[10:, :10], bins[:10, 10:], bins[10:, 10:]
    mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
    hists = [
        np.bincount(b.ravel(), m.ravel(), bin_n)
        for b, m in zip(bin_cells, mag_cells)
    ]
    hist2 = np.hstack(hists)

    hog_result = (np.corrcoef((hist1, hist2)[0][1]) * 100)
    print("HOG CORRELATION RESULT = ")
    print(hog_result)

    return (check_result, features_result, hog_result)

    cv2.imshow("image_akaze", img_akaze)
    cv2.imshow("img_circle", img_circle)
    cv2.waitKey(0)
コード例 #36
0
    hsv = np.zeros_like(frame1)
    hsv[:, :, 1] = 255

    flow = cv2.calcOpticalFlowFarneback(frame1_gray,
                                        frame2_gray,
                                        flow=None,
                                        pyr_scale=0.5,
                                        levels=3,
                                        winsize=15,
                                        iterations=3,
                                        poly_n=5,
                                        poly_sigma=1.2,
                                        flags=0)

    mag, ang = cv2.cartToPolar(flow[:, :, 0], flow[:, :, 1])
    #angle of movement
    hsv[:, :, 0] = ang * 180 / np.pi / 2
    #strength of movement
    hsv[:, :, 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

    cv2.imwrite(path + "flow" + str(i) + ".jpg", rgb)

    frame1_gray = frame2_gray

#%%

cv2.imwrite(path + "test.png", rgb[:, :, 2])

#%%
コード例 #37
0
def extract_features(camera, align_mode=None):
    preds = []

    windowSize = 100
    displacement = 0
    figure = plt.figure(figsize=(10, 10))
    ax = plt.axes(xlim=(0, 10), ylim=(340, 600))
    #plt.xlim(0,10)
    plt.ylim(0, 3)
    plot_data, = ax.plot([], [])

    plt.ion()

    #aligner = FaceAligner()
    #prvs = cv.cvtColor(frame1,cv.COLOR_BGR2GRAY)

    prev = None
    init_detect_pos = None
    #plt.draw()

    while camera.isOpened():
        ret, frame = camera.read()

        mean_mag = 0
        if not ret:
            break

        orig_frame = frame.copy()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_detection.detectMultiScale(gray,
                                                scaleFactor=1.5,
                                                minNeighbors=5,
                                                minSize=(100, 100),
                                                maxSize=(300, 300),
                                                flags=cv2.CASCADE_SCALE_IMAGE)

        roi = None
        roi_col = None
        if align_mode == "facenet_align":
            aligned = aligner.detect(frame)
            if aligned is not None:
                cv2.imshow('algined', aligned)
            roi = cv2.cvtColor(aligned, cv2.COLOR_BGR2GRAY)
            roi_col = aligned
            #aligned = misc.imresize(cropped, (160, 160), interp='bilinear')

        #dets = detector(orig_frame, 1)

        if roi is None and len(faces) > 0:
            face = faces[0]
            x, y, w, h = face
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255))
            kernel = np.ones((5, 5), np.uint8)  #cleanup a bit
            orig_frame = cv2.dilate(orig_frame, kernel, iterations=1)
            #extract roi, get region of interest
            roi = gray[y:y + h, x:x + w]
            roi_col = orig_frame[y:y + h, x:x + w]

            if init_detect_pos is None or adjust_counter == re_adjust_frame:
                init_detect_pos = x, y, w, h
                adjust_counter = 0
            else:
                x, y, w, h = init_detect_pos
                roi_col = orig_frame[y:y + h, x:x + w]
            adjust_counter += 1

        if roi is not None:

            roi = cv2.resize(roi, (64, 64))
            roi_col = cv2.resize(roi_col, (164, 164))

            if prev is None:
                prev = cv2.cvtColor(roi_col, cv2.COLOR_BGR2GRAY)
                hsv = np.zeros_like(roi_col)
                hsv[..., 1] = 255
                print(hsv.shape)
            else:
                #nextImg = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
                nextImg = cv2.cvtColor(roi_col, cv2.COLOR_BGR2GRAY)
                flow = cv2.calcOpticalFlowFarneback(prev, nextImg, None, 0.5,
                                                    3, 15, 3, 5, 1.2, 0)
                mag, ang = cv2.cartToPolar(flow[..., 0], 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.imshow('optical_flow', bgr)
                mean_mag = np.mean(mag)

                xvals = np.arange(len(preds))

                x_start = 0
                x_end = 0
                if len(xvals) > 0:
                    x_end = xvals[-1] + 3
                x_start = x_end - windowSize + displacement
                if x_start < 0:
                    x_start = 0
                if (x_end < windowSize):
                    x_end = windowSize
                ax.set_xlim(x_start, x_end)

                #sc.set_offsets(np.c_[xvals,preds])
                plot_data.set_data(xvals, preds)
                figure.canvas.draw_idle()
                plt.pause(0.01)

                #plt.plot([0,0,1],[1,1,0])
                #print(mean_mag)
                prev = nextImg

            #plt.plot()
            #plt.draw()
            #roi = roi.astype("float") / 255.0
            #roi = img_to_array(roi)
            #roi = np.expand_dims(roi, axis=0)

            #print(label, emotion_probability, len(faces))

        preds.append(mean_mag)
        #output
        cv2.imshow("Probabilities", frame)
        if roi is not None:
            cv2.imshow("Face", roi)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('a'):
            break

        #cv2.imshow('your_face', frameClone)
        #cv2.imshow("Probabilities", canvas)
        #if cv2.waitKey(1) & 0xFF == ord('q'):
        #	break
    plt.close("all")
    camera.release()
    return preds
コード例 #38
0
import cv2
import numpy as np

img = cv2.imread('63063.jpg')

rows, cols = img.shape[:2]

exp = 1.5
scale = 1

mapy, mapx = np.indices((rows, cols), dtype=np.float32)

mapx = 2 * mapx / (cols - 1) - 1
mapy = 2 * mapy / (rows - 1) - 1

r, theta = cv2.cartToPolar(mapx, mapy)

r[r > scale] = r[r > scale]**exp

mapx, mapy = cv2.polarToCart(r, theta)

mapx = ((mapx + 1) * cols - 1) / 2
mapy = ((mapy + 1) * rows - 1) / 2

distorted = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)

cv2.imshow('dis', distorted)
cv2.waitKey()
cv2.destroyAllWindows()
コード例 #39
0
def detect_video(yolo, video_path):
    vid = cv2.VideoCapture(video_path)
    if not vid.isOpened():
        raise IOError("Couldn't open webcam or video")
    accum_time = 0
    curr_fps = 0
    fps = "FPS: ??"
    prev_time = timer()
    #yolo.close_session()
    #Optical flow initilize

    return_value, old_frame = vid.read()
    if(optical_flow_enable):
        old_frame = cv2.resize(old_frame, (0,0), fx=0.4, fy=0.4)
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
    # Create a mask image for drawing purposes
    arrows = np.zeros_like(old_frame)
    #Optical flow end
    frameCount = 1
    startFrame = 10
    #bbox = (287, 23, 86, 320)
    boxes = []
    while True:
        return_value, frame = vid.read()
        frameCount += 1
        if(optical_flow_enable):
            frame = cv2.resize(frame, (0,0), fx=0.4, fy=0.4)
        image = Image.fromarray(frame)
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        image, boxes = yolo.detect_image(image)
        img = frame.copy()
        #(boxes)
        #Optical flow start=====================================================

        # calculate optical flow
        if(optical_flow_enable):
            flow = cv2.calcOpticalFlowFarneback(old_gray, frame_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
            arrows = np.zeros_like(frame)
            mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
            i = 0
            mag_norm = cv2.normalize(mag, None, 0, 10, cv2.NORM_MINMAX)
            # draw the tracks
            """for x in range(mag.shape[1]):
                for y in range(mag.shape[0]):
                    ang_norm = ang[y][x] * 180 / (np.pi)

                    x2 = int(x + mag_norm[y][x] * math.cos(ang_norm))
                    y2 = int(y + mag_norm[y][x] * math.sin(ang_norm))
                    if(i % 50 == 0):
                        arrows = cv2.arrowedLine(arrows, (x, y), (x2, y2), color[1].tolist(), 1)
                    i += 1
            img = cv2.add(frame,arrows)"""
            img = np.asarray(img)
            img, arrows = cropped_img(img, boxes, mag_norm, ang)
            img = cv2.add(frame,arrows)
        #cv2.imshow('frame',img)
        # Now update the previous frame and previous points
        old_gray = frame_gray.copy()
        #Optical flow end=======================================================

        #Yolo implementation ===================================================
        result = np.asarray(image)
        curr_time = timer()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1
        if accum_time > 1:
            accum_time = accum_time - 1
            fps = "FPS: " + str(curr_fps)
            curr_fps = 0
        cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=0.50, color=(255, 0, 0), thickness=2)
        #cv2.namedWindow("result", cv2.WINDOW_NORMAL)
        if(optical_flow_enable):
            result = image_overlay(result, img, boxes)
        cv2.imshow("result", result)
        #Yolo implementation end ===============================================

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    yolo.close_session()
コード例 #40
0
def bg_sub(filename, file_no, final_res):
    try:
        #filename='jogging/person16_jogging_d1_uncomp.avi'
        ct = 0
        res = []
        cap = cv2.VideoCapture(filename)
        n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        #print(n_frames)

        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(cap.get(cv2.CAP_PROP_FPS))

        lbp_res = np.ndarray(shape=(5, h, w))

        feature_params = dict(maxCorners=100,
                              qualityLevel=0.3,
                              minDistance=7,
                              blockSize=7)

        # Parameters for lucas kanade optical flow
        lk_params = dict(winSize=(w, h),
                         maxLevel=2,
                         criteria=(cv2.TERM_CRITERIA_EPS
                                   | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

        color = np.random.randint(0, 255, (100, 3))

        #fourcc = cv2.VideoWriter_fourcc('*MJPG')
        #videoout = cv2.VideoWriter(out_loc, fourcc, fps, (w, h))
        _, frame = cap.read()
        old_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        start_time = time.time()

        p0 = cv2.goodFeaturesToTrack(old_frame, mask=None, **feature_params)
        mask = np.zeros_like(frame)

        ret, thresh = cv2.threshold(frame, 127, 255, 0)
        fgbg = cv2.createBackgroundSubtractorMOG2()
        for i in range(n_frames - 2):
            ret, frame = cap.read()
            if (ct < 5):
                lbp1 = lbp(frame)
                #print(lbp1.shape)
                lbp_res[ct] = lbp1

            ct = ct + 1
            new_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            if ret is False:
                print("Cannot read video stream")
                break
            fgmask = fgbg.apply(new_frame)
            fg_frame = np.float32(fgmask) / 255.0

            gx = cv2.Sobel(fg_frame, cv2.CV_32F, 1, 0, ksize=1)
            gy = cv2.Sobel(fg_frame, cv2.CV_32F, 0, 1, ksize=1)

            mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)

            if (i == 0):
                sum_mag = np.zeros(shape=mag.shape, dtype=mag.dtype)
                sum_angle = np.zeros(shape=angle.shape, dtype=angle.dtype)

                sum_mag = np.add(sum_mag, mag)
                sum_angle = np.add(sum_angle, angle)

            else:
                sum_mag = np.add(sum_mag, mag)
                sum_angle = np.add(sum_angle, angle)
            '''M = cv2.moments(fgmask)
			if i==0:
				init_cX=int(M["m10"] / M["m00"])
				init_cY=int(M["m01"] / M["m00"])
				cv2.circle(fgmask, (init_cX, init_cY), 5, (255, 255, 255), -1)
			else:
				if(M["m00"]==0)
				cX = int(M["m10"] / M["m00"])
				cY = int(M["m01"] / M["m00"])
				cv2.circle(fgmask, (cX, cY), 5, (255, 255, 255), -1)
            '''
            p1, st, err = cv2.calcOpticalFlowPyrLK(old_frame, new_frame, p0,
                                                   None, **lk_params)
            good_new = p1[st == 1]
            good_old = p0[st == 1]

            for j, (new, old) in enumerate(zip(good_new, good_old)):
                a, b = new.ravel()
                c, d = old.ravel()
                cv2.line(fgmask, (a, b), (c, d), color[j].tolist(), 2)
                cv2.circle(fgmask, (a, b), 2, color[j].tolist(), -1)

            #print(angle.shape)
            cv2.imshow('frame', fgmask)
            #	    videoout.write(fgmask)
            k = cv2.waitKey(30) & 0xff
            if k == 27:
                break
        end_time = time.time()
        tot_time = end_time - start_time
        hog_avg_mag = sum_mag / n_frames

        hog_mag = hog_avg_mag.flatten()
        #print(hog_mag.shape)

        #print(hog_avg_mag)
        hog_avg_angle = sum_angle / n_frames

        hog_angle = hog_avg_angle.flatten()
        #print(hog_angle.shape)

        res = hog_mag + hog_angle

        old_sum_X = 0
        old_sum_Y = 0
        new_sum_X = 0
        new_sum_Y = 0
        #print(good_new.shape)
        #print(good_old.shape)
        len1 = len(good_new)
        for j, (new, old) in enumerate(zip(good_new, good_old)):
            a, b = new.ravel()
            c, d = old.ravel()
            old_sum_X = old_sum_X + c
            old_sum_Y = old_sum_Y + d
            new_sum_X = new_sum_X + a
            new_sum_Y = new_sum_Y + b

        old_sum_X = old_sum_X / len1
        old_sum_Y = old_sum_Y / len1
        new_sum_X = new_sum_X / len1
        new_sum_Y = new_sum_Y / len1

        disp_opt = math.sqrt(
            ((old_sum_X - new_sum_X)**2 + (old_sum_Y - new_sum_Y)**2))

        #disp=math.sqrt(((init_cX-cX)**2+(init_cY-cY)**2))

        #velocity=disp/tot_time
        velocity_opt = disp_opt / tot_time

        #print(disp)
        #print(disp_opt)
        #print(velocity)
        #print(velocity_opt)

        res = list(res)
        res.append(disp_opt)
        res.append(velocity_opt)

        # lbp_res=np.array(lbp1)

        lbp_res = lbp_res.flatten()
        lbp_res = list(lbp_res)
        # print(lbp_res)
        # print(lbp_res.shape)
        res = [*res, *lbp_res]
        # res=list(res)
        res = np.array(res)
        featuresize = len(res)
        print(featuresize)

        final_res[file_no] = res
        cap.release()
        # videoout.release()
        cv2.destroyAllWindows()
    except LookupError:
        print("Error")
コード例 #41
0
import cv2
import os
import numpy as np

path = '/home/eshwarmannuru/Desktop/NumberPlateDetection/result/1/1.png'
path = sys.argv[1]
im = cv2.imread(path)

#print(im.shape)

im = np.float32(im) / 255.0

gx = cv2.Sobel(im, cv2.CV_32F, 1, 0, ksize = 1)
gy = cv2.Sobel(im, cv2.CV_32F, 0, 1, ksize = 1)

mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)
cv2.imshow("Mag",mag)

hog = cv2.HOGDescriptor()

img = hog(im)


"""
cv2.imshow("Image",im)

from pytesseract import image_to_string
from PIL import Image

print(image_to_string(Image.open(path)))
コード例 #42
0
while (ret):
    index += 1
    flow = cv2.calcOpticalFlowFarneback(
        prvs,
        next,
        None,
        pyr_scale=0.5,  # Taux de réduction pyramidal
        levels=3,  # Nombre de niveaux de la pyramide
        winsize=
        15,  # Taille de fenêtre de lissage (moyenne) des coefficients polynomiaux
        iterations=3,  # Nb d'itérations par niveau
        poly_n=7,  # Taille voisinage pour approximation polynomiale
        poly_sigma=1.5,  # E-T Gaussienne pour calcul dérivées 
        flags=0)
    mag, ang = cv2.cartToPolar(flow[:, :, 0],
                               flow[:, :,
                                    1])  # Conversion cartésien vers polaire
    hsv[:, :, 0] = (ang * 180) / (
        2 * np.pi)  # Teinte (codée sur [0..179] dans OpenCV) <--> Argument
    hsv[:, :, 2] = (mag * 255) / np.amax(mag)  # Valeur <--> Norme

    bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    result = np.vstack((frame2, bgr))
    cv2.imshow('Image et Champ de vitesses (Farnebäck)', result)
    k = cv2.waitKey(15) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        cv2.imwrite('Frame_%04d.png' % index, frame2)
        cv2.imwrite('OF_hsv_%04d.png' % index, bgr)
    prvs = next
コード例 #43
0
print("remapping...")
newf = cv2.remap(ext,
                 mx,
                 my,
                 cv2.INTER_CUBIC,
                 cv2.BORDER_CONSTANT,
                 borderValue=255)

name = sys.argv[2]
print("writing result file ", name, "...")
cv2.imwrite(name, newf)

if len(sys.argv) > 4:
    print("beautifying...")
    mag, ang = cv2.cartToPolar(cx, cy)
    hsv = np.zeros((ext.shape[0], ext.shape[1], 3), dtype=np.uint8)
    ### for color: fill ch.1 white
    hsv[..., 1].fill(255)
    # hsv[...,0] = ang * 180 / np.pi / 2
    ## careful: normalised in each transformation on its own!
    hsv[..., 0] = cv2.normalize(ang * 180 / np.pi / 2, None, 0, 255,
                                cv2.NORM_MINMAX)
    hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

    name = sys.argv[4]
    print("writing local distortion visualization %s..." % name)
    cv2.imwrite(name, bgr)

print("global transform...")
コード例 #44
0
    def dense(self,
              filename='',
              pyr_scale=0.5,
              levels=3,
              winsize=15,
              iterations=3,
              poly_n=5,
              poly_sigma=1.2,
              flags=0,
              skip_empty=False):
        """
        Renders a dense optical flow video of the input video file using `cv2.calcOpticalFlowFarneback()`.
        For more details about the parameters consult the cv2 documentation.

        Parameters
        ----------
        - filename : str, optional

            Path to the input video file. If not specified the video file pointed to by the MgObject is used.
        - pyr_scale : float, optional

            Default is 0.5.
        - levels : int, optional

            Default is 3.
        - winsize : int, optional

            Default is 15.
        - iterations : int, optional

            Default is 3.
        - poly_n : int, optional

            Default is 5.
        - poly_sigma : float, optional

            Default is 1.2.
        - flags : int, optional

            Default is 0.
        - skip_empty : bool, optional

            Default is `False`. If `True`, repeats previous frame in the output when encounters an empty frame.

        Outputs
        -------
        - `filename`_flow_dense.avi

        Returns
        -------
        - MgObject

            A new MgObject pointing to the output '_flow_dense' video file.
        """

        if filename == '':
            filename = self.filename

        of = os.path.splitext(filename)[0]
        fex = os.path.splitext(filename)[1]
        vidcap = cv2.VideoCapture(filename)
        ret, frame = vidcap.read()
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')

        fps = int(vidcap.get(cv2.CAP_PROP_FPS))
        width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))

        out = cv2.VideoWriter(of + '_flow_dense' + fex, fourcc, fps,
                              (width, height))

        ret, frame1 = vidcap.read()
        prev_frame = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
        hsv = np.zeros_like(frame1)
        hsv[..., 1] = 255

        ii = 0

        while (vidcap.isOpened()):
            ret, frame2 = vidcap.read()
            if ret == True:
                next_frame = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

                flow = cv2.calcOpticalFlowFarneback(prev_frame, next_frame,
                                                    None, pyr_scale, levels,
                                                    winsize, iterations,
                                                    poly_n, poly_sigma, flags)

                mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
                hsv[..., 0] = ang * 180 / np.pi / 2
                hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
                rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

                if skip_empty:
                    if np.sum(rgb) > 0:
                        out.write(rgb.astype(np.uint8))
                    else:
                        out.write(prev_rgb.astype(np.uint8))
                else:
                    out.write(rgb.astype(np.uint8))

                prev_frame = next_frame
                prev_rgb = rgb

            else:
                mg_progressbar(length, length,
                               'Rendering dense optical flow video:',
                               'Complete')
                break

            ii += 1

            mg_progressbar(ii, length + 1,
                           'Rendering dense optical flow video:', 'Complete')

        out.release()
        source_audio = extract_wav(of + fex)
        destination_video = of + '_flow_dense' + fex
        embed_audio_in_video(source_audio, destination_video)
        os.remove(source_audio)

        return mgmodule.MgObject(destination_video,
                                 color=self.color,
                                 returned_by_process=True)
コード例 #45
0
                current_path = os.path.join(image_dir, k, current)

                prev_image = cv.imread(prev_path)
                current_image = cv.imread(current_path)

                prev_gray = cv.cvtColor(prev_image, cv.COLOR_BGR2GRAY)
                current_gray = cv.cvtColor(current_image, cv.COLOR_BGR2GRAY)

                mask = np.zeros_like(current_image)

                hsv_current = cv.cvtColor(current_image, cv.COLOR_RGB2HSV)
                mask[:,:,1] = hsv_current[:,:,1]

                flow = cv.calcOpticalFlowFarneback(prev_gray, current_gray, None, 0.5, 1, 15, 2, 5, 1.3, 0)

                magnitude, angle = cv.cartToPolar(flow[..., 0], flow[..., 1])

                mask[..., 0] = angle * (180 / np.pi / 2)

                mask[..., 2] = cv.normalize(magnitude, None, 0, 255, cv.NORM_MINMAX)

                rgb = cv.cvtColor(mask, cv.COLOR_HSV2BGR)

                cv.imwrite(os.path.join(parent_folder, f'{k}-{j}.png'), rgb)

                of_map.setdefault(k, []).append((f'{k}-{j}.png', speed))

                num_processed += 1

    pkl.dump(of_map, open(os.path.join(of_dir, f'optical_flow_map_{mode}.pkl'), 'wb'))
    pkl.dump(new_files, open(os.path.join(of_dir, f'new_files_{mode}.pkl'), 'wb'))
コード例 #46
0
def compute(offset, end):
    CONFIG["output_compressed_file"] = "optical_flow{}-{}.tar.gz".format(
        offset, end)

    video = Video(CONFIG["input_video_file"])
    compressed_file = None
    tmp_dir = None

    if CONFIG["create_compressed_file"]:
        tmp_dir = tempfile.TemporaryDirectory()
        compressed_file = tarfile.open(CONFIG["output_compressed_file"],
                                       "w:gz")

    for current_frame_n in range(offset, end):
        try:
            crnt = video[current_frame_n]
            nxt = video[current_frame_n + 1]
        except StopIteration:
            break
        print("Meow")
        hsv = np.zeros(crnt.shape, dtype=np.uint8)

        crnt = cv2.cvtColor(crnt, cv2.COLOR_BGR2GRAY)
        nxt = cv2.cvtColor(nxt, cv2.COLOR_BGR2GRAY)

        flow = cv2.calcOpticalFlowFarneback(
            crnt,
            nxt,
            flow=None,
            pyr_scale=0.2,
            levels=5,
            winsize=25,
            iterations=2,
            poly_n=10,
            poly_sigma=1.2,
            flags=cv2.OPTFLOW_FARNEBACK_GAUSSIAN)

        hsv[:, :, 0] = 255
        hsv[:, :, 1] = 255
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        hsv[..., 0] = ang * 180 / np.pi / 2
        hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
        rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

        cv2.imwrite("meow.jpg", rgb)
        cv2.imshow("meow", rgb)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        if CONFIG["display_optical_flow"]:
            cv2.imshow("Optical Flow", opt)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

        if CONFIG["create_compressed_file"]:
            cv2.imwrite(
                "{}/optical_flow{}-{}.jpg".format(tmp_dir.name,
                                                  current_frame_n,
                                                  current_frame_n + 1), opt)

        print("Counter #", current_frame_n)

    os.chdir(tmp_dir.name)

    if CONFIG["create_compressed_file"]:
        for _files in glob.glob("*".format(tmp_dir.name)):
            compressed_file.add(_files, recursive=False)
        compressed_file.close()
        tmp_dir.cleanup()
コード例 #47
0
    def runOptFlow(self, frame0, frame1, participants):

        # https://docs.opencv.org/4.0.1/dc/d6b/group__video__track.html#ga5d10ebbd59fe09c5f650289ec0ece5af
        # (..., ..., ...,                                  pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags	)
        # pyr_scale = .5 means each next layer is .5 the size of the previous.
        # levels, number of layers
        # winsize, larger is smoother and faster but lower res
        # iterations, ...
        # poly_n, typically 5 or 7
        # poly_sigma, 1.1 or 1.5
        # flags, extra options
        flow = cv2.calcOpticalFlowFarneback(self.frame0, self.frame1, None, .5,
                                            0, self.of_fb_winsize, 1, 5, 1.1,
                                            0)

        # average angle
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])

        hsv = np.zeros_like(self.frame00)
        hsv[..., 0] = ang * 180 / np.pi / 2
        hsv[..., 2] = mag

        # Find the mean vector.
        flow[mag < self.mag_threshold, 0] = 0
        flow[mag < self.mag_threshold, 1] = 0

        # Work out the x/y and mag/angle components of each participant
        for item in participants:
            # Split the data on x and then y. Doing this in one command crashes the code for some obscure reason
            fl2 = flow[:, item.xRange, :]
            fl2 = fl2[item.yRange, :, :]
            item.X = np.nanmean(fl2[:, :, 0])
            item.Y = np.nanmean(fl2[:, :, 1])
            item.XYmag = (np.sqrt(item.X**2 + item.Y**2))
            item.XYang = (np.arctan2(item.Y, item.X))
            if item.XYang < 0:
                item.XYang = np.mod(item.XYang, np.pi) + np.pi

        if len(participants) >= 2:
            # Get the relative angle between the first two participents
            relAng = np.mod(
                np.subtract(participants[0].XYang, participants[1].XYang),
                2 * np.pi)
            xrel, yrel = cv2.polarToCart(1, relAng)
        else:
            xrel, yrel = 0, 0

        # # Experiment with the scaling and thresholding to map motion b/w 0 and 255.
        mag[mag < self.mag_threshold] = 0
        mag = mag * 10
        if np.max(np.abs(mag)) > 255:
            print(np.max(np.abs(mag)))

        hsv[..., 2] = mag
        # I don't remember any more why I commented this out. Oh yeah. You want to be able to tell how much movement is detected, and how fast, from the video.
        # hsv[...,2] = cv2.normalize(mag,None,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX)
        bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

        cv2.putText(bgr,
                    datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
                    (10, bgr.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
                    (0, 0, 255), 1)

        cv2.circle(bgr,
                   self.center,
                   self.feedback_circle_r, (25, 25, 25, 1),
                   thickness=1)

        cv2.setMouseCallback("Camera", self.boxSelect)
        camImg = self.frame01.copy()
        for i, item in enumerate(participants):
            cv2.rectangle(camImg, (item.xRange[0], item.yRange[0]),
                          (item.xRange[-1], item.yRange[-1]),
                          item.color,
                          thickness=2)

        # Either display individual velocity vectors or the relative phase.
        if self.REL_PHASE_FEEDBACK == 1:
            cv2.line(bgr,
                     self.center,
                     (int(self.center[0] + xrel[0] * self.feedback_circle_r),
                      int(self.center[1] + yrel[0] * self.feedback_circle_r)),
                     (200, 200, 250, 1),
                     thickness=2)
        else:
            for item in self.participents:
                cv2.line(
                    bgr,
                    self.center,
                    (int(self.center[0] + item.X * self.feedback_circle_r),
                     int(self.center[1] + item.Y * self.feedback_circle_r)),
                    item.color,
                    thickness=2)

            cv2.imshow("Camera", camImg)
        if self.ANY_FEEDBACK:
            cv2.imshow('Dense optic flow', bgr)

        self.TIME.append(time.time() - self.TIME[0])
コード例 #48
0
def main():
    stepCounter = 0
    synchron = False

    robot = EPuckVRep('ePuck', port=19999, synchronous=synchron)

    if synchron:
        robot.startsim()

    robot.enableAllSensors()
    robot.enableCamera()
    robot.setImageCycle(1)
    robot.setSensesAllTogether(
        True
    )  # we want fast sensing, so set robot to sensing mode where all sensors are sensed

    # get initial image from robot
    resolX, resolY = 64, 64
    # get initial image
    open_cv_image = getOpenCVCameraImage(robot.getCameraImage(), resolX,
                                         resolY)
    # Convert the new image to gray
    prvsGray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)

    magNew = angNew = mag = ang = 0
    noChangeCount = 0
    first = True

    # main sense-act cycle
    while robot.isConnected():

        # Get sensor values and camera image
        robot.fastSensingOverSignal()
        proxSensors = robot.getProximitySensorValues()
        open_cv_image = getOpenCVCameraImage(robot.getCameraImage(), resolX,
                                             resolY)
        noDetectionDistance = 0.05 * robot.getS()
        # Convert the new image to gray
        nextGray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)
        #cv2.imshow('Gray', nextGray)

        if prvsGray is not None and next is not None:
            flow = cv2.calcOpticalFlowFarneback(prvsGray, nextGray, None, 0.5,
                                                3, 15, 3, 5, 1.2, 0)

            # get angle and magnitude
            mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
            ret, mask = cv2.threshold(mag, 2, 1, cv2.THRESH_BINARY)
            cv2.multiply(mag, mask, mag)
            cv2.multiply(ang, mask, ang)
            mag = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
            ang = ang * 180 / np.pi

            # Check if flow is detected
            if np.any(mag[:, :] > 0) or np.any(
                    ang[:, :] > 0) or noChangeCount > 4 or first == True:
                first = False
                noChangeCount = 0
                magNew = mag
                angNew = ang
                prvsGray = nextGray
            else:
                noChangeCount += 1

            # show image
            hsv = np.zeros_like(open_cv_image)
            hsv[..., 1] = 255
            hsv[..., 0] = angNew * 180 / np.pi / 2
            hsv[..., 2] = magNew
            rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
            res = cv2.resize(rgb,
                             None,
                             fx=6,
                             fy=6,
                             interpolation=cv2.INTER_CUBIC)
            cv2.imshow('flow', res)

        leftMotor, rightMotor = calculateMotorValues(magNew, angNew,
                                                     proxSensors)

        # to abort controller
        k = cv2.waitKey(1) & 0xff
        if k == 27:
            break

        robot.setMotorSpeeds(leftMotor, rightMotor)

        if synchron:
            robot.stepsim(1)

    robot.disconnect()
コード例 #49
0
## 좌우 대칭 거울 좌표 연산
map_mirrorh_x[:, cols // 2:] = cols - map_mirrorh_x[:, cols // 2:] - 1
## 상하 대칭 거울 좌표 연산
map_mirrorv_y[rows // 2:, :] = rows - map_mirrorv_y[rows // 2:, :] - 1

# 물결 효과
map_wave_x, map_wave_y = map_x.copy(), map_y.copy()
map_wave_x = map_wave_x + 15 * np.sin(map_y / 20)
map_wave_y = map_wave_y + 15 * np.sin(map_x / 20)

# 렌즈 효과
## 렌즈 효과, 중심점 이동
map_lenz_x = (2 * map_x - cols) / cols
map_lenz_y = (2 * map_y - rows) / rows
## 렌즈 효과, 극좌표 변환
r, theta = cv2.cartToPolar(map_lenz_x, map_lenz_y)
r_convex = r.copy()
r_concave = r
## 볼록 렌즈 효과 매핑 좌표 연산
r_convex[r < 1] = r_convex[r < 1]**2
print(r.shape, r_convex[r < 1].shape)
## 오목 렌즈 효과 매핑 좌표 연산
r_concave[r < 1] = r_concave[r < 1]**0.5
## 렌즈 효과, 직교 좌표 복원
map_convex_x, map_convex_y = cv2.polarToCart(r_convex, theta)
map_concave_x, map_concave_y = cv2.polarToCart(r_concave, theta)
## 렌즈 효과, 좌상단 좌표 복원
map_convex_x = ((map_convex_x + 1) * cols) / 2
map_convex_y = ((map_convex_y + 1) * rows) / 2
map_concave_x = ((map_concave_x + 1) * cols) / 2
map_concave_y = ((map_concave_y + 1) * rows) / 2
コード例 #50
0
def Canny_detector(img, weak_th = None, strong_th = None): 
	
	# conversion of image to grayscale 
	img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
	
	# Noise reduction step 
	img = cv2.GaussianBlur(img, (5, 5), 1.4) 
	
	# Calculating the gradients 
	gx = cv2.Sobel(np.float32(img), cv2.CV_64F, 1, 0, 3) 
	gy = cv2.Sobel(np.float32(img), cv2.CV_64F, 0, 1, 3) 
	
	# Conversion of Cartesian coordinates to polar 
	mag, ang = cv2.cartToPolar(gx, gy, angleInDegrees = True) 
	
	# setting the minimum and maximum thresholds 
	# for double thresholding 
	mag_max = np.max(mag) 
	if not weak_th:weak_th = mag_max * 0.1
	if not strong_th:strong_th = mag_max * 0.5
	
	# getting the dimensions of the input image 
	height, width = img.shape 
	
	# Looping through every pixel of the grayscale 
	# image 
	for i_x in range(width): 
		for i_y in range(height): 
			
			grad_ang = ang[i_y, i_x] 
			grad_ang = abs(grad_ang-180) if abs(grad_ang)>180 else abs(grad_ang) 
			
			# selecting the neighbours of the target pixel 
			# according to the gradient direction 
			# In the x axis direction 
			if grad_ang<= 22.5: 
				neighb_1_x, neighb_1_y = i_x-1, i_y 
				neighb_2_x, neighb_2_y = i_x + 1, i_y 
			
			# top right (diagnol-1) direction 
			elif grad_ang>22.5 and grad_ang<=(22.5 + 45): 
				neighb_1_x, neighb_1_y = i_x-1, i_y-1
				neighb_2_x, neighb_2_y = i_x + 1, i_y + 1
			
			# In y-axis direction 
			elif grad_ang>(22.5 + 45) and grad_ang<=(22.5 + 90): 
				neighb_1_x, neighb_1_y = i_x, i_y-1
				neighb_2_x, neighb_2_y = i_x, i_y + 1
			
			# top left (diagnol-2) direction 
			elif grad_ang>(22.5 + 90) and grad_ang<=(22.5 + 135): 
				neighb_1_x, neighb_1_y = i_x-1, i_y + 1
				neighb_2_x, neighb_2_y = i_x + 1, i_y-1
			
			# Now it restarts the cycle 
			elif grad_ang>(22.5 + 135) and grad_ang<=(22.5 + 180): 
				neighb_1_x, neighb_1_y = i_x-1, i_y 
				neighb_2_x, neighb_2_y = i_x + 1, i_y 
			
			# Non-maximum suppression step 
			if width>neighb_1_x>= 0 and height>neighb_1_y>= 0: 
				if mag[i_y, i_x]<mag[neighb_1_y, neighb_1_x]: 
					mag[i_y, i_x]= 0
					continue

			if width>neighb_2_x>= 0 and height>neighb_2_y>= 0: 
				if mag[i_y, i_x]<mag[neighb_2_y, neighb_2_x]: 
					mag[i_y, i_x]= 0

	weak_ids = np.zeros_like(img) 
	strong_ids = np.zeros_like(img)			 
	ids = np.zeros_like(img) 
    
	cv2.imwrite("temp.jpeg", mag)
	# double thresholding step 
	for i_x in range(width): 
		for i_y in range(height): 
			
			grad_mag = mag[i_y, i_x] 
			
			if grad_mag<weak_th: 
				mag[i_y, i_x]= 0
			elif strong_th>grad_mag>= weak_th: 
				ids[i_y, i_x]= 1
			else: 
				ids[i_y, i_x]= 2
	
	
	# finally returning the magnitude of 
	# gradients of edges 
	return mag 
コード例 #51
0
        num_rows = int(85 / 5)  # 17
        a = 0
        b = a + 5 * num_rows

        xx = x_flow.copy()
        xx = reverse_rescale(xx.astype(np.float32), minmax[:, 0], minmax[:, 1])
        xx = xx.reshape(85, 2, 224, 224).swapaxes(1, 2).swapaxes(2, 3)
        # print(x_flow.shape)
        subset = xx[a:b]

        fig = plt.figure(figsize=(15, num_rows * 3))
        for i, flow in enumerate(subset):
            plt.subplot(num_rows, 5, i + 1)

            mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])  # get magnitude and direction/angle
            hsv = np.zeros((flow.shape[0], flow.shape[1], 3), dtype=np.uint8)
            hsv[..., 0] = 255  # sets hue
            hsv[..., 1] = 255  # # Sets image saturation to maximum
            hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)  # sets value/brightness
            rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
            gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
            im = plt.imshow(255-gray, cmap='gray')

            plt.tick_params(
                which='both',  # both major and minor ticks are affected
                bottom=False,  # ticks along the bottom edge are off
                left=False,  # ticks along the top edge are off
                labelbottom=False,  # labels along the bottom edge are off
                labelleft=False
            )
コード例 #52
0
ファイル: pcalda.py プロジェクト: stgitorious/TrafficRec
def HOG(img, angleBins=8):
    #	YUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
    img = cv2.resize(img, (40, 40))
    kernel1 = np.ones((3, 1), np.float32)
    kernel2 = np.ones((1, 3), np.float32)
    kernel1[2] = -1
    kernel1[1] = 0
    kernel2[0] = [-1, 0, 1]
    #	print img
    dsth = cv2.filter2D(img, cv2.CV_16S, kernel1).astype(np.float32)
    dstv = cv2.filter2D(img, cv2.CV_16S, kernel2).astype(np.float32)
    #	print dsth
    Mag, direction = cv2.cartToPolar(dsth, dstv, angleInDegrees=1)
    #	print Mag
    final = np.zeros((img.shape[0], img.shape[1], 2), np.float32)
    for rows in range(len(Mag)):
        for cols in range(len(Mag[rows])):
            final[rows, cols][0] = max(Mag[rows, cols])
            final[rows, cols][1] = direction[rows, cols, Mag[
                rows, cols].tolist().index(max(Mag[rows, cols]))]
#	print final
    Hists = []
    for row in final:
        histCol = []
        for col in row:
            hist = [0] * angleBins
            index = int(col[1] / (360 / angleBins))
            angle = col[1] - index * (360 / angleBins)
            try:
                i = index % angleBins
            except:
                i = 0
            hist[i] = (angle / (360 / angleBins)) * col[0]
            hist[angleBins % (index + 1)] = (1 - (angle /
                                                  (360 / angleBins))) * col[0]
            histCol.append(hist)
        Hists.append(histCol)
    histos = np.array(Hists).astype(np.float32)
    #	print histos
    #	histos = np.swapaxes(hists,0,2)
    #	print hists
    #	print histos
    kernel = np.ones((5, 5), np.float32)
    histos = cv2.filter2D(histos, cv2.CV_32FC1, kernel).astype(np.float32)
    histos = histos[::5, ::5, :]
    final = []
    for x in range(len(histos) - 1):
        row = []
        for y in range(len(histos[0]) - 1):
            lister = []
            lister.extend(histos[x, y])
            lister.extend(histos[x + 1, y])
            lister.extend(histos[x, y + 1])
            lister.extend(histos[x + 1, y + 1])
            row.append(lister)
        final.append(row)


#	print histos
#	final = np.array(final)
#	print final
#	print final.shape
    histos = [L2HysNormalize(x) for row in final for x in row]
    histos = [x for row in histos for x in row]
    #	print histos
    #	print np.array(histos).shape
    return histos
コード例 #53
0
    # extract the image filename (assumed to be unique)
    filename = imagePath[imagePath.rfind("/") + 1:]
    # Read the image as grey image
    image = cv2.imread(imagePath, 0)
    # Use canny edge detector to select edge points
    cannyImageMask = cv2.Canny(image, 100, 250)
    # To create the 8-bit mask
    cannyImageMask = np.uint8(cannyImageMask)
    # Apply bitwise and on the image to mask it
    maskedImage = cv2.bitwise_and(image, image, mask=cannyImageMask)
    # Compute gradients in x and y direction
    sobelXDir = cv2.Sobel(maskedImage, cv2.CV_64F, 1, 0, ksize=5)
    sobelYDir = cv2.Sobel(maskedImage, cv2.CV_64F, 0, 1, ksize=5)
    # Compute magnitude and theta angle using the gradients
    magnitude, theta = cv2.cartToPolar(sobelXDir,
                                       sobelYDir,
                                       angleInDegrees=True)
    # To turn theta into hist index
    theta = np.round(np.divide(theta, 10))
    theta = np.uint8(theta)
    # flatten the theta and magnitude arrays
    flattenedTheta = hist_bins(theta)
    flattenedMagnitude = hist_bins(magnitude)
    # Build 36-bin histograms
    hist, bins = np.histogram(flattenedTheta,
                              range(37),
                              weights=flattenedMagnitude)
    histogram_grey_list[filename] = hist
    plt.plot(hist)
    plt.show()
コード例 #54
0
def main():
    # uncomment only one
    #
    logging.basicConfig(format='%(message)s', level=logging.INFO)
    #logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

    # load camera configuration
    try:
        settings = config.camera
    except AttributeError:
        settings = {}

    # send counters to Unix Domain Socket
    import uds

    # use Raspberry Pi Camera
    #
    camera = PiCamera()
    camera.resolution = (320, 240)
    rawCapture = PiRGBArray(camera, size=(320, 240))
    time.sleep(0.1)

    camera.capture(rawCapture, format="bgr")
    image = rawCapture.array
    prev = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    np.zeros_like(prev)
    rawCapture.truncate(0)

    # initialise model for human faces
    #
    face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_alt.xml')

    # initialise model for standing humans
    #
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

    # compute counters every 2 seconds
    #
    flagone = datetime.datetime.now()
    while True:

        flagtwo = datetime.datetime.now()
        elapsed = (flagtwo - flagone).total_seconds()

        if elapsed < 2:
            time.sleep(0.1)
            continue
        flagone = datetime.datetime.now()

        people_count = 0
        people_moves = 0
        people_faces = 0

        # capture an image
        #
        mask = np.zeros(image.shape[:2], dtype='uint8')
        camera.capture(rawCapture, format='bgr')
        image = rawCapture.array
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        rawCapture.truncate(0)

        # detect human faces
        #
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        people_faces = len(faces)

        # detect human beings
        #
        (rects, _) = hog.detectMultiScale(image,
                                          winStride=(4, 4),
                                          padding=(8, 8),
                                          scale=1.07)
        rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
        pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
        people_count = len(pick)

        for(xA, yA, xB, yB) in pick:
            cv2.rectangle(mask, (xA, yA), (xB, yB), 255, -1)

        # count moving people
        #
        nextim = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        nextim = cv2.bitwise_and(nextim, nextim, mask=mask)
        flow = cv2.calcOpticalFlowFarneback(prev, nextim, None, 0.5, 1, 3, 15, 3, 5, 1)
        mag, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        mag = mag * 0.8
        prev = nextim

        for(xA, yA, xB, yB) in pick:
            try:
                m = np.median(mag[yA:yB, xA:xB])
    #            print m
                if m > 1.6:
                    people_moves += 1
            except RuntimeWarning:
                pass

        # push counters via Unix Domain Socket
        #
        message = '%s %d %d %d' % (settings.get('id', 'myCamera'),
                                   people_count,
                                   people_moves,
                                   people_faces)
        uds.push(message)
コード例 #55
0
    starttime = time.time()

    dualtvl1_opticalflow = cv2.optflow.DualTVL1OpticalFlow_create()
    #

    #    @jit
    #     flowDTVL1=dualtvl1_opticalflow.calc(prvs,nextframe,None)
    flowDTVL1 = dualtvl1_opticalflow.calc(prvs, nextframe, None)
    #     help(dualtvl1_opticalflow.calc)
    #     print(prvs.dtype,nextframe.dtype)
    #     flowDTVL1=calc_optical(prvs,nextframe,None)
    print(flowDTVL1.dtype)
    # flowDTVL1=cv2.calcOpticalFlowFarneback(prvs,nextframe,None,0.5,3,15,3,5,1.2,0)endtime=time.time()
    endtime = time.time()
    #print(flowDTVL1.shape)
    mag, ang = cv2.cartToPolar(flowDTVL1[..., 0], flowDTVL1[..., 1])
    hsv[..., 0] = ang * 180 / np.pi / 2
    hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    # endtime=time.time()
    fps = 1 / (endtime - starttime)  #计算帧率
    print("fps is", fps)
    rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    cv2.imshow('opticalflow image', rgb)

    cv2.imshow('original image', frame1)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        cv2.imwrite('opt', frame1)
        cv2.imwrite('rgb', rgb)
コード例 #56
0
import cv2 as cv
import numpy as np
cap = cv.VideoCapture("videos/test.mp4")
ret, frame1 = cap.read()
prvs = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
while (1):
    ret, frame2 = cap.read()
    next = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)
    flow = cv.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2,
                                       0)
    mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])
    hsv[..., 0] = ang * 180 / np.pi / 2
    hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
    bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
    cv.imshow('frame2', bgr)
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        cv.imwrite('opticalfb.png', frame2)
        cv.imwrite('opticalhsv.png', bgr)
    prvs = next
cap.release()
cv.destroyAllWindows()
コード例 #57
0
def preprocess_video(data_id):
    print('preprocessing data: {0} ...'.format(data_id))
    label_path = os.path.join(label_dir, data_id + '.txt')
    video_path = os.path.join(video_dir, data_id + '.avi')

    video_capture = cv2.VideoCapture(video_path)
    _, frame = video_capture.read()
    frame_id = 1

    num_actions, action_classes, start_frames, end_frames = parse_label(
        label_path)

    for i in range(num_actions):
        cur_action_class = action_classes[i]
        print('  No.{:02} action, belongs to class {:02}'.format(
            i + 1, cur_action_class))

        cur_rgb_dir = os.path.join(rgb_frame_dir,
                                   '{:02}'.format(cur_action_class))
        cur_optical_flow_dir = os.path.join(optical_flow_dir,
                                            '{:02}'.format(cur_action_class))
        if not os.path.exists(cur_rgb_dir):
            os.mkdir(cur_rgb_dir)
        if not os.path.exists(cur_optical_flow_dir):
            os.mkdir(cur_optical_flow_dir)

        start_frame, end_frame = start_frames[i], end_frames[i]
        frame_interval = round((end_frame - start_frame) / NUM_FRAME_SAMPLE)
        frame_interval = max(frame_interval, 1)
        frame_count = 0

        # optical flow frame
        prvs = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        hsv = np.zeros_like(frame)
        hsv[..., 1] = 255

        while frame_id < start_frame:
            _, frame = video_capture.read()
            frame_id += 1

        while frame_id < end_frame:
            if frame_id % frame_interval == 0:
                frame_count += 1
                # rgb frame
                frame_filename = data_id + '-{:02}.jpg'.format(frame_count)
                frame_file_path = os.path.join(cur_rgb_dir, frame_filename)
                cv2.imwrite(frame_file_path, frame)
                # optical flow frame
                # reference: https://docs.opencv.org/3.3.1/d7/d8b/tutorial_py_lucas_kanade.html
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                flow = cv2.calcOpticalFlowFarneback(prvs, gray, None, 0.5, 3,
                                                    15, 3, 5, 1.2, 0)
                mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
                hsv[..., 0] = ang * 180 / np.pi / 2
                hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
                optical_flow_frame = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
                optical_flow_filename = data_id + '-flow-{:02}.jpg'.format(
                    frame_count)
                optical_flow_file_path = os.path.join(cur_optical_flow_dir,
                                                      optical_flow_filename)
                cv2.imwrite(optical_flow_file_path, optical_flow_frame)
            prvs = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            frame_id += 1
            _, frame = video_capture.read()
コード例 #58
0
ファイル: behavior.py プロジェクト: Rack95/Caiman_edit
def to_polar(x, y):
    mag, ang = cv2.cartToPolar(x, y)
    return mag, ang
コード例 #59
0
ファイル: behavior.py プロジェクト: Rack95/Caiman_edit
def compute_optical_flow(m,
                         mask=None,
                         polar_coord=True,
                         do_show=False,
                         do_write=False,
                         file_name=None,
                         gain_of=None,
                         frate=30,
                         pyr_scale=.1,
                         levels=3,
                         winsize=25,
                         iterations=3,
                         poly_n=7,
                         poly_sigma=1.5):
    """
    This function compute the optical flow of behavioral movies using the opencv cv2.calcOpticalFlowFarneback function

    Parameters:
    ----------

    m: 3D ndarray:
        input movie

    mask: 2D ndarray
        mask selecting relevant pixels

    polar_coord: boolean
        wheather to return the coordinate in polar coordinates (or cartesian)

    do_show: bool
        show flow movie

    do_write: bool
        save flow movie

    frate: double
        frame rate saved movie

    parameters_opencv_function: cv2.calcOpticalFlowFarneback
        pyr_scale,levels,winsize,iterations,poly_n,poly_sigma



    Returns:
    --------
    mov_tot: 4D ndarray containing the movies of the two coordinates

    Raise:
    -----
    Exception('You need to provide file name (.avi) when saving video')

    """
    prvs = np.uint8(m[0])
    frame1 = cv2.cvtColor(prvs, cv2.COLOR_GRAY2RGB)

    hsv = np.zeros_like(frame1)
    hsv[..., 1] = 255

    if do_show:
        cv2.namedWindow("frame2", cv2.WINDOW_NORMAL)

    if mask is not None:
        data = mask.astype(np.int32)
    else:
        data = 1

    T, d1, d2 = m.shape
    mov_tot = np.zeros([2, T, d1, d2])

    if do_write:
        if file_name is not None:
            video = cv2.VideoWriter(file_name,
                                    cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                                    frate, (d2 * 2, d1), 1)
        else:
            raise Exception(
                'You need to provide file name (.avi) when saving video')

    for counter, next_ in enumerate(m):
        if counter % 100 == 0:
            print(counter)
        frame2 = cv2.cvtColor(np.uint8(next_), cv2.COLOR_GRAY2RGB)
        flow = cv2.calcOpticalFlowFarneback(prvs, next_, None, pyr_scale,
                                            levels, winsize, iterations,
                                            poly_n, poly_sigma, 0)

        if polar_coord:
            coord_1, coord_2 = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        else:
            coord_1, coord_2 = flow[:, :, 0], flow[:, :, 1]

        coord_1 *= data
        coord_2 *= data

        if do_show or do_write:
            if polar_coord:
                hsv[..., 0] = coord_2 * 180 / np.pi / 2
            else:
                hsv[..., 0] = cv2.normalize(coord_2, None, 0, 255,
                                            cv2.NORM_MINMAX)
            if gain_of is None:
                hsv[..., 2] = cv2.normalize(coord_1, None, 0, 255,
                                            cv2.NORM_MINMAX)
            else:
                hsv[..., 2] = gain_of * coord_1
            rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
            frame_tot = np.concatenate([rgb, frame2], axis=1)

        if do_write:
            video.write(frame_tot)

        if do_show:
            cv2.imshow('frame2', frame_tot)
            k = cv2.waitKey(30) & 0xff
            if k == 27:
                break

        mov_tot[0, counter] = coord_1
        mov_tot[1, counter] = coord_2

        prvs = next_.copy()

    if do_write:
        video.release()

    if do_show:
        cv2.destroyAllWindows()

    return mov_tot
コード例 #60
0
def opt_flow(cap, show_video, filename):
    # buffer size for keeping RAM smooth
    maxlen = 1000
    mag_deque = deque(maxlen=maxlen)
    timestamp_deque = deque(maxlen=maxlen)
    xy_deque = deque(maxlen=maxlen)

    # grab the current frame
    frame1 = cap.read()
    # images will come as
    # (h, w, channels)
    original_width = frame1.shape[1]
    # resize_width for ease of computation
    resize_width = 320

    # reduce size
    frame1 = imutils.resize(frame1, width=resize_width)

    # ret, frame1 = cap.read()
    # Grayscale
    prev = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

    hsv = np.zeros_like(frame1)
    # We fix saturation into the maximal possible value
    hsv[..., 1] = 255

    # create_iteration number
    iter_number = 0

    while (True):
        # get timestamp
        timestamp = datetime.datetime.now().isoformat(" ")
        timestamp_deque.append(timestamp)
        frame2 = cap.read()
        # if we are viewing a video and we did not grab a frame,
        # then we have reached the end of the video
        if frame2 is None:
            print("End of video. Getting out")
            break

        # reduce the frame so that we speed up computation
        frame2 = imutils.resize(frame2, width=resize_width)

        gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (5, 5), 0)

        # see docs here: https://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html
        flow = cv2.calcOpticalFlowFarneback(
            prev,
            gray,
            None,
            pyr_scale=0.5,
            levels=3,
            winsize=
            20,  # averaging window size; larger values increase the algorithm robustness to image noise and give more chances for fast motion detection, but yield more blurred motion field.
            iterations=3,
            poly_n=5,  # typically poly_n =5 or 7.
            poly_sigma=1.1,  # 1.1 for poly_n = 5 and 1.5 for poly_n = 7
            flags=0)

        # For OpenCV’s implementation, it computes the magnitude and direction
        # of optical flow from a 2-channel array of flow vectors (dx/dt,dy/dt),
        # the optical flow problem.
        # It then visualizes the angle (direction) of flow by hue and the distance (magnitude)
        # of flow by value of HSV color representation.
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])

        # Increase signal to noise ratio ?
        # We can always do this afterwards but visual normalization might need it
        #mag = mag * mag
        # np.median is probably less noisy but it misses a lot of movement
        sum_mag = np.sum(mag)
        mag_deque.append(sum_mag)

        # if there's enough movement
        if sum_mag > 1000:
            # if mag is 1 for any pixel looks like real movement
            # try to calculate mask and centroid
            # calculate the mask as cv2 likes it
            mask = np.array((mag > 1) * 255).astype('uint8')
            xy = get_centroid(mask)
            # xy will be a tupple
            # we need to adjust with resize factor
            # the way to keep it tupple is with tuple and list comprehension
            if xy is not None:
                xy = tuple([
                    int(value * original_width / resize_width) for value in xy
                ])
            xy_deque.append(xy)
        else:
            xy_deque.append(None)
            mask = None

        if (show_video):
            # Direction/angle goes into hue (first channel)
            hsv[..., 0] = ang * 180 / np.pi / 2
            # magnitude goes into value (third channel)
            # We normalize to be able to see...
            # Because this distorts values, all data analysis will use the mag object
            hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
            bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

            #bgr_blur = cv2.medianBlur(bgr, 5)
            #bgr_blur[bgr_blur < 50]  = 0
            # put text for the total movement
            cv2.putText(bgr, "total mov: " + str(round(sum_mag, 2)), (10, 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1)
            cv2.imshow('frame2', bgr)
            cv2.imshow('original', gray)
            #cv2.imshow('blurr', bgr_blur)
            if mask is not None:
                show_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
                if xy is not None:
                    # we need to correct back for the resizing to show
                    xy_show = tuple([
                        int(value * resize_width / original_width)
                        for value in xy
                    ])
                    cv2.circle(
                        show_mask,
                        xy_show,
                        5,  # a very small circle
                        (0, 0, 255),  # red
                        -1
                    )  # Negative thickness means that a filled circle is to be drawn.
                cv2.imshow('mask', show_mask)

            k = cv2.waitKey(1) & 0xff
            if k == 27:
                break
            # possiblilty to save via command
            #elif k == ord('s'):
            #    with open('mag_deque.csv','a') as outfile:
            #        np.savetxt(outfile, mag_deque,
            #        delimiter=',', fmt='%s')
        else:
            # if we don't give any feedback it's difficult to know
            print('opt_flow.py Iteration: {} Total movement: {}'.format(
                iter_number, sum_mag),
                  end="\r")

        # Clean-up
        # assign the last frame to previous
        prev = gray
        unsaved_elements = iter_number % maxlen
        if (unsaved_elements == 0):
            save_deque(iter_number, timestamp_deque, mag_deque, xy_deque,
                       maxlen, filename)
        iter_number = iter_number + 1

    exit_handler(iter_number, timestamp_deque, mag_deque, xy_deque, maxlen,
                 filename)
    return mag_deque