Пример #1
0
    def __init__(self, size):
        # Get frame size
        x1, y1 = 0, 0
        x2, y2 = size

        # Resize to fft window
        w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
        x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2

        # Store position and size relative to frame
        self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
        self.size = w, h

        # Create Hanning window (weighting of values from center)
        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)

        # Create output image (centered Gaussian point--for correlation)
        g = np.zeros((h, w), np.float32)
        g[h//2, w//2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        # Save the desired output image in frequency space
        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)

        # Create sum matrices
        self.F = np.zeros_like(self.G)
        self.HSum = np.zeros_like(self.G)

        self.count = 0
Пример #2
0
    def __init__(self, frame, rect, number):
        self.num = number
        x1, y1, x2, y2 = rect
        w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
        x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2
        self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
        self.size = w, h
        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h//2, w//2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            a = self.preprocess(MOSSE.rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(     A, A, 0, conjB=True)
        self.update_kernel()
        self.update(frame)
Пример #3
0
def phase_corr():
    single = imageio.imread("images/single-device-mask.jpg", as_gray=True)
    grid = imageio.imread("images/raw/01 BF-before.mask.tif")
    re = scipy.signal.fftconvolve(single, grid)

    cv2.imshow('re', np.log(re / np.max(re)))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return

    single = pad_fft(single) / 255.
    grid = pad_fft(grid) / 255.

    height, width = grid.shape
    hanw = cv2.createHanningWindow(grid.shape, cv2.CV_64F)

    # Windowing and FFT
    G_a = np.fft.fft2(single * hanw)
    G_b = np.fft.fft2(grid * hanw)
    conj_b = np.ma.conjugate(G_b)
    R = G_a * conj_b
    R /= np.absolute(R)
    r = np.fft.fftshift(np.fft.ifft2(R).real)
    r = 255. * r / np.max(r)

    cv2.imshow('r', np.log(r))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Пример #4
0
 def preprocess(self, im):
     if self._window is None:
         self._window = cv2.createHanningWindow((im.shape[1], im.shape[0]), cv2.CV_64FC1)
     # grayscale = np.mean(im, 2)
     hanned = im * self._window
     image_fft = PhaseCorrelate.preprocessForPhaseCorrelate(hanned)
     return image_fft
Пример #5
0
def PhaseCorrelation(a, b):
    height, width = a.shape
    #dt = a.dtype # data type
    # Windowing
    hann_ = cv2.createHanningWindow((height, width), cv2.CV_64F)
    #hann = hann_.astype(dt) # convert to correspoinding dtype
    rhann = np.sqrt(hann_)
    rhann = hann_

    # FFT
    G_a = np.fft.fft2(a * rhann)
    G_b = np.fft.fft2(b * rhann)
    conj_b = np.ma.conjugate(G_b)
    R = G_a * conj_b
    R /= np.absolute(R)
    r = np.fft.fftshift(np.fft.ifft2(R).real)
    # Get result and Interpolation
    DY, DX = np.unravel_index(r.argmax(), r.shape)
    # Subpixel Accuracy
    boxsize = 5
    box = r[DY - int((boxsize - 1) / 2):DY + int((boxsize - 1) / 2) + 1,
            DX - int((boxsize - 1) / 2):DX + int((boxsize - 1) / 2) +
            1]  # x times x box
    #TY,TX= CenterOfGravity(box)
    TY, TX = WeightedCOG(box)
    sDY = TY + DY
    sDX = TX + DX
    # Show the result
    # print('DX=',width/2-sDX,'DY=',height/2-sDY)
    # print('CorrelationVal=',r[DY,DX])
    plt.imshow(r, vmin=r.min(), vmax=r.max())
    return [width / 2 - sDX, height / 2 - sDY], r[DY, DX]
    def __init__(self, frame, rect, index, prev_id=0, reInit=False):
        self.stationary_for_frames = 0
        x1, y1, x2, y2 = rect
        self.index = index
        if not reInit:
            self.id_ = MOSSE.MosseCounter
            MOSSE.MosseCounter = MOSSE.MosseCounter + 1
        else:
            self.id_ = prev_id
        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
        self.pos = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        self.size = w, h
        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
        self.update_kernel()
        self.update(frame)
    def __init__(self, frame, rect):
        org = frame.copy()
        if len(frame.shape) == 3:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     
        x1, y1, x2, y2 = rect
        w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
        x1, y1 = (x1+x2-w)//2, (y1+y2 - h)//2
        self.pos = x,y = x1+0.5*(w-1), y1 + 0.5*(h-1)
        self.size = w, h
        img = cv2.getRectSubPix(frame, (w,h), (x,y))

        self.win = cv2.createHanningWindow((w,h), cv2.CV_32F)
        #expected response
        g = np.zeros((h,w), np.float32)
        g[h//2, w//2] = 1
        g = cv2.GaussianBlur(g, (-1,-1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags = cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        
        for i in range(128):
            f = self.preprocess(self.rnd_warp(img), self.win)
            F = cv2.dft(f, flags = cv2.DFT_COMPLEX_OUTPUT)

            self.H1 += cv2.mulSpectrums(self.G, F, 0, conjB = True)
            self.H2 += cv2.mulSpectrums(     F, F, 0, conjB = True)
           
        self.update_kernel()    #update kernel
        self.update(frame)  #
Пример #8
0
    def shift_stabilization(img1, img2, rows, cols, print_result=None):
        """
        Perform shift stabilization on two images using phase correlation with hanning window

    :param img1                     source image
        :param img2:                target image
        :param rows:                rows of result image
        :param cols:                columns of result image
        :param print_result:        gathered information during stabilization
        :return:
            result_image:   stabilized (shifted) image
            print_result:   collected information during shift stabilization
        """
        img1_gray = ImageProcess.to_gray(img1)
        img2_gray = ImageProcess.to_gray(img2)

        hanning = cv2.createHanningWindow((cols, rows), cv2.CV_32F)
        (cx, cy), _ = cv2.phaseCorrelate(np.float32(img2_gray),
                                         np.float32(img1_gray))
        # (cx, cy) = (round(cx, 2), round(cy, 2))
        M = np.float32([[1, 0, cx], [0, 1, cy]])
        print_result['x'] = cx
        print_result['y'] = cy
        t_form = transform.EuclideanTransform(translation=(cx, cy))
        result_image = transform.warp(img2, t_form)
        return img_as_ubyte(result_image), print_result
    def onrect(self, rect):
        #print "onrect enterd:",rect
        self.lk_ready = True
        frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
        frame_gray = cv2.equalizeHist(frame_gray)

        self.old_gray = frame_gray.copy()

        self.patch = self.old_gray[rect[1]:rect[3],rect[0]:rect[2]]
        self.w = rect[2]-rect[0]
        self.h = rect[3]-rect[1]
        #cv2.imshow('patch',self.patch)
        #cv2.waitKey(10)
        mask = cv2.createHanningWindow((self.w,self.h),cv2.CV_32F)
        self.patch = np.uint8(self.patch*mask)

        self.p0 = getUniformPoints(self.old_gray,rect[:2],rect[2:],100)
#        self.p0 = cv2.goodFeaturesToTrack(self.patch, mask = None, **feature_params)
        if len(self.p0)==0:
            self.lk_ready = False
            print "len p0:",len(self.p0)
##        else:
##            for i in xrange(len(self.p0)):
##                self.p0[i][0][0] = self.p0[i][0][0] + rect[0]
##                self.p0[i][0][1] = self.p0[i][0][1] + rect[1]
        tracker = MOSSE(frame_gray, rect)
        self.trackers.append(tracker)
Пример #10
0
    def init(self, frame, rect):
        if frame.ndim == 3:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        rect = rect.astype(int)
        rect[2:] += rect[:2]
        x1, y1, x2, y2 = rect
        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
        self.t_center = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        self.t_sz = w, h
        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.A = np.zeros_like(self.G)
        self.B = np.zeros_like(self.G)
        for _i in range(128):
            patch = self._preprocess(self._random_warp(img))
            F = cv2.dft(patch, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.A += cv2.mulSpectrums(self.G, F, 0, conjB=True)
            self.B += cv2.mulSpectrums(F, F, 0, conjB=True)
        self._update_kernel()
        self.update(frame)
Пример #11
0
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect
        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
        self.pos = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        self.size = w, h
        #self.good = 0
        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
        self.update_kernel()
        self.update(frame)
Пример #12
0
def pre_process(img):
    log_img = np.log(img + 1)
    normalize_img = (log_img - np.mean(log_img.flatten())) / (
        np.std(log_img.flatten() + 1e-5))
    cosine_window = cv2.createHanningWindow(img.shape[:2], cv2.CV_32F)
    output = normalize_img * cosine_window.T
    return output
Пример #13
0
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect # Extract coordinates of the rectangle to be tracked
        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2

        # 0.5 * x \in [w, h] (-1) = the centre point of the region
        self.pos = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        self.size = w, h

        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        # Hanning Window
        # http://en.wikipedia.org/wiki/Window_function
        # http://en.wikipedia.org/wiki/Window_function#Hann_.28Hanning.29_window
        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)

        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, None, cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)

        for i in xrange(128):
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, None, cv2.DFT_COMPLEX_OUTPUT)

            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)

        self.update_kernel()
        self.update(frame)
        self.id = id(self)
def make_image():
    image = np.random.rand(20, 70, 3)
    window = cv2.createHanningWindow((image.shape[1], image.shape[0]), cv2.CV_64FC1)
    grayscale = np.mean(image, 2)
    hanned = grayscale * window
    image_fft = PhaseCorrelate.preprocessForPhaseCorrelate(hanned)
    return image, image_fft
Пример #15
0
    def __reduce_image_noise_and_details(self):
        # apply Gaussian Blur on the original image, to reduce noise and the amount of details, for
        # faster processing
        self.g = np.zeros((self.size[::-1]), np.float32)
        self.g[self.size[1] // 2, self.size[0] // 2] = 1
        self.g = cv2.GaussianBlur(self.g, (-1, -1), 2.0)
        self.g /= self.g.max()

        self.hann_window = cv2.createHanningWindow(self.size, cv2.CV_32F)
Пример #16
0
def getCylHanning(size, axis=0, ptype=cv2.CV_64F):
    tmp = cv2.createHanningWindow(size[::-1], ptype)
    if axis == 0:
        tmp = tmp[size[1] / 2, :]
        ret = np.tile(tmp, (size[0], 1))
    else:
        tmp = tmp[:, size[0] / 2]
        ret = np.transpose(np.tile(tmp, (size[1], 1)))
        # print ret.shape
    return ret
Пример #17
0
def getCylHanning(size, axis=0, ptype=cv2.CV_64F):
    tmp=cv2.createHanningWindow(size[::-1], ptype)
    if axis==0:
        tmp=tmp[size[1]/2,:]
        ret=np.tile(tmp,(size[0],1))
    else:
        tmp=tmp[:, size[0]/2]
        ret=np.transpose(np.tile(tmp,(size[1],1)))
        # print ret.shape
    return ret
Пример #18
0
def pre_process(img):
    # get the size of the img...
    height, width = img.shape
    img = np.log(np.float32(img) + 1.0)
    img = (img - img.mean()) / (img.std() + 1e-5)
    # use the hanning window...
    # window = window_func_2d(height, width)
    window = cv2.createHanningWindow((width, height), cv2.CV_32F)
    img = img * window

    return img
Пример #19
0
    def initialise(self, gray_image, bounding_box):
        '''
        Receives a grayscale frame and a bounding box and operates over it
        return True if OK

        Bounding box format: ((x0, y0),(x1, y1))

        This uses the Numpy FFT since it already represents the numbers in
        complex representation and allows to do Spectrum Multiplciation and
        Vision in a straight-forward fashion
        '''
        self.last_frame = gray_image
        p1, w, h = self.extractBoundingBox(bounding_box)
        p1, p2 = bounding_box
        cols = p2[0] - p1[0]
        rows = p2[1] - p1[1]

        self.size = (w, h)
        x1 = int(round((2 * p1[0] + cols - w) / 2))
        y1 = int(round((2 * p1[1] + rows - h) / 2))
        self.center = (x1 + (w / 2), y1 + (h / 2))

        # Get inputs - window from BBox and the HanningWindow for gaussianity
        window = cv.getRectSubPix(gray_image, self.size, self.center)
        self.hanWin = cv.createHanningWindow(self.size, cv.CV_32F)

        # Create goal and its FFT
        g = np.zeros((h, w), np.float32)
        g[int(h / 2)][int(w / 2)] = 1.
        g = cv.GaussianBlur(g, (-1, -1), 2.0)
        maxVal = cv.minMaxLoc(g)[1]
        maxVal = 1. / maxVal
        g = g * maxVal
        self.G = np.fft.fft2(g)

        # Initialise the filter and other elements
        self.A = np.zeros(self.G.shape, dtype=np.complex128)
        self.B = np.zeros(self.G.shape, dtype=np.complex128)

        # Train the filter with a random warping
        for i in range(8):
            warped = randWarp(window)
            self.f = preprocess(warped, self.hanWin)
            F = np.fft.fft2(self.f)
            A_i = self.G * np.conjugate(F)
            B_i = F * np.conjugate(F)
            self.A += A_i
            self.B += B_i

        mask_b = np.ma.array(self.B, mask=(self.B == 0))
        self.H = self.A / mask_b
        self.H = self.H.filled(0.)

        return True
Пример #20
0
def create_window(reference, plot=False):
    height, width = reference.shape
    hanning_window = cv2.createHanningWindow((width, height), cv2.CV_64F)
    #hanning_window = hanning_window[height//2, :]
    #hanning_window = np.outer(hanning_window, hanning_window)

    if plot:
        plt.contourf(hanning_window)
        plt.show()

    return hanning_window
def phaseCorr(img1, img2, isUseHann=True, isDebug=False):
    assert(img1.shape==img2.shape)
    if isUseHann:
        hw=cv2.createHanningWindow(img1.shape[::-1], cv2.CV_64F)
        img1=img1*hw
        img2=img2*hw
    if isDebug:
        cv2.imshow("debug-img12", np.concatenate((nrmMat(img1), nrmMat(img2))))
    fft1=np.fft.fft2(img1)
    fft2=np.fft.fft2(img2)
    CC=np.fft.fftshift(np.fft.ifft2( (fft1*fft2.conj())/(fft2*fft2.conj()) ).real)
    minVal, maxVal, minLoc, maxLoc=cv2.minMaxLoc(CC)
    dxy=(maxLoc - np.array(img1.shape[::-1])/2)
    return (CC, dxy, maxVal)
Пример #22
0
def phaseCorr(img1, img2, isUseHann=True, isDebug=False):
    assert (img1.shape == img2.shape)
    if isUseHann:
        hw = cv2.createHanningWindow(img1.shape[::-1], cv2.CV_64F)
        img1 = img1 * hw
        img2 = img2 * hw
    if isDebug:
        cv2.imshow("debug-img12", np.concatenate((nrmMat(img1), nrmMat(img2))))
    fft1 = np.fft.fft2(img1)
    fft2 = np.fft.fft2(img2)
    CC = np.fft.fftshift(
        np.fft.ifft2((fft1 * fft2.conj()) / (fft2 * fft2.conj())).real)
    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(CC)
    dxy = (maxLoc - np.array(img1.shape[::-1]) / 2)
    return (CC, dxy, maxVal)
Пример #23
0
def ripoc(srcImg1, srcImg2, logpolarMagnitude=40, plotFig=False):
    grayImg1 = np.float64(cv2.cvtColor(srcImg1, cv2.COLOR_BGRA2GRAY)) / 255.0
    grayImg2 = np.float64(cv2.cvtColor(srcImg2, cv2.COLOR_BGRA2GRAY)) / 255.0

    center = (grayImg1.shape[0] / 2.0, grayImg1.shape[1] / 2.0)
    #    lpImg1 = cv2.logPolar(grayImg1, center, logpolarMagnitude, cv2.INTER_CUBIC + cv2.WARP_FILL_OUTLIERS)
    lpImg1 = _logPolar(grayImg1, center, logpolarMagnitude)
    center = (grayImg2.shape[0] / 2.0, grayImg2.shape[1] / 2.0)
    #    lpImg2 = cv2.logPolar(grayImg2, center, logpolarMagnitude, cv2.INTER_CUBIC + cv2.WARP_FILL_OUTLIERS)
    lpImg2 = _logPolar(grayImg2, center, logpolarMagnitude)

    if plotFig:
        figLP, (figLPLeft, figLPRight) = plt.subplots(ncols=2)
        figLPLeft.imshow(lpImg1, cmap="gray")
        figLPRight.imshow(lpImg2, cmap="gray")
        plt.show(figLP)

    hann = cv2.createHanningWindow(lpImg1.shape, cv2.CV_64F)
    ret, resp = cv2.phaseCorrelate(lpImg1, lpImg2, hann)

    #    angle = -((ret[1] + 0.5) * 360.0 / float(lpImg1.shape[0]) )
    #    scale = np.exp(ret[0] / logpolarMagnitude)
    angle = ret[1] * 360.0 / float(lpImg1.shape[0])
    scale = np.exp(ret[0] / logpolarMagnitude)
    #    print("ret = " + str(ret) + "\nresponse = " + str(resp))
    #    print("angle = " + str(angle) + "\nscale = " + str(scale))

    matTrans = cv2.getRotationMatrix2D(center, angle, scale)
    imgTrans = cv2.warpAffine(srcImg1,
                              matTrans,
                              srcImg1.shape[0:2],
                              flags=cv2.INTER_LINEAR)
    if plotFig:
        figDst, (figDstLeft, figDstRight) = plt.subplots(ncols=2)
        figDstLeft.imshow(imgTrans)
        figDstRight.imshow(srcImg2)
        plt.show(figDst)

        subtractImg = cv2.subtract(cv2.cvtColor(imgTrans, cv2.COLOR_BGRA2RGB),
                                   cv2.cvtColor(srcImg2, cv2.COLOR_BGRA2RGB))
        plt.imshow(subtractImg)
        plt.show()

    grayTrans = np.float64(cv2.cvtColor(imgTrans, cv2.COLOR_BGRA2GRAY)) / 255.0
    ret, resp = cv2.phaseCorrelate(grayTrans, grayImg2, hann)
    return angle, scale, ret, resp
Пример #24
0
 def train_H(self, train_data):
     h, w = train_data[0].shape
     self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
     g = np.zeros((h, w), np.float32)
     g[h // 2, w // 2] = 1
     g = cv2.GaussianBlur(g, (-1, -1), 2.0)
     g /= g.max()
     self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
     self.H1 = np.zeros_like(self.G)
     self.H2 = np.zeros_like(self.G)
     for elem in train_data:
         a = self.preprocess(elem)
         A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
         self.H1 += cv2.mulSpectrums(
             self.G, A, 0, conjB=True)  # conjB=True 表示在做乘法之前取第二个输入数组的共轭.
         self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
     self.update_kernel()
Пример #25
0
def phaseCorr(img1, img2, isUseHann=True, isDebug=False, isFloatAcc=True, RadPQ=3):
    assert(img1.shape==img2.shape)
    if isUseHann:
        hw=cv2.createHanningWindow(img1.shape[::-1], cv2.CV_64F)
        img1=img1*hw
        img2=img2*hw
    if isDebug:
        cv2.imshow("debug-img12", np.concatenate((nrmMat(img1), nrmMat(img2))))
    fft1=np.fft.fft2(img1)
    fft2=np.fft.fft2(img2)
    CC=np.fft.fftshift(np.fft.ifft2( (fft1*fft2.conj())/(fft2*fft2.conj()) ).real)
    minVal, maxVal, minLoc, maxLoc=cv2.minMaxLoc(CC)
    _,_,pq=calcPeakQ(CC,maxLoc,RadPQ)
    if isFloatAcc:
       maxLoc=calcWeightedCenter(CC,maxLoc,RadPQ)
    dxy=(maxLoc - np.array(img1.shape[::-1])/2) #TODO: Check this point !!!
    return (CC, dxy, maxVal,pq)
Пример #26
0
def phaseCorr(img1, img2, isUseHann=True, isDebug=False, isFloatAcc=True, RadPQ=3):
    assert(img1.shape==img2.shape)
    if isUseHann:
        hw=cv2.createHanningWindow(img1.shape[::-1], cv2.CV_64F)
        img1=img1*hw
        img2=img2*hw
    if isDebug:
        cv2.imshow("debug-img12", np.concatenate((nrmMat(img1), nrmMat(img2))))
    fft1=np.fft.fft2(img1)
    fft2=np.fft.fft2(img2)
    CC=np.fft.fftshift(np.fft.ifft2( (fft1*fft2.conj())/(fft2*fft2.conj()) ).real)
    minVal, maxVal, minLoc, maxLoc=cv2.minMaxLoc(CC)
    _,_,pq=calcPeakQ(CC,maxLoc,RadPQ)
    if isFloatAcc:
       maxLoc=calcWeightedCenter(CC,maxLoc,RadPQ)
    dxy=(maxLoc - np.array(img1.shape[::-1])/2) #TODO: Check this point !!!
    return (CC, dxy, maxVal,pq)
Пример #27
0
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect
        self.frameWidth = frame.shape[1]
        self.frameHeight = frame.shape[0]
        self.firstframe = frame[int(y1):int(y2), int(x1):int(x2)]
        cv2.imshow('first frame', self.firstframe)

        fout.write('bounding box initial position : ')
        fout.write(
            str(x1) + ',' + str(y1) + ',' + str(x2 - x1) + ',' + str(y2 - y1) +
            '\n')
        fout.write('tracking object from here \n')

        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
        self.pos = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        self.size = w, h
        img = cv2.getRectSubPix(frame, (w, h), (x, y))

        self.PF_number = n_PF
        self.prev_PF_count = n_PF
        self.f0 = np.array([])
        self.f = np.array([])
        self.pt = np.ones((n_PF, 2), int) * self.pos
        self.last_img_PF = np.array([])
        self.last_resp_PF = np.array([])

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
        self.update_kernel()
Пример #28
0
    def __init__(self, frame, rect):
        # Get frame size
        x1, y1, x2, y2 = rect

        # Resize to fft window
        w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
        x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2

        # Store position and size relative to frame
        self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
        self.size = w, h

        # Crop template image from frame
        img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Create Hanning window (weighting of values from center)
        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)

        # Create output image (centered Gaussian point--for correlation)
        g = np.zeros((h, w), np.float32)
        g[h//2, w//2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        # Save the desired output image in frequency space
        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)

        # Create transformed variants of input
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            # Preprocess, get DFT
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)

            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)  # Sum of G x F*
            self.H2 += cv2.mulSpectrums(     A, A, 0, conjB=True)  # Sum of F x F*

        # Update filter
        self.update_kernel()
        self.update(frame)
def calcPhaseCorrShift(fimg1, fimg2):
    frm1=cv2.imread(fimg1, 0)
    frm2=cv2.imread(fimg2, 0)
    frm1=cv2.resize(frm1.astype(np.float).copy(), (int(frm1.shape[1]/kdif), int(frm1.shape[0]/kdif)))
    frm2=cv2.resize(frm2.astype(np.float).copy(), (int(frm2.shape[1]/kdif), int(frm2.shape[0]/kdif)))
    frm1_nrm=cv2.normalize(frm1.copy(), None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    frm2_nrm=cv2.normalize(frm2.copy(), None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    hann=cv2.createHanningWindow((frm1.shape[1], frm1.shape[0]), cv2.CV_64F)
    #
    shift = cv2.phaseCorrelate(frm1, frm2, hann)
    dxy=shift[0]
    frm2_nrm_shift=np.roll(frm2_nrm, int(math.floor(-dxy[0])), 1)
    frm2_nrm_shift=np.roll(frm2_nrm_shift, int(math.floor(-dxy[1])), 0)
    tmp=np.zeros((frm1.shape[0], frm1.shape[1], 3), np.uint8)
    tmp[:,:,2]=frm1_nrm
    tmp[:,:,1]=frm2_nrm_shift
    tmp[:,:,0]=0
    # tmp[:,:,1]=frm2_nrm
    cv2.imshow("frame #2 shift",  cv2.resize(tmp, (tmp.shape[1]/1, tmp.shape[0]/1)))
    return dxy
Пример #30
0
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect
        self.frameWidth = frame.shape[1]
        self.frameHeight = frame.shape[0]
        self.firstframe = frame[int(y1):int(y2),int(x1):int(x2)]
        cv2.imshow('first frame',self.firstframe)

        fout.write('bounding box initial position : ')
        fout.write(str(x1)+','+ str(y1)+','+ str(x2-x1)+','+ str(y2-y1)+'\n')
        fout.write('tracking object from here \n')


        w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
        x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2
        self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
        self.size = w, h
        img = cv2.getRectSubPix(frame,  (w, h), (x, y))

        self.PF_number = n_PF
        self.prev_PF_count = n_PF
        self.f0 = np.array([])
        self.f = np.array([])
        self.pt = np.ones((n_PF, 2), int) * self.pos
        self.last_img_PF = np.array([])
        self.last_resp_PF = np.array([])

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h//2, w//2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        for i in xrange(128):
            a = self.preprocess(rnd_warp(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(     A, A, 0, conjB=True)
        self.update_kernel()
Пример #31
0
def register(fn1, fn2):
    im1 = cv2.imread(fn1)
    im2 = cv2.imread(fn2)

    # Create a Hanning window.
    print(im1.shape)
    im1_flt = tofloat(im1)
    im2_flt = tofloat(im2)
    hann = cv2.createHanningWindow(im1.shape[:2], cv2.CV_64F)

    # Find the shift using.
    shift = cv2.phaseCorrelate(im1_flt, im2_flt)
    print('shift: ', shift)
    radius = math.sqrt(shift.x * shift.x + shift.y*shift.y)

    if radius > 5:
        center = (im1.cols >> 1, im1.rows >> 1)
        cv2.circle(im1, center, int(radius), (0,255,0), 3)

    cv2.imshow('result', im1)
    cv2.waitKey(0)
Пример #32
0
    def __init__(self, frame, rect, trackNo, frameNo):
        x1, y1, x2, y2 = rect

        self.startpos = (x1, y1)
        self.startFrame = frameNo

        w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
        x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
        self.pos = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
        #self.lastPos = self.pos
        self.size = w, h
        self.objectMoved = False
        self.objectLeft = False
        self.stationaryCount = 0
        #self.isOverLap = False
        #print(frame,w,h,x,y)

        self.trackNo = trackNo
        img = cv2.getRectSubPix(frame, (w, h), (x, y))
        self.obj = None

        self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
        g = np.zeros((h, w), np.float32)
        g[h // 2, w // 2] = 1
        g = cv2.GaussianBlur(g, (-1, -1), 2.0)
        g /= g.max()

        self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)
        # print('in constructor - img = {}, img.shape = {}'.format(img, img.shape))
        for i in range(128):
            a = self.preprocess(Apply_affine(img))
            A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
            self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
            self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
        self.update_Filter()
        self.update(frame)
Пример #33
0
    def __init__(self, frame, rect): #given a initial frame, and ROI region (top-left,down-right)
        if len(frame.shape) == 3:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     
        x1, y1, x2, y2 = rect
        w_, h_ = x2-x1, y2-y1
        self.factor = 2 # region is factor * roi
        x1, y1 = (x1+x2- w_)//2, (y1+y2 - h_ )//2
        self.pos = x,y = x1+0.5*(w_-1), y1 + 0.5*(h_-1) #center 
        w, h = map(cv2.getOptimalDFTSize, [int(self.factor*w_), int(self.factor*h_)]) #get optimal DFT size
        self.size = w, h 

        img = cv2.getRectSubPix(frame, self.size, self.pos) #will auto padding if size exceeds boundaries
        self.win = cv2.createHanningWindow(self.size, cv2.CV_32F)
        self.X = self.preprocess(img, self.win) #first patch
      
        self.Y = self.target(w/self.factor, h/self.factor, self.factor) #expected response, gaussian distri.
 
        self.sigma = 0.2
        self.lamb = 0.01
        self.dgk = self.gaussian_kernel

        self.alphaf = self.training(self.X, self.Y, self.sigma, self.lamb)
    def test(self, frame):
        (x,y), (w,h) = self.pos, self.size
        win = cv2.createHanningWindow((w,h), cv2.CV_32F)

        img1 = self.preprocess(self.last_img, win)
        F1 = cv2.dft(img1, flags = cv2.DFT_COMPLEX_OUTPUT)
        G1 = cv2.mulSpectrums(F1, self.H, 0, conjB = True)
        resp1 = cv2.idft(G1, flags = cv2.DFT_SCALE|cv2.DFT_REAL_OUTPUT)
            
        img = cv2.getRectSubPix(frame, (w, h), (x,y))
        img2 = self.preprocess(img, win)
        F2 = cv2.dft(img2, flags = cv2.DFT_COMPLEX_OUTPUT)
        G2 = cv2.mulSpectrums(F2, self.H, 0, conjB = True)
        resp2 = cv2.idft(G2, flags = cv2.DFT_SCALE|cv2.DFT_REAL_OUTPUT)
    
        merge1 = np.hstack((img1 , resp1))
        merge2 = np.hstack((img2, resp2))
        merge = np.vstack((merge1, merge2))
    

        name = 'comparison--last vs cur.'
        cv2.namedWindow(name, cv2.WINDOW_NORMAL)
        cv2.resizeWindow(name, 600,400)
        cv2.imshow(name, merge)
Пример #35
0
    def match(self):
        height, width = self.ref.shape
        self.hanw = cv2.createHanningWindow((height, width), cv2.CV_64F)

        # Windowing and FFT
        G_a = np.fft.fft2(self.ref * self.hanw)
        G_b = np.fft.fft2(self.cmp * self.hanw)

        # 1.1: Frequency Whitening
        self.LA = np.fft.fftshift(np.log(np.absolute(G_a) + 1))
        self.LB = np.fft.fftshift(np.log(np.absolute(G_b) + 1))
        # 1.2: Log polar Transformation
        cx = self.center[1]
        cy = self.center[0]
        self.Mag = width / (math.log(width) - math.log(2) * 0.5)
        self.LPA = cv2.logPolar(self.LA, (cy, cx),
                                self.Mag,
                                flags=cv2.INTER_LINEAR +
                                cv2.WARP_FILL_OUTLIERS)
        self.LPB = cv2.logPolar(self.LB, (cy, cx),
                                self.Mag,
                                flags=cv2.INTER_LINEAR +
                                cv2.WARP_FILL_OUTLIERS)

        # 1.3:filtering
        LPmin = math.floor(self.Mag *
                           math.log(self.alpha * width / 2.0 / math.pi))
        LPmax = min(width,
                    math.floor(self.Mag * math.log(width * self.beta / 2)))
        assert LPmax > LPmin, 'Invalid condition!\n Enlarge lpmax tuning parameter or lpmin_tuning parameter'
        Tile = np.repeat([0.0, 1.0, 0.0],
                         [LPmin - 1, LPmax - LPmin + 1, width - LPmax])
        self.Mask = np.tile(Tile, [height, 1])
        self.LPA_filt = self.LPA * self.Mask
        self.LPB_filt = self.LPB * self.Mask

        # 1.4: Phase Correlate to Get Rotation and Scaling
        Diff, peak, self.r_rotatescale = self.PhaseCorrelation(
            self.LPA_filt, self.LPB_filt)
        theta1 = 2 * math.pi * Diff[1] / height
        # deg
        theta2 = theta1 + math.pi
        # deg theta ambiguity
        invscale = math.exp(Diff[0] / self.Mag)
        # 2.1: Correct rotation and scaling
        b1 = self.Warp_4dof(self.cmp, [0, 0, theta1, invscale])
        b2 = self.Warp_4dof(self.cmp, [0, 0, theta2, invscale])

        # 2.2 : Translation estimation
        diff1, peak1, self.r1 = self.PhaseCorrelation(
            self.ref, b1)  #diff1, peak1 = PhaseCorrelation(a,b1)
        diff2, peak2, self.r2 = self.PhaseCorrelation(
            self.ref, b2)  #diff2, peak2 = PhaseCorrelation(a,b2)
        # Use cv2.phaseCorrelate(a,b1) because it is much faster

        # 2.3: Compare peaks and choose true rotational error
        if peak1 > peak2:
            Trans = diff1
            peak = peak1
            theta = -theta1
        else:
            Trans = diff2
            peak = peak2
            theta = -theta2

        if theta > math.pi:
            theta -= math.pi * 2
        elif theta < -math.pi:
            theta += math.pi * 2

        # Results
        self.param = [Trans[0], Trans[1], theta, 1 / invscale]
        self.peak = peak
        self.perspective = self.poc2warp(self.center, self.param)
        self.affine = self.perspective[0:2, :]
Пример #36
0
	def _process_movie(self, **kwargs):
	
		"""
			Function for analyzing a full film or video. This is where the magic happens when we're making analyses. Function will exit if neither a movie path nor a data path are supplied. This function is not intended to be called directly. Normally, call one of the two analyze_ functions instead, which will call this function.
		
		"""

		ap = self._check_pcorr_params(kwargs)
		verbose = ap['verbose']

		if not HAVE_CV:
			print "WARNING: You must install OpenCV in order to analyze or view!"
			return
		
		if (self.movie_path is None) and (self.data_path is None) and ap['mode'] is 'analysis':
			print "ERROR: Must supply both a movie and a data path for analysis!"
			return
		
		have_mov = os.path.exists(self.movie_path)
		have_data = os.path.exists(self.data_path)
		
		if (have_mov is False) and (have_data is False):
			print "Both movie file and data file are missing! Please supply at least one."
			return None
		
		print ap

		if have_mov:
	 		self.capture = cv2.VideoCapture(self.movie_path)
			frame_width = int(self.capture.get(cv.CV_CAP_PROP_FRAME_WIDTH))
			frame_height = int(self.capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
		else:
			frame_width = 640
			frame_height = int(frame_width / self._read_json_value('aspect'))
		
		fps = ap['fps']
		grid_x_divs = ap['grid_divs_x']
		grid_y_divs = ap['grid_divs_y']
		frame_size = (frame_width, frame_height)
		grid_width = int(frame_width/grid_x_divs)
		grid_height = int(frame_height/grid_y_divs)
		grid_size = (grid_width, grid_height)
		
		centers_x = range((frame_width/16),frame_width,(frame_width/8))
		centers_y = range((frame_height/16),frame_height,(frame_height/8))
		
		if verbose:
			print fps, ' | ', frame_size, ' | ', grid_size
		
		# container for prev. frame's grayscale subframes
		prev_sub_grays = []								

		if ap['offset'] > 0:
			offset_secs = ap['offset']
		else:
			offset_secs = 0
		
		print "%%% ", ap['duration']
		
		if ap['duration'] > 0:
			dur_secs = ap['duration']
		elif ap['duration'] < 0:
			ap['duration'] = self.determine_movie_length()
		else:
			print "Duration cannot be 0."
			return
		dur_secs = ap['duration']
		
		stride_frames = ap['stride']
		stride_hop = stride_frames - 1

		print "1. ", ap['duration']
		print "2. ", dur_secs
		
		# check offset first, then compress duration, if needed
		offset_secs = min(max(offset_secs, 0), ap['duration'])
		dur_secs = min(max(dur_secs, 0), (ap['duration'] - offset_secs))
		offset_strides = int(offset_secs * (fps / stride_frames))
		dur_strides = int(dur_secs * (fps / stride_frames))
		offset_frames = offset_strides * stride_frames
		dur_frames = dur_strides * stride_frames
		
		if verbose:
			print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
			print 'DUR TOTAL: ', dur_total_secs
			print "OFFSET (SECONDS): ", offset_secs
			print "OFFSET (STRIDES): ", offset_strides
			print "OFFSET (FRAMES): ", offset_frames
			print "DUR (SECONDS): ", dur_secs
			print 'DUR (STRIDES): ', dur_strides
			print 'DUR (FRAMES): ', dur_frames
			print 'XDIVS: ', grid_x_divs
			print 'YDIVS: ', grid_y_divs
			print "FPS: ", fps
			print "stride_frames: ", stride_frames
		
		# set up memmap
		if ap['mode'] == 'playback' and ap['display'] == True:
			fp = np.memmap(self.data_path, dtype='float32', mode='r', shape=((offset_strides + dur_strides),(64+1),2))
		else:
			fp = np.memmap(self.data_path, dtype='float32', mode='w+', shape=(dur_strides,(64+1),2))
		
		if ap['display']:
			cv.NamedWindow('Image', cv.CV_WINDOW_AUTOSIZE)
			cv.ResizeWindow('Image', frame_width, frame_height)
		
		self.frame_idx = offset_frames
		end_frame = offset_frames + dur_frames

		if have_mov:
			self.capture.set(cv.CV_CAP_PROP_POS_FRAMES, offset_frames)
			ret, frame = self.capture.read()
			if frame is None: 
				print 'Frame error! Exiting...'
				return # no image captured... end the processing		
			frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
			prev_frame_gray = np.float32(frame_gray[:])
		
			fhann = cv2.createHanningWindow((frame_width,frame_height), cv2.CV_32FC1)
			ghann = cv2.createHanningWindow((grid_width,grid_height), cv2.CV_32FC1)
		
			for row in range(grid_y_divs):
				for col in range(grid_x_divs):
					prev_sub_grays += [np.float32(frame_gray[(row*grid_height):((row+1)*grid_height), (col*grid_width):((col+1)*grid_width)])]
		
		else:
			frame = np.empty((frame_width, frame_height), np.uint8)
			print frame.shape

		self.frame_idx += 1
		
		print "<<< ", frame.mean()
# 		cv.ShowImage('Image', cv.fromarray(frame))



		while self.frame_idx < end_frame:
			
			if (self.frame_idx % 1000) == 0: print 'fr. idx: ', self.frame_idx, ' (', self.frame_idx / float(end_frame), ' | ', end_frame, ')'

			if have_mov:
				# grab next frame
				ret, frame = self.capture.read()
				if frame is None: 
					print 'Frame error! Exiting...'
					break # no image captured... end the processing
				frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
			else:
				frame[:] = 0
			
			# display stage (full)
			if ap['mode'] == 'playback' and ap['display']:
				fret = fp[self.frame_idx][64]
				# print fret
			elif have_mov:
				fret, fres = cv2.phaseCorrelateRes(prev_frame_gray, np.float32(frame_gray[:]), fhann)
				print fret
				if abs(fres) > 0.01:
					fp[self.frame_idx][64] = [(fret[0]/frame_width),(fret[1]/frame_height)]
				else:
					fp[self.frame_idx][64] = [0,0]
			else:
				return
			
			# display stage (gridded)
			for row in range(grid_y_divs):
				for col in range(grid_x_divs):
					if ap['mode'] == 'playback' and ap['display']:
						cell = ((row*8)+col)
						gret = fp[self.frame_idx][cell]
					elif have_mov:
						sub_gray = np.float32(frame_gray[(row*grid_height):((row+1)*grid_height), (col*grid_width):((col+1)*grid_width)][:])
						gret, gres = cv2.phaseCorrelateRes(prev_sub_grays[(row*grid_x_divs)+col], sub_gray, ghann)
						prev_sub_grays[(row*grid_x_divs)+col] = sub_gray
						if abs(gres) > 0.7: # WAS 0.01!!!!
							fp[self.frame_idx][(row*grid_x_divs)+col] = [(gret[0]/grid_width),(gret[1]/grid_height)]
 						else:
							fp[self.frame_idx][(row*grid_x_divs)+col] = [0,0]
					else:
						return
					
					if ap['display'] and (gret[0] != 0 and gret[1] != 0):
						print gret
						print grid_size
						print centers_x[col]
						print centers_y[row]
						xval = int(min((gret[0]*1000), grid_size[0])+centers_x[col])
						yval = int(min((gret[1]*1000), grid_size[1])+centers_y[row])
						# print ((centers_x[i], centers_y[j], xval, yval), False, (0,255,255))
						print (centers_x[col], centers_y[row])	
						print (xval, yval)
						cv2.line(frame, (centers_x[col], centers_y[row]), (xval, yval), (255,255,255))
				
			#### SHOW
			if ap['display'] and have_mov:
				print "<<< ", frame.mean()
				cv.ShowImage('Image', cv.fromarray(frame))
			fp.flush()
			
			self.frame_idx += 1
			if have_mov:
				self.prev_gray = np.float32(frame_gray[:])
			
			# handle events for abort
			k = cv.WaitKey (int(1000 / ap['afps']))	
			if k % 0x100 == 27:
				# user has press the ESC key, so exit
					break
		
		del fp
		if ap['display']:
			cv2.destroyAllWindows()
Пример #37
0
fnfrm1='/home/ar/video/drone_project/2/frame_364_640x480.3.png'
fnfrm2='/home/ar/video/drone_project/2/frame_420_640x480.3.png'

# fnfrm1='/home/ar/data/UAV_Drone_Project/VID_20150324_012829/image-01-013.jpeg'
# fnfrm2='/home/ar/data/UAV_Drone_Project/VID_20150324_012829/image-01-014.jpeg'

xy_frm1=np.array([825, 618])
xy_frm2=np.array([1147, 675])

if __name__=='__main__':
    frm1=cv2.imread(fnfrm1, 0)
    frm2=cv2.imread(fnfrm2, 0)
    frm1=frm1.astype(np.float)
    frm2=frm2.astype(np.float)
    hann=cv2.createHanningWindow((frm1.shape[1], frm1.shape[0]), cv2.CV_64F)
    hann2=cv2.createHanningWindow((100,100), cv2.CV_64F)
    hann2_nrm=cv2.normalize(hann2, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    cv2.imshow("F**K", hann2_nrm)
    shift = cv2.phaseCorrelate(frm1, frm2, hann)
    dxy=shift[0]
    print shift
    print xy_frm2-xy_frm1
    hann_nrm=cv2.normalize(hann, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    frm1_nrm=cv2.normalize(frm1, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    frm2_nrm=cv2.normalize(frm2, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U)
    cv2.imshow("hanning",   hann_nrm)
    cv2.imshow("frame #1",  frm1_nrm)
    # cv2.imshow("frame #2",  frm2_nrm)
    frm2_nrm_shift=np.roll(frm2_nrm, int(math.floor(-dxy[0])), 1)
    frm2_nrm_shift=np.roll(frm2_nrm_shift, int(math.floor(-dxy[1])), 0)
 img2F=np.abs(np.fft.fftshift(np.fft.fft2(img2)))
 img1Fs=ph.nrmMat(np.abs(np.log(img1F+1.0)))
 img2Fs=ph.nrmMat(np.abs(np.log(img2F+1.0)))
 cv2.imshow("win-img-12", np.dstack((img1,img2,img1)).astype(np.uint8) )
 # cv2.imshow("win-fft-12", np.dstack((img1Fs,img2Fs,img1Fs)) )
 CC,dxy,ccval=ph.phaseCorr(img1, img2, isDebug=False)
 print dxy, ccval
 # cv2.imshow("win-CC", nrmMat(CC) )
 img2Shift=np.roll(np.roll(img2,dxy[0],1), dxy[1], 0)
 # cv2.imshow("win-shift-12", np.dstack((nrmMat(img1),nrmMat(img2Shift),nrmMat(img1))) )
 #
 # Rotate-Scale
 img1FLP,MM=ph.getLogPolar(img1F.copy())
 img2FLP,MM=ph.getLogPolar(img2F.copy())
 # hwLP=getCylHanning(img1FLP.shape, axis=0)
 hwLP=cv2.createHanningWindow(img1FLP.shape[::-1], cv2.CV_64F)
 img1FLP=img1FLP*hwLP
 img2FLP=img2FLP*hwLP
 #
 img1FLPN=ph.nrmMat( np.abs(np.log(img1FLP+1.0)) )
 img2FLPN=ph.nrmMat( np.abs(np.log(img2FLP+1.0)) )
 # cv2.imshow("win-Hann-LP", nrmMat(hwLP) )
 cv2.imshow("win-fft-LP-img1", np.dstack((img1FLPN,img2FLPN,img1FLPN)) )
 CCRS,dxyRS,ccvalRS=ph.phaseCorr(img1FLP,img2FLP)
 #
 dAng=(360./CCRS.shape[0])*dxyRS[1]
 dScl=np.exp(float(dxyRS[0])/MM)
 print dxyRS, ccvalRS, ", dAng = ", dAng, ", dScale=", dScl
 #
 rotm2=cv2.getRotationMatrix2D((img2.shape[1]/2, img2.shape[0]/2), -dAng, 1./dScl)
 print rotm2
Пример #39
0
 def load_H(self, H_file):
     self.H = np.load(H_file)
     h, w = self.H.shape[:2]
     self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
Пример #40
0
 def preprocess(self, img, size):
     img = np.log(np.float32(img)+1.0)
     img = (img-img.mean()) / (img.std()+eps)
     win = cv2.createHanningWindow((size[0], size[1]), cv2.CV_32F)
     return img*win
Пример #41
0
def _process_phase_corr(**kwargs):

	if not HAVE_CV:
		print "WARNING: You must install OpenCV in order to analyze or view!"
		return
	
	ap = kwargs
	ap = {'fps':24, 'grid_divs_x':8, 'grid_divs_y':8, 'stride':STRIDE, 'mode':'analysis', 'display': True}
	
#	fstring = '~/dev/git/synaesthesia/films/Synae_test_v2.mov'
 	fstring = '~/Movies/action/JoggersRaining/JoggersRaining.mov'
# 	fstring = '~/Pictures/iPhoto Library/Masters/2012/05/31/20120531-210318/IMG_0140.MOV'
	
	movie_path = ap['movie_path'] = os.path.expanduser(fstring)
	
	data_path = ap['data_path'] = os.path.expanduser('~/dev/git/synaesthesia/films/Synae_test_v2.pkl')
	verbose = True
	
	if (ap['movie_path'] is None) or (ap['data_path'] is None):
		print "ERROR: Must supply both a movie and a data path!"
		return	
	
	capture = cv2.VideoCapture(ap['movie_path'])
	
	fps = ap['fps']
	grid_x_divs = ap['grid_divs_x']
	grid_y_divs = ap['grid_divs_y']
	frame_width = int(capture.get(cv.CV_CAP_PROP_FRAME_WIDTH))
	frame_height = int(capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
	frame_size = (frame_width, frame_height)
	grid_width = int((frame_width/grid_x_divs)/DSF)
	grid_height = int((frame_height/grid_y_divs)/DSF)
	grid_size = (grid_width, grid_height)
	
	centers_x = range((frame_width/16/DSF),(frame_width/DSF),(frame_width/8/DSF))
	centers_y = range((frame_height/16/DSF),(frame_height/DSF),(frame_height/8/DSF))
	
	if verbose:
		print fps, ' | ', frame_size, ' | ', grid_size
	
	# container for prev. frame's grayscale subframes
	prev_sub_grays, prev_grid_xvals, prev_grid_yvals = [], [0 for i in range(64)], [0 for i in range(64)]
	
	
	# last but not least, get total_frame_count and set up the memmapped file
	dur_total_secs = int(capture.get(cv.CV_CAP_PROP_FRAME_COUNT) / fps)
	stride_frames = ap['stride']
	# if ap['duration'] < 0:
	dur_secs = dur_total_secs
	# else:
	#	dur_secs = ap['duration']
	
	offset_secs = 0 # min(max(ap['offset'], 0), dur_total_secs)
	dur_secs = min(max(dur_secs, 0), (dur_total_secs - offset_secs))
	offset_strides = int(offset_secs * (fps / stride_frames))
	dur_strides = int(dur_secs * (fps / stride_frames))
	offset_frames = offset_strides * stride_frames
	dur_frames = dur_strides * stride_frames
			
	if verbose:
		print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
		print 'FRAMES: ', int(capture.get(cv.CV_CAP_PROP_FRAME_COUNT))
		print 'DUR TOTAL: ', dur_total_secs
		print "OFFSET (SECONDS): ", offset_secs
		print "OFFSET (STRIDES): ", offset_strides
		print "OFFSET (FRAMES): ", offset_frames
		print "DUR (SECONDS): ", dur_secs
		print 'DUR (STRIDES): ', dur_strides
		print 'DUR (FRAMES): ', dur_frames
		print 'XDIVS: ', grid_x_divs
		print 'YDIVS: ', grid_y_divs
		print "FPS: ", fps
		print "stride_frames: ", stride_frames
	
	# set up memmap
	if ap['mode'] == 'playback' and ap['display'] == True:
		fp = np.memmap(data_path, dtype='float32', mode='r+', shape=((offset_strides + dur_strides),64,2))
	else:
		fp = np.memmap(data_path, dtype='float32', mode='w+', shape=(dur_strides,64,2))
	
	# set some drawing constants
	frame_idx = offset_frames
	end_frame = offset_frames + dur_frames
	
	if ap['display']:
		cv.NamedWindow('Image', cv.CV_WINDOW_AUTOSIZE)

	capture.set(cv.CV_CAP_PROP_POS_FRAMES, offset_frames)
		
	fhann = cv2.createHanningWindow((frame_width/DSF,frame_height/DSF), cv2.CV_32FC1)
	ghann = cv2.createHanningWindow((grid_width/DSF,grid_height/DSF), cv2.CV_32FC1)
	
	# load arrays with default vals
	for row in range(grid_y_divs):
		for col in range(grid_x_divs):
			#prev_sub_grays += [np.float32(frame_gray_pyr[(row*grid_height):((row+1)*grid_height), (col*grid_width):((col+1)*grid_width)])]
# 			sub_grays += [np.zeros_like(ghann, dtype=np.int8)]
			prev_sub_grays += [np.zeros_like(ghann, dtype=np.int8)]
			
			
			xval = int(0+centers_x[col])
			yval = int(0+centers_y[row])
			
			frame_gray_pyr = np.zeros_like(fhann, dtype=np.int8)
			prev_frame_gray_pyr = np.zeros_like(fhann, dtype=np.int8)
	
	ds_grid_height = grid_height/DSF
	ds_grid_width = grid_width/DSF

	while frame_idx < end_frame:
		
		if ((frame_idx % stride_frames) == 0):
			t = clock()
			print '1. fr. idx: ', frame_idx, ' (', frame_idx / float(end_frame), ' | ', end_frame, ')'

			# grab next frame
			ret, frame = capture.read()
			dt = clock() - t
			print 'time: %.1f ms' % (dt*1000)
			t = clock()
			if frame is None: 
				print 'Frame error! Exiting...'
				break # no image captured... end the processing
			prev_frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
			if DSF > 1:
				prev_frame_gray_pyr = cv2.pyrDown(prev_frame_gray)
			else:
				prev_frame_gray_pyr = prev_frame_gray #[:]
			
			# grid stage (prev frame)
			for row in range(grid_y_divs):
				for col in range(grid_x_divs):
					cell = ((row*grid_x_divs)+col)
					prev_sub_grays[(row*grid_x_divs)+col] = prev_frame_gray_pyr[(row*ds_grid_height):((row+1)*ds_grid_height), (col*ds_grid_width):((col+1)*ds_grid_width)]
			dt = clock() - t
			print '2. time: %.1f ms' % (dt*1000)
			
		if (frame_idx % stride_frames) == 1: 
			# grab next frame
			t = clock()
			ret, frame = capture.read()
			if frame is None: 
				print 'Frame error! Exiting...'
				break # no image captured... end the processing
			frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
			if DSF > 1:
				frame_gray_pyr = cv2.pyrDown(frame_gray)
			else:
				frame_gray_pyr = frame_gray #[:]

			
			# grid stage (full)
			for row in range(grid_y_divs):
				for col in range(grid_x_divs):
					if ap['mode'] == 'playback':
						cell = ((row*grid_x_divs)+col)
						gres = fp[(frame_idx/stride_frames)][cell]
					else:
# 						print '--grid-----------'
# 						print (row*ds_grid_height)
# 						print (col*ds_grid_width)
# 						print (row,col)
						sub_gray = np.float32(frame_gray_pyr[(row*ds_grid_height):((row+1)*ds_grid_height), (col*ds_grid_width):((col+1)*ds_grid_width)])
# 						print sub_gray.shape
# 						print np.float32(prev_sub_grays[(row*grid_x_divs)+col]).shape
# 						print ghann.shape
						gres = cv2.phaseCorrelateRes(np.float32(prev_sub_grays[(row*grid_x_divs)+col]), sub_gray, ghann)
						
						dflag = 0
						if abs(gres[1]) > 0.9:
							fp[(frame_idx/stride_frames)][(row*grid_x_divs)+col] = [(gres[0][0]/grid_width),(gres[0][1]/grid_height)]
							dflag = 1
						else:
							fp[(frame_idx/stride_frames)][(row*grid_x_divs)+col] = [0,0]

					if ap['display'] == True and dflag > 0:
					
						xvec = gres[0][0]*1000.0
						yvec = gres[0][1]*1000.0
						
						 #sqrt preserving signs:
						if xvec < 0:
							xval = int((-1*((-1*xvec) ** 0.5))+centers_x[col])
						else:
							xval = int((xvec ** 0.5)+centers_x[col])
						if yvec < 0:
							yval = int((-1*((-1*yvec) ** 0.5))+centers_y[row])
						else:
							yval = int((yvec ** 0.5)+centers_y[row])

# 						xval = int(xvec)+centers_x[col]
# 						yval = int(yvec)+centers_y[row]
						#delta = ((xval - prev_grid_xvals[(row*grid_x_divs)+col]) ** 2.0) + ((yval - prev_grid_yvals[(row*grid_x_divs)+col]) ** 2.0)
					
						#print delta
						#if delta < MAXDIST:
						cv2.line(frame_gray_pyr, (centers_x[col], centers_y[row]), (xval, yval), (255,255,255))

						#prev_grid_xvals[(row*grid_x_divs)+col] = xval
						#prev_grid_yvals[(row*grid_x_divs)+col] = yval
			print 
			dt = clock() - t
			print 'time: %.1f ms' % (dt*1000)
			
			#### SHOW
			if ap['display']:
				print "show"
				cv.ShowImage('Image', cv.fromarray(frame_gray_pyr))
			fp.flush()
		
		if ((frame_idx % stride_frames) == 0):
			frame_idx += 1
		elif (frame_idx % stride_frames) == 1:
			frame_idx += 5
		capture.set(cv.CV_CAP_PROP_POS_FRAMES, frame_idx)
		
# 		print "fr. idx.: ", frame_idx
		# prev_frame_gray_pyr = np.float32(frame_gray_pyr[:])
		
		# handle events for abort
		k = cv2.waitKey (1)
		if k % 0x100 == 27:
			# user has press the ESC key, so exit
				break
	
	del fp
	if ap['display']:
		cv2.destroyAllWindows()
Пример #42
0
 def preprocess(self, img, size):
     img = np.log(np.float32(img)+1.0)
     img = (img-img.mean()) / (img.std()+eps)
     win = cv2.createHanningWindow((size[0], size[1]), cv2.CV_32F)
     return img*win
import cv2
import numpy as np

img = cv2.imread('C:\\Users\\ankitdeora2856\\Desktop\\pic1.jpg')

res = cv2.getRectSubPix(img,(200,200),(320,500))
win = cv2.createHanningWindow((500, 300), cv2.CV_32F)
blur = cv2.GaussianBlur(img,(-1,-1),10)

win0 = cv2.resize(win,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)

#cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img',win0)
cv2.waitKey(0)
cv2.destroyAllWindows()
Пример #44
0
def POC(a, b):
    imshowflag = 1  # show the processing image
    a = a.astype(np.float32)
    b = b.astype(np.float32)
    height, width = a.shape
    hann = cv2.createHanningWindow((height, width), cv2.CV_64F)
    # rhann = np.sqrt(hann)

    # Windowing and FFT
    G_a = np.fft.fft2(a * hann)
    G_b = np.fft.fft2(b * hann)

    # 1. Get Rotation and Scaling Error
    # 1.1: Frequency Whitening
    LA = np.fft.fftshift(np.log(np.absolute(G_a) + 1))
    LB = np.fft.fftshift(np.log(np.absolute(G_b) + 1))
    # 1.2: Log polar
    cx = width / 2
    cy = height / 2
    Mag = width / math.log(width)
    LPA = cv2.logPolar(LA, (cy, cx), Mag, cv2.INTER_LINEAR)
    LPB = cv2.logPolar(LB, (cy, cx), Mag, cv2.INTER_LINEAR)

    # 1.3: Filtering
    # --------------- Tuning parameter ----------------
    lpmin_tuning = 0.5  # default 0.5
    lpmax_tuning = 0.8  # default 0.8
    # -------------------------------------------------
    LPmin = math.floor(Mag * math.log(lpmin_tuning * width / 2.0 / math.pi))
    LPmax = min(width, math.floor(Mag * math.log(width * lpmax_tuning / 2)))
    assert LPmax > LPmin, 'Invalid condition!\n Enlarge lpmax tuning parameter or lpmin_tuning parameter'
    Tile = np.repeat([0.0, 1.0, 0.0],
                     [LPmin - 1, LPmax - LPmin + 1, width - LPmax])
    Mask = np.tile(Tile, [height, 1])
    LPA_filt = LPA * Mask
    LPB_filt = LPB * Mask

    # 1.4: Phase Correlate to Get Rotation and Scaling
    Diff, peak = PhaseCorrelation(LPA_filt, LPB_filt)
    # Do not use "cv2.phaseCorrelate(LPA_filt,LPB_filt)" because the 2nd order fitting process is not suitable for this fucntion.

    #  Final output of scale and rotation
    theta1 = 360 * Diff[1] / height
    # deg
    theta2 = theta1 + 180
    # deg theta ambiguity
    #invscale = math.pow(float(width),Diff[0]/float(width))
    invscale = math.exp(Diff[0] / Mag)
    # print('Theta? ',-theta1,'Scale ',1/invscale)

    # 2.1: Correct rotation and scaling
    b1 = Warp_4dof(b, 0, 0, theta1 * math.pi / 180, invscale)
    b2 = Warp_4dof(b, 0, 0, theta2 * math.pi / 180, invscale)

    # 2.2 : Translation estimation
    diff1, peak1 = cv2.phaseCorrelate(
        a, b1)  #diff1, peak1 = PhaseCorrelation(a,b1)
    diff2, peak2 = cv2.phaseCorrelate(
        a, b2)  #diff2, peak2 = PhaseCorrelation(a,b2)
    # Use cv2.phaseCorrelate(a,b1) because it is much faster

    # 2.3: Compare peaks and choose true rotational error
    if peak1 > peak2:
        Trans = diff1
        peak = peak1
        theta = -theta1
    else:
        Trans = diff2
        peak = peak2
        theta = -theta2

    if theta > 180:
        theta -= 360
    elif theta < -180:
        theta += 360

    if 0:
        # Imshow
        plt.subplot(5, 2, 1)
        plt.imshow(LA, vmin=LA.min(), vmax=LA.max())
        plt.subplot(5, 2, 2)
        plt.imshow(LB, vmin=LB.min(), vmax=LB.max())
        plt.subplot(5, 2, 3)
        plt.imshow(LPA, vmin=LPA.min(), vmax=LPA.max(), cmap="gray")
        plt.subplot(5, 2, 4)
        plt.imshow(LPB, vmin=LPB.min(), vmax=LPB.max(), cmap="gray")
        plt.subplot(5, 2, 5)
        plt.imshow(LPA_filt,
                   vmin=LPA_filt.min(),
                   vmax=LPA_filt.max(),
                   cmap="gray")
        plt.subplot(5, 2, 6)
        plt.imshow(LPB_filt,
                   vmin=LPB_filt.min(),
                   vmax=LPB_filt.max(),
                   cmap="gray")
        plt.subplot(5, 2, 7)
        plt.imshow(b1, vmin=b1.min(), vmax=b1.max(), cmap="gray")
        plt.subplot(5, 2, 8)
        plt.imshow(b2, vmin=b2.min(), vmax=b2.max(), cmap="gray")

    return [Trans[0], Trans[1], theta, 1 / invscale], peak
Пример #45
0
 img2F = np.abs(np.fft.fftshift(np.fft.fft2(img2)))
 img1Fs = ph.nrmMat(np.abs(np.log(img1F + 1.0)))
 img2Fs = ph.nrmMat(np.abs(np.log(img2F + 1.0)))
 cv2.imshow("win-img-12", np.dstack((img1, img2, img1)).astype(np.uint8))
 # cv2.imshow("win-fft-12", np.dstack((img1Fs,img2Fs,img1Fs)) )
 CC, dxy, ccval = ph.phaseCorr(img1, img2, isDebug=False)
 print dxy, ccval
 # cv2.imshow("win-CC", nrmMat(CC) )
 img2Shift = np.roll(np.roll(img2, dxy[0], 1), dxy[1], 0)
 # cv2.imshow("win-shift-12", np.dstack((nrmMat(img1),nrmMat(img2Shift),nrmMat(img1))) )
 #
 # Rotate-Scale
 img1FLP, MM = ph.getLogPolar(img1F.copy())
 img2FLP, MM = ph.getLogPolar(img2F.copy())
 # hwLP=getCylHanning(img1FLP.shape, axis=0)
 hwLP = cv2.createHanningWindow(img1FLP.shape[::-1], cv2.CV_64F)
 img1FLP = img1FLP * hwLP
 img2FLP = img2FLP * hwLP
 #
 img1FLPN = ph.nrmMat(np.abs(np.log(img1FLP + 1.0)))
 img2FLPN = ph.nrmMat(np.abs(np.log(img2FLP + 1.0)))
 # cv2.imshow("win-Hann-LP", nrmMat(hwLP) )
 cv2.imshow("win-fft-LP-img1", np.dstack((img1FLPN, img2FLPN, img1FLPN)))
 CCRS, dxyRS, ccvalRS = ph.phaseCorr(img1FLP, img2FLP)
 #
 dAng = (360. / CCRS.shape[0]) * dxyRS[1]
 dScl = np.exp(float(dxyRS[0]) / MM)
 print dxyRS, ccvalRS, ", dAng = ", dAng, ", dScale=", dScl
 #
 rotm2 = cv2.getRotationMatrix2D((img2.shape[1] / 2, img2.shape[0] / 2),
                                 -dAng, 1. / dScl)
Пример #46
0
def getLittleF(gray):
    inter = np.log(gray.astype('float64') + 1)
    inter2 = (inter - np.amin(inter)) / np.amax(inter)
    return cv2.createHanningWindow((inter2.shape[1], inter2.shape[0]), cv2.CV_64F) * inter2
Пример #47
0
 def init_hanning_window(self):
     self.win = cv2.createHanningWindow(tuple(self.size), cv2.CV_32F)
     return self