Esempio n. 1
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)
Esempio n. 2
0
    def update(self, frame, rate=0.125):
        (x_ori, y_ori), (w, h) = self.pos, self.size
        x, y = x_ori, y_ori
        #best_x, best_y, best_dx, best_dy, best_psr = 0,0,0,0,0
        #for y_iter in range(int(max(0, y_ori-h)), int(min(y_ori+h, frame.shape[0] - h + 1)), h//3):
        #    for x_iter in range(int(max(0, x_ori-w)), int(min(x_ori+w, frame.shape[1] - w + 1)), w//3):
        #        x, y = x_iter, y_iter
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)
        self.last_resp, (dx, dy), self.psr = self.correlate(img)
        '''        if self.psr > best_psr:
                    best_x, best_y, best_dx, best_dy = x, y, dx, dy
                    best_psr = self.psr'''
        #self.psr = best_psr
        #x, y, dx, dy = best_x, best_y, best_dx, best_dy
        self.good = self.psr > 8.0
        if not self.good:
            return

        self.pos = x + dx, y + dy
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate
        self.update_kernel()
Esempio n. 3
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)
Esempio n. 4
0
	def gaussianCorrelation(self, x1, x2):
		if(self._hogfeatures):
			c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
			for i in xrange(self.size_patch[2]):
				x1aux = x1[i, :].reshape((self.size_patch[0], self.size_patch[1]))
				x2aux = x2[i, :].reshape((self.size_patch[0], self.size_patch[1]))
				caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB = True)
				caux = real(fftd(caux, True))
				#caux = rearrange(caux)
				c += caux
			c = rearrange(c)
		else:
			c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB = True)   # 'conjB=' is necessary!
			c = fftd(c, True)
			c = real(c)
			c = rearrange(c)

		if(x1.ndim==3 and x2.ndim==3):
			d = (np.sum(x1[:,:,0]*x1[:,:,0]) + np.sum(x2[:,:,0]*x2[:,:,0]) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
		elif(x1.ndim==2 and x2.ndim==2):
			d = (np.sum(x1*x1) + np.sum(x2*x2) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])

		d = d * (d>=0)
		d = np.exp(-d / (self.sigma*self.sigma))

		return d
Esempio n. 5
0
    def update(self, frame, rate=0.125, img_override=None):
        (x, y), (w, h) = self.pos, self.size

        if img_override is None:
            self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        else:
            self.last_img = img = img_override

        img = self.preprocess(img)

        self.last_resp, (dx, dy), self.psr = self.correlate(img)
        self.good = self.psr > self.MIN_PSR

        if not self.good:
            return

        self.pos = x + dx, y + dy
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)

        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate

        self.update_kernel()
Esempio n. 6
0
    def update(self, frame, rate=0.125):
        (x, y), (w, h) = self.pos, self.size

        self.lastPos = self.pos
        #self.lastsize = self.size
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)
        self.last_resp, (dx, dy), self.psr = self.correlate(img)

        if dx != 0 or dy != 0:
            self.objectMoved = True

        self.good = self.psr >= 3.0
        if not self.good:
            return

        self.pos = x + dx, y + dy

        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate
        self.update_Filter()
    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)
Esempio n. 8
0
    def gaussianCorrelation(self, x1, x2):
        if(self._hogfeatures):
            c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
            for i in xrange(self.size_patch[2]):
                x1aux = x1[i, :].reshape((self.size_patch[0], self.size_patch[1]))
                x2aux = x2[i, :].reshape((self.size_patch[0], self.size_patch[1]))
                caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB = True)
                caux = real(fftd(caux, True))
                #caux = rearrange(caux)
                c += caux
            c = rearrange(c)
        else:
            c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB = True)   # 'conjB=' is necessary!
            c = fftd(c, True)
            c = real(c)
            c = rearrange(c)

        if(x1.ndim==3 and x2.ndim==3):
            d = (np.sum(x1[:,:,0]*x1[:,:,0]) + np.sum(x2[:,:,0]*x2[:,:,0]) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
        elif(x1.ndim==2 and x2.ndim==2):
            d = (np.sum(x1*x1) + np.sum(x2*x2) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])

        d = d * (d>=0)
        d = np.exp(-d / (self.sigma*self.sigma))

        return d
Esempio n. 9
0
    def train_scale(self, image, ini=False):
        xsf = self.get_scale_sample(image)

        # Adjust ysf to the same size as xsf in the first time
        if ini:
            totalSize = xsf.shape[0]
            self.ysf = cv2.repeat(self.ysf, totalSize, 1)

        # Get new GF in the paper (delta A)
        new_sf_num = cv2.mulSpectrums(self.ysf, xsf, 0, conjB=True)

        new_sf_den = cv2.mulSpectrums(xsf, xsf, 0, conjB=True)
        new_sf_den = cv2.reduce(real(new_sf_den), 0, cv2.REDUCE_SUM)

        if ini:
            self.sf_den = new_sf_den
            self.sf_num = new_sf_num
        else:
            # Get new A and new B
            self.sf_den = cv2.addWeighted(self.sf_den, (1 - self.scale_lr),
                                          new_sf_den, self.scale_lr, 0)
            self.sf_num = cv2.addWeighted(self.sf_num, (1 - self.scale_lr),
                                          new_sf_num, self.scale_lr, 0)

        self.update_roi()
Esempio n. 10
0
    def update(self, frame, rate=0.125):
        (x, y), (w, h) = self.pos, self.size
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)
        self.last_resp, (dx, dy), self.psr = self.correlate(img)
        self.good = self.psr > 8.0
        if not self.good:
            self.posHist = []
            return

        self.pos = x + dx, y + dy
        self.posHist.append(self.pos)

        if len(self.posHist) > HIST_SIZE:
            self.posHist.pop(0)

            #std
            an = np.array(self.posHist)
            s = np.std(an, axis=0)
            self.std = s[0] + s[1]

        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate
        self.update_kernel()
Esempio n. 11
0
    def update(self, frame, learning_rate=0.125):
        # get the current's frame template and preprocess it
        image = cv2.getRectSubPix(frame, self.size, self.template_center)
        image = self.preprocess_frame(image)

        (dx, dy), self.psr = self.correlate(image)

        # PSR under 8 means that the object is occluded or tracking has failed
        if self.psr < 8.0:
            return

        # update the templates center according to the new discovered position
        self.template_center = (self.template_center[0] + dx,
                                self.template_center[1] + dy)
        image = cv2.getRectSubPix(frame, self.size, self.template_center)
        image = self.preprocess_frame(image)

        image_dft = cv2.dft(
            image, flags=cv2.DFT_COMPLEX_OUTPUT)  # fourier transformation
        # convolution (mulSpectrums: together with dft and idft, it may be used to calculate convolution
        H1 = cv2.mulSpectrums(self.G, image_dft, 0, conjB=True)
        H2 = cv2.mulSpectrums(image_dft, image_dft, 0,
                              conjB=True)  # convolution

        self.H1 = H1 * learning_rate + self.H1 * (1.0 - learning_rate
                                                  )  # equation 11
        self.H2 = H2 * learning_rate + self.H2 * (1.0 - learning_rate
                                                  )  # equation 12

        self.update_kernel()
Esempio n. 12
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)
Esempio n. 13
0
    def init(self, roi, image, cnnFeature):
        if cnnFeature.ndim == 3:
            self.feature_channel = cnnFeature.shape[2]
        self.confidence = 1
        self._roi = list(map(float, roi))
        self.cnnFeature = cnnFeature
        self._scale2img_x = image.shape[1] / cnnFeature.shape[1]
        self._scale2img_y = image.shape[0] / cnnFeature.shape[0]
        self._roi[0] = self._roi[0] / self._scale2img_x
        self._roi[1] = self._roi[1] / self._scale2img_y
        self._roi[2] = self._roi[2] / self._scale2img_x
        self._roi[3] = self._roi[3] / self._scale2img_y
        self.target_center[0] = self._roi[0] + self._roi[2] / 2
        self.target_center[1] = self._roi[1] + self._roi[3] / 2
        self._x_sz = [self._roi[2], self._roi[3]]
        self._alphaf = []
        self._scale_x_buffer = []  # store scale of w
        self._scale_y_buffer = []  # store scale of h
        self._keyFrame_buffer = []
        self._x, searchingRegion = self.getTargetModel(image)
        self._yf = self.createGaussianPeak(self.size_patch[0], self.size_patch[1])
        # self._yf = self.createRickerPeak(self.size_patch[0], self.size_patch[1])

        if self.feature_channel > 1:
            for c in range(self.feature_channel):
                xf = fftd(self._x[:, :, c])
                kf = cv2.mulSpectrums(xf, xf, 0, conjB=True)  # 'conjB=' is necessary!
                self._alphaf.append(complexDivision(self._yf, kf + self.lambdar))
        else:
            xf = fftd(self._x)
            kf = cv2.mulSpectrums(xf, xf, 0, conjB=True)  # 'conjB=' is necessary!
            self._alphaf.append(complexDivision(self._yf, kf + self.lambdar))

        self.keyFrame = True
        self.scaleUpdate(searchingRegion)
    def update(self, frame, rate=0.125):
        (x, y), (w, h) = self.pos, self.size
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)
        self.last_resp, (dx, dy), self.psr = self.correlate(img)
        self.good = self.psr > 8.0
        if not self.good:
            self.stationary_for_frames = self.stationary_for_frames + 1
            return

        self.pos = x + dx, y + dy
        if abs(dx) + abs(dy) < 1:
            self.stationary_for_frames = self.stationary_for_frames + 1
        else:
            self.stationary_for_frames = 0

        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate
        self.update_kernel()
Esempio n. 15
0
    def update(self, frame, pos=None, rate=0.125):
        # Crop template image from last position
        (x, y), (w, h) = self.pos if pos is None else pos, self.size
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = self.preprocess(img)

        # Correlate, find position of object
        self.last_resp, (dx, dy), self.psr = self.correlate(img)

        # Break if lost tracking (don't update filter)
        self.good = self.psr > 8.0
        if not self.good:
            return

        # Cut out new image based on tracked location
        self.pos = x+dx, y+dy
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Preprocess, get DFT
        img = self.preprocess(img)
        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)

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

        # Get weighted average based on the rate (using the new image)
        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate

        # Update filter
        self.update_kernel()
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect
        self.using_lk = False
        self.frameWidth = frame.shape[1]
        self.frameHeight = frame.shape[0]

        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.org_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)
        #print "init G",self.G.shape
        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),self.size)
            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)

        #print "init imgF:",A.shape
        #print "init H1",self.H1.shape
        #print "init H2",self.H2.shape
        self.update_kernel()
    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)  #
Esempio n. 18
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)
Esempio n. 19
0
    def update(self, frame, rate = 0.125):

        if self.maxcount > self.currentcount:
            (x, y), (w, h) = self.pos, self.size
            self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
            img = self.preprocess(img)
            self.last_resp, (dx, dy), self.psr = self.correlate(img)
            self.good = self.psr > 4.0  #8.0
            self.currentcount = self.currentcount + 1

            if not self.good:
                print("NOTGOOD")
                return False

            self.pos = x+dx, y+dy
            self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
            img = self.preprocess(img)

            A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
            H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
            H2 = cv2.mulSpectrums(     A, A, 0, conjB=True)
            self.H1 = self.H1 * (1.0-rate) + H1 * rate
            self.H2 = self.H2 * (1.0-rate) + H2 * rate
            self.update_kernel()
        else:
            return True
Esempio n. 20
0
    def apply_sample(self, sample):
        # Preprocess, get DFT
        img = self.preprocess(sample)
        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)

        # Add to sum matrices
        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*
Esempio n. 21
0
    def update(self, image, cnnFeature):
        self.cnnFeature = cnnFeature

        cx = self.target_center[0]
        cy = self.target_center[1]

        x, searchingRegion = self.getTargetModel(image)
        if self.confidence == 0:
            return self._roi, cnnFeature

        x = cv2.resize(x, (self._x.shape[1], self._x.shape[0]))

        loc, peak_value = self.detect(x)

        self.target_center[0] = cx + loc[0] * self._scale2keyframe_x
        self.target_center[1] = cy + loc[1] * self._scale2keyframe_y
        self._roi[0] = self.target_center[0] - self._roi[2] / 2.0
        self._roi[1] = self.target_center[1] - self._roi[3] / 2.0

        self.scaleUpdate(searchingRegion)

        self._roi[2] = self._x_sz[0] * self._scale2keyframe_x
        self._roi[3] = self._x_sz[1] * self._scale2keyframe_y
        self._roi[0] = cx - self._roi[2] / 2.0
        self._roi[1] = cy - self._roi[3] / 2.0

        self.roiCheck(cnnFeature.shape[0], cnnFeature.shape[1])

        if self.confidence == 0:
            return [0, 0, 0, 0], searchingRegion

        x, searchingRegion = self.getTargetModel(image)
        x = cv2.resize(x, (self._x.shape[1], self._x.shape[0]))
        # self._yf = self.createGaussianPeak(x.shape[0], x.shape[1])
        # if self.keyFrame:
        #     interp_factor = self.interp_factor
        # else:
        #     interp_factor = 0.2
        interp_factor = self.interp_factor
        if self.feature_channel > 1:
            for c in range(self.feature_channel):
                xf = fftd(x[:, :, c])
                kf = cv2.mulSpectrums(xf, xf, 0, conjB=True)  # 'conjB=' is necessary!
                alphaf = complexDivision(self._yf, kf + self.lambdar)
                self._x[:, :, c] = (1 - interp_factor) * self._x[:, :, c] + interp_factor * x[:, :, c]
                self._alphaf[c] = (1 - interp_factor) * self._alphaf[c] + interp_factor * alphaf
        else:
            xf = fftd(x)
            kf = cv2.mulSpectrums(xf, xf, 0, conjB=True)  # 'conjB=' is necessary!
            alphaf = complexDivision(self._yf, kf + self.lambdar)
            self._x = (1 - interp_factor) * self._x + interp_factor * x
            self._alphaf[0] = (1 - interp_factor) * self._alphaf[0] + interp_factor * alphaf
        # # return tracking result

        target_roi = [self._roi[0] * self._scale2img_x, self._roi[1] * self._scale2img_y, self._roi[2] * self._scale2img_x, self._roi[3] * self._scale2img_y]

        return target_roi, searchingRegion
Esempio n. 22
0
    def gaussianCorrelation(self, x1, x2):
        if self._hogfeatures:
            c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
            for i in range(self.size_patch[2]):
                x1aux = x1[i, :].reshape((self.size_patch[0], self.size_patch[1]))
                x2aux = x2[i, :].reshape((self.size_patch[0], self.size_patch[1]))
                caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB=True)
                #print('x1aux x2aux: ',x1aux.shape, x2aux.shape)
                caux = real(fftd(caux, True))
                # caux = rearrange(caux)
                c += caux
            c = rearrange(c)
        elif self._cnnfeatures: # 3D cnn feat
            if type(x1) == type([]):
                _c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
                c = []
                for i in range(len(self.cnn_multidepth)):
                    c.append(_c)
                    #print('c:',_c.shape,'self.cnn_multidepth[i]:',self.cnn_multidepth[i])
                    for j in range(self.cnn_multidepth[i]):
                        x1aux,x2aux = x1[i][:,:,j],x2[i][:,:,j]
                        caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB=True)
                        caux = real(fftd(caux, True))
                        c[i] += caux
                    c[i] = rearrange(c[i])
            else:
                c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
                for i in range(self.size_patch[2]):
                    x1aux,x2aux = x1[:,:,i],x2[:,:,i]
                    caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB=True)
                    caux = real(fftd(caux, True))
                    c += caux
                c = rearrange(c)
        else:
            # 'conjB=' is necessary!在做乘法之前取第二个输入数组的共轭.
            c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB=True)
            c = fftd(c, True)
            c = real(c)
            c = rearrange(c)
        # print('c: ', c.shape)

        if type(x1) == type([]): # multi-KCF
            k = []
            for i in range(len(self.cnn_multidepth)):
                d = (np.sum(x1[i] * x1[i]) + np.sum(x2[i] * x2[i]) - 2.0 * c[i]) / (
                        self.size_patch[0] * self.size_patch[1] * self.cnn_multidepth[i])
                d = d * (d >= 0)
                d = np.exp(-d / (self.sigma * self.sigma))
                k.append(d)
            return k
        else:
            d = (np.sum(x1 * x1) + np.sum(x2 * x2) - 2.0 * c) / (
                        self.size_patch[0] * self.size_patch[1] * self.size_patch[2])
            d = d * (d >= 0)
            d = np.exp(-d / (self.sigma * self.sigma))
            return d
Esempio n. 23
0
    def adapt(self, frame, pos, rate=0.125):
        (w, h) = self.size
        self.pos = (x,y) = pos
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(     A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate
        self.update_kernel()
Esempio n. 24
0
 def reconstruct_ocv(self, img):
     assert opencv, "No opencv present"
     img2 = np.zeros((2 * self.N, 2 * self.N), dtype=np.single)
     for i in range(3):
         imf = cv2.mulSpectrums(cv2.dft(img[i, :, :]), self._prefilter_ocv, 0)
         self._carray_ocv[0:self.N // 2, 0:self.N] = imf[0:self.N // 2, 0:self.N]
         self._carray_ocv[3 * self.N // 2:2 * self.N, 0:self.N] = imf[self.N // 2:self.N, 0:self.N]
         img2 = cv2.add(img2, cv2.multiply(cv2.idft(self._carray_ocv, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT),
                                           self._reconfactor[i, :, :]))
     self._imgstore = img.copy()
     return cv2.idft(cv2.mulSpectrums(cv2.dft(img2), self._postfilter_ocv, 0),
                     flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
 def reconstructframe_ocv(self, img, i):
     assert opencv, "No opencv present"
     diff = img - self._imgstore[i, :, :]
     imf = cv2.mulSpectrums(cv2.dft(diff), self._prefilter_ocv, 0)
     self._carray_ocv[0:self.N // 2, 0:self.N] = imf[0:self.N // 2, 0:self.N]
     self._carray_ocv[3 * self.N // 2:2 * self.N, 0:self.N] = imf[self.N // 2:self.N, 0:self.N]
     img2 = cv2.multiply(cv2.idft(self._carray_ocv, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT),
                         self._reconfactor[i, :, :])
     self._imgstore[i, :, :] = img
     self._bigimgstore = self._bigimgstore + cv2.idft(cv2.mulSpectrums(cv2.dft(img2), self._postfilter_ocv, 0),
                                                      flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
     return self._bigimgstore
Esempio n. 26
0
    def adapt(self, frame, pos, rate=0.125):
        (w, h) = self.size
        self.pos = (x, y) = pos
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0 - rate) + H1 * rate
        self.H2 = self.H2 * (1.0 - rate) + H2 * rate
        self.update_kernel()
Esempio n. 27
0
 def gaussian_correlation(self, cur_feat, avg_feat):
     if (self._hog_features):
         c = np.zeros((self._size_patch_info[0], self._size_patch_info[1]),
                      np.float32)
         # 对每个维度进行循环检测
         for i in range(self._size_patch_info[2]):
             x1aux = cur_feat[i, :].reshape(
                 (self._size_patch_info[0], self._size_patch_info[1]))
             x2aux = avg_feat[i, :].reshape(
                 (self._size_patch_info[0], self._size_patch_info[1]))
             # BEGIN FFTD
             caux = cv2.mulSpectrums(ffttools.fftd(x1aux),
                                     ffttools.fftd(x2aux),
                                     0,
                                     conjB=True)
             caux = ffttools.real(ffttools.fftd(caux, True))
             # END FFTD
             c += caux
         c = ffttools.rearrange(c)
     else:
         # 'conjB=' is necessary!在做乘法之前取第二个输入数组的共轭.
         # BEGIN FFTD,和上面的代码块做了一样的事情
         c = cv2.mulSpectrums(ffttools.fftd(cur_feat),
                              ffttools.fftd(avg_feat),
                              0,
                              conjB=True)
         # c = ffttools.fftd(c, True)
         c = ffttools.real(ffttools.fftd(c, True))
         # END_FFTD
         c = ffttools.rearrange(c)
     # 这里的作用是验证特征的维度是否相同,不同维度特征不得合到一起
     # 三维特征(HOG等多特征)
     if (cur_feat.ndim == 3 and avg_feat.ndim == 3):
         d = (np.sum(cur_feat[:, :, 0] * cur_feat[:, :, 0]) +
              np.sum(avg_feat[:, :, 0] * avg_feat[:, :, 0]) - 2.0 * c) / (
                  self._size_patch_info[0] * self._size_patch_info[1] *
                  self._size_patch_info[2])
     # 二维特征(CN等单特征)
     elif (cur_feat.ndim == 2 and avg_feat.ndim == 2):
         d = (np.sum(cur_feat * cur_feat) + np.sum(avg_feat * avg_feat) -
              2.0 * c) / (self._size_patch_info[0] *
                          self._size_patch_info[1] *
                          self._size_patch_info[2])
     else:
         raise Exception('Array dim error.', cur_feat.ndim)
     # 核计算公式
     d = d * (d >= 0)
     d = np.exp(-d / (self._sigma * self._sigma))
     # 返回高斯核结果
     return d
    def __init__(self, frame, rect):
        x1, y1, x2, y2 = rect
        self.using_lk = False
        self.Impused = False
        self.tx = 0
        self.ty = 0
        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(str(x1)+','+ str(y1)+','+ str(x2)+','+ str(y2)+'\n')
        org_fout.write(str(x1)+','+ str(y1)+','+ str(x2)+','+ str(y2)+'\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
        self.org_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)
        #print "init G",self.G.shape
        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),self.size)
            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)

        #print "init imgF:",A.shape
        #print "init H1",self.H1.shape
        #print "init H2",self.H2.shape
        self.update_kernel()
Esempio n. 29
0
    def __prepare_convolution_terms(self):
        self.G = cv2.dft(self.g, flags=cv2.DFT_COMPLEX_OUTPUT)
        self.H1 = np.zeros_like(self.G)
        self.H2 = np.zeros_like(self.G)

        a = self.preprocess_frame(self.template)

        # compute the DFT of the image
        A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)

        # get correlation between G and A, without flags
        self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
        # get correlation between A and A, without flags
        self.H2 += cv2.mulSpectrums(A, A, 0, conjB=True)
Esempio n. 30
0
    def init_training_data(self, frame, count=128):
        img = self.get_target_img_from_frame(frame)
        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)

        return self
Esempio n. 31
0
def complexDivide(G, F) :
   
    top = cv.mulSpectrums(G, F, flags=cv.DFT_COMPLEX_OUTPUT, conjB=True) # Top is G*(F conjugate)
    bot = cv.mulSpectrums(F, F, flags=cv.DFT_COMPLEX_OUTPUT, conjB=True) # Bot is F*(F conjugate)
    
    # Bottom is strictly real and we should divide real and complex parts by it
    botRe = [np.float32(bot), np.zeros(bot.shape, np.float32)]
    botRe = cv.split(bot)
    botRe[1] = botRe[0].copy()
    bot = cv.merge(botRe)

    # Do the actual division
    H = np.divide(top, bot)
    
    return H
Esempio n. 32
0
def cv2_convolution(image, b):
    dft_m = cv2.getOptimalDFTSize(image.shape[0] + b.shape[0] - 1)
    dft_n = cv2.getOptimalDFTSize(image.shape[1] + b.shape[1] - 1)
    d = b.shape[0]
    c = np.zeros((image.shape[0] + d - 1, image.shape[1] + d - 1), dtype='uint8')
    # getting gaussian dft
    dft_b = np.zeros((dft_m, dft_n), dtype='float64')
    dft_b[:b.shape[0], :b.shape[1]] = b
    dft_b = cv2.dft(dft_b, flags=cv2.DFT_REAL_OUTPUT)
    # getting layers dft
    dfts = []
    new_channels = []
    channels = cv2.split(image)
    for i, channel in enumerate(channels):
        #cv2.imshow('channel %d'%i, channel)
        a = np.array(channel, dtype='float64')
        dft_a = np.zeros((dft_m, dft_n), dtype='float64')
        dft_a[:a.shape[0], :a.shape[1]] = a
        dft_a = cv2.dft(dft_a, flags=cv2.DFT_REAL_OUTPUT)
        dft_a = cv2.mulSpectrums(dft_a, dft_b, 0)
        dft_a = cv2.idft(dft_a, flags= cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        tmp = dft_a[d/2:a.shape[0] + d/2, d/2:a.shape[1] + d/2]
        channel = np.array(tmp, dtype='uint8')
        #cv2.imshow('new channel %d'%i, channel)
        new_channels.append(channel)
    result = cv2.merge(new_channels)
    return result
def homomorphic():
    global yh, yl, c, d0, complex
    du = np.zeros(complex.shape, dtype=np.float32)
    #H(u, v)
    for u in range(dft_M):
        for v in range(dft_N):
            du[u, v] = sqrt((u - dft_M / 2.0) * (u - dft_M / 2.0) +
                            (v - dft_N / 2.0) * (v - dft_N / 2.0))

    du2 = cv2.multiply(du, du) / (d0 * d0)
    re = np.exp(-c * du2)
    H = (yh - yl) * (1 - re) + yl
    #S(u, v)
    filtered = cv2.mulSpectrums(complex, H, 0)
    #inverse DFT (does the shift back first)
    filtered = np.fft.ifftshift(filtered)
    filtered = cv2.idft(filtered)
    #normalization to be representable
    filtered = cv2.magnitude(filtered[:, :, 0], filtered[:, :, 1])
    cv2.normalize(filtered, filtered, 0, 1, cv2.NORM_MINMAX)
    #g(x, y) = exp(s(x, y))
    filtered = np.exp(filtered)
    cv2.normalize(filtered, filtered, 0, 1, cv2.NORM_MINMAX)

    cv2.namedWindow('homomorphic', cv2.WINDOW_NORMAL)
    cv2.imshow("homomorphic", filtered)
    cv2.resizeWindow("homomorphic", 600, 550)
Esempio n. 34
0
    def deconvolve(self, img):
        """Remove impulse response function from ping

        Copy from https://github.com/pvazteixeira/multibeam
        """
        img = np.float32(img)

        img_f = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        psf_padded = np.zeros_like(img)
        kh, kw = self.psf.shape
        psf_padded[:kh, :kw] = self.psf

        # compute (padded) psf's DFT
        psf_f = cv2.dft(psf_padded, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=kh)

        psf_f_2 = (psf_f ** 2).sum(-1)
        ipsf_f = psf_f / (psf_f_2 + self.noise)[..., np.newaxis]

        result_f = cv2.mulSpectrums(img_f, ipsf_f, 0)
        result = cv2.idft(result_f, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)

        result = np.roll(result, -kh // 2, 0)
        result = np.roll(result, -kw // 2, 1)

        # clip to 0-1 range
        result[result < 0] = 0
        result = (np.max(img) / np.max(result)) * result

        return result.astype(np.float32)
Esempio n. 35
0
def cv2_deconvolution(image, b):
    dft_m = cv2.getOptimalDFTSize(image.shape[0] + b.shape[0] - 1)
    dft_n = cv2.getOptimalDFTSize(image.shape[1] + b.shape[1] - 1)
    c = np.zeros((image.shape[0] + d - 1, image.shape[1] + d - 1), dtype='uint8')
    # getting gaussian dft
    dft_b = np.zeros((dft_m, dft_n), dtype='float64')
    dft_b[:b.shape[0], :b.shape[1]] = b
    psf = cv2.dft(dft_b, flags=cv2.DFT_COMPLEX_OUTPUT)
    psf2 = (psf**2).sum(-1)
    ipsf = psf / (psf2 + 0.7)[..., np.newaxis]
    # getting layers dft
    dfts = []
    new_channels = []
    channels = cv2.split(image)
    for i, channel in enumerate(channels):
        #cv2.imshow('channel %d'%i, channel)
        a = np.array(channel, dtype='float64')
        dft_a = np.zeros((dft_m, dft_n), dtype='float64')
        dft_a[:a.shape[0], :a.shape[1]] = a
        print 'deconv'
        dft_a = cv2.dft(dft_a, flags=cv2.DFT_COMPLEX_OUTPUT)
        dft_a = cv2.mulSpectrums(dft_a, ipsf, 0)
        print dft_a
        dft_a = cv2.idft(dft_a, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        print dft_a
        tmp = dft_a[d/2:a.shape[0] + d/2, d/2:a.shape[1] + d/2]
        channel = np.array(tmp, dtype='uint8')
        cv2.imshow('new channel %d'%i, channel)
        new_channels.append(channel)
    result = cv2.merge(new_channels)
    return result
Esempio n. 36
0
def aplicar_filtro(magn, filtro):
    # Filtro para o domínio da frequência
    fmg, ffs = tf_complexa(filtro)
    # Aplicar
    rmg = cv.mulSpectrums(magn, fmg, cv.DFT_ROWS)
    rmg = np.fft.ifftshift(rmg, axes=None)
    return rmg
Esempio n. 37
0
def cv2_deconvolution(image, b):
    dft_m = cv2.getOptimalDFTSize(image.shape[0] + b.shape[0] - 1)
    dft_n = cv2.getOptimalDFTSize(image.shape[1] + b.shape[1] - 1)
    c = np.zeros((image.shape[0] + d - 1, image.shape[1] + d - 1),
                 dtype='uint8')
    # getting gaussian dft
    dft_b = np.zeros((dft_m, dft_n), dtype='float64')
    dft_b[:b.shape[0], :b.shape[1]] = b
    psf = cv2.dft(dft_b, flags=cv2.DFT_COMPLEX_OUTPUT)
    psf2 = (psf**2).sum(-1)
    ipsf = psf / (psf2 + 0.7)[..., np.newaxis]
    # getting layers dft
    dfts = []
    new_channels = []
    channels = cv2.split(image)
    for i, channel in enumerate(channels):
        #cv2.imshow('channel %d'%i, channel)
        a = np.array(channel, dtype='float64')
        dft_a = np.zeros((dft_m, dft_n), dtype='float64')
        dft_a[:a.shape[0], :a.shape[1]] = a
        print 'deconv'
        dft_a = cv2.dft(dft_a, flags=cv2.DFT_COMPLEX_OUTPUT)
        dft_a = cv2.mulSpectrums(dft_a, ipsf, 0)
        print dft_a
        dft_a = cv2.idft(dft_a, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        print dft_a
        tmp = dft_a[d / 2:a.shape[0] + d / 2, d / 2:a.shape[1] + d / 2]
        channel = np.array(tmp, dtype='uint8')
        cv2.imshow('new channel %d' % i, channel)
        new_channels.append(channel)
    result = cv2.merge(new_channels)
    return result
Esempio n. 38
0
    def update(_):
        ang = np.deg2rad(cv2.getTrackbarPos('angle', win))
        d = cv2.getTrackbarPos('d', win)
        noise = 10**(-0.1 * cv2.getTrackbarPos('SNR (db)', win))

        if defocus:
            pointspread = defocus_kernel(d)
        else:
            pointspread = motion_kernel(ang, d)

        pointspread /= pointspread.sum()
        pointspread_pad = np.zeros_like(img)
        kh, kw = pointspread.shape
        pointspread_pad[:kh, :kw] = pointspread
        PSF = cv2.dft(pointspread_pad,
                      flags=cv2.DFT_COMPLEX_OUTPUT,
                      nonzeroRows=kh)
        PSF2 = (PSF**2).sum(-1)
        iPSF = PSF / (PSF2 + noise)[..., np.newaxis]
        RES = cv2.mulSpectrums(IMG, iPSF, 0)
        result = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        result = np.roll(result, -kh // 2, 0)
        result = np.roll(result, -kw // 2, 1)

        cv2.imwrite('../Generated/bot_blur.jpg', 255 * result)
        cv2.destroyAllWindows()
Esempio n. 39
0
    def _polynomial_correlation(self, x1, x2, a, b):
        xcorr = cv2.mulSpectrums(fft2(x1), fft2(x2), 0, conjB=True)
        xcorr = fftshift(real(ifft2(xcorr)))

        out = (xcorr / x1.size + a) ** b

        return out
Esempio n. 40
0
def cv2_convolution(image, b):
    dft_m = cv2.getOptimalDFTSize(image.shape[0] + b.shape[0] - 1)
    dft_n = cv2.getOptimalDFTSize(image.shape[1] + b.shape[1] - 1)
    d = b.shape[0]
    c = np.zeros((image.shape[0] + d - 1, image.shape[1] + d - 1),
                 dtype='uint8')
    # getting gaussian dft
    dft_b = np.zeros((dft_m, dft_n), dtype='float64')
    dft_b[:b.shape[0], :b.shape[1]] = b
    dft_b = cv2.dft(dft_b, flags=cv2.DFT_REAL_OUTPUT)
    # getting layers dft
    dfts = []
    new_channels = []
    channels = cv2.split(image)
    for i, channel in enumerate(channels):
        #cv2.imshow('channel %d'%i, channel)
        a = np.array(channel, dtype='float64')
        dft_a = np.zeros((dft_m, dft_n), dtype='float64')
        dft_a[:a.shape[0], :a.shape[1]] = a
        dft_a = cv2.dft(dft_a, flags=cv2.DFT_REAL_OUTPUT)
        dft_a = cv2.mulSpectrums(dft_a, dft_b, 0)
        dft_a = cv2.idft(dft_a, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        tmp = dft_a[d / 2:a.shape[0] + d / 2, d / 2:a.shape[1] + d / 2]
        channel = np.array(tmp, dtype='uint8')
        #cv2.imshow('new channel %d'%i, channel)
        new_channels.append(channel)
    result = cv2.merge(new_channels)
    return result
    def apply_channel_deconvolution (self, img, psfsize=10, snrVal=8):    # Based on deconvolution.py in python samples of opencv
        
        img = img.astype('double')/255.0
        img = self.blur_edge(img)
        IMG = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
    
        if (psfsize==0): return img
        
        defocus = True
    
        ang = 0
        d = psfsize
        snr = snrVal
        noise = 10**(-0.1*snr)

        if defocus:
            psf = self.defocus_kernel(d)
        else:
            psf = self.motion_kernel(ang, d)

        psf /= psf.sum()
        psf_pad = np.zeros_like(img)
        kh, kw = psf.shape
        psf_pad[:kh, :kw] = psf
        PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows = kh)
        PSF2 = (PSF**2).sum(-1)
        iPSF = PSF / (PSF2 + noise)[...,np.newaxis]
        RES = cv2.mulSpectrums(IMG, iPSF, 0)
        res = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
        res = np.roll(res, -kh//2, 0)
        res = np.roll(res, -kw//2, 1)

        return res
Esempio n. 42
0
def fft2Conv(I, kernel, borderType=cv2.BORDER_DEFAULT):
    #图像矩阵的高、宽
    R, C = I.shape[:2]
    #卷积核的高、宽
    r, c = kernel.shape[:2]
    #卷积核的半径
    tb = (r - 1) / 2
    lr = (c - 1) / 2
    #第一步:扩充边界
    I_padded = cv2.copyMakeBorder(I, tb, tb, lr, lr, borderType)
    #第二步:对 I_padded 和 kernel 右侧和下侧补零
    #满足二维快速傅里叶变换的行数、列数
    rows = cv2.getOptimalDFTSize(I_padded.shape[0] + r - 1)
    cols = cv2.getOptimalDFTSize(I_padded.shape[1] + c - 1)
    #补零
    I_padded_zeros = np.zeros((rows, cols), np.float64)
    I_padded_zeros[:I_padded.shape[0], :I_padded.shape[1]] = I_padded
    kernel_zeros = np.zeros((rows, cols), np.float64)
    kernel_zeros[:kernel.shape[0], :kernel.shape[1]] = kernel
    #第三步:快速傅里叶变换
    fft2_Ipz = np.zeros((rows, cols, 2), np.float64)
    cv2.dft(I_padded_zeros, fft2_Ipz, cv2.DFT_COMPLEX_OUTPUT)
    fft2_kz = np.zeros((rows, cols, 2), np.float64)
    cv2.dft(kernel_zeros, fft2_kz, cv2.DFT_COMPLEX_OUTPUT)
    #第四步:两个快速傅里叶变换点乘
    Ipz_rz = cv2.mulSpectrums(fft2_Ipz, fft2_kz, cv2.DFT_ROWS)
    #第五步:傅里叶逆变换,并只取实部
    ifft2FullConv = np.zeros((rows, cols), np.float64)
    cv2.dft(Ipz_rz, ifft2FullConv,
            cv2.DFT_REAL_OUTPUT + cv2.DFT_INVERSE + cv2.DFT_SCALE)
    print np.max(ifft2FullConv)
    #第六步:裁剪,同输入的图像矩阵尺寸一样
    sameConv = np.copy(ifft2FullConv[r - 1:R + r - 1, c - 1:C + c - 1])
    return sameConv
Esempio n. 43
0
 def correlate(self, img):
     C = cv2.mulSpectrums(cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)
     resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
     h, w = resp.shape
     _, mval, _, (mx, my) = cv2.minMaxLoc(resp)
     side_resp = resp.copy()
     cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)
     smean, sstd = side_resp.mean(), side_resp.std()
     psr = (mval-smean) / (sstd+eps)
     return resp, (mx-w//2, my-h//2), psr
Esempio n. 44
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)
Esempio n. 45
0
def filterImg(img,filtro_magnitud):
    """Filtro para imágenes de un canal"""
    
    
    #como la fase del filtro es 0 la conversión de polar a cartesiano es directa (magnitud->x, fase->y)
    filtro=np.array([filtro_magnitud,np.zeros(filtro_magnitud.shape)]).swapaxes(0,2).swapaxes(0,1)
    imgf=cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)
   
    imgf=cv.mulSpectrums(imgf, np.float32(filtro), cv.DFT_ROWS)
    
    return cv.idft(imgf, flags=cv.DFT_REAL_OUTPUT | cv.DFT_SCALE)
Esempio n. 46
0
 def correlate(self, img):
     C = cv2.mulSpectrums(cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)
     resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
     h, w = resp.shape
     _, mval, _, (mx, my) = cv2.minMaxLoc(resp)
     fmx = vertex(mx, *resp[my,mx-1:mx+2]) if (1 <= mx <= w-1) else mx
     fmy = vertex(my, *resp[my-1:my+2,mx]) if (1 <= my <= h-1) else my
     #print (mx - w//2, my - h//2), (fmx - w//2, fmy - h//2)
     side_resp = resp.copy()
     cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)
     smean, sstd = side_resp.mean(), side_resp.std()
     psr = (mval-smean) / (sstd+eps)
     return resp, (fmx-w//2, fmy-h//2), psr
Esempio n. 47
0
    def get_shift(self, imgA, imgB):
        rv = np.array([0.0, 0.0])
        if (imgA is not None) and (imgB is not None) and (imgA.shape==imgB.shape):
            # Phase correlation.
            A  = cv2.dft(imgA)
            B  = cv2.dft(imgB)
            AB = cv2.mulSpectrums(A, B, flags=0, conjB=True)
            normAB = cv2.norm(AB)
            if (normAB != 0.0):
                crosspower = AB / normAB
                shift = cv2.idft(crosspower)
                shift0  = np.roll(shift,  int(shift.shape[0]/2), 0)
                shift00 = np.roll(shift0, int(shift.shape[1]/2), 1) # Roll the matrix so 0,0 goes to the center of the image.
                
                # Get the coordinates of the maximum shift.
                kShift = np.argmax(shift00)
                (iShift,jShift) = np.unravel_index(kShift, shift00.shape)
    
                # Get weighted centroid of a region around the peak, for sub-pixel accuracy.
                w = 7
                r = int((w-1)/2)
                i0 = clip(iShift-r, 0, shift00.shape[0]-1)
                i1 = clip(iShift+r, 0, shift00.shape[0]-1)+1
                j0 = clip(jShift-r, 0, shift00.shape[1]-1)
                j1 = clip(jShift+r, 0, shift00.shape[1]-1)+1
                peak = shift00[i0:i1].T[j0:j1].T
                moments = cv2.moments(peak, binaryImage=False)
                           
                if (moments['m00'] != 0.0):
                    iShiftSubpixel = moments['m01']/moments['m00'] + float(i0)
                    jShiftSubpixel = moments['m10']/moments['m00'] + float(j0)
                else:
                    iShiftSubpixel = float(shift.shape[0])/2.0
                    jShiftSubpixel = float(shift.shape[1])/2.0
                
                # Accomodate the matrix roll we did above.
                iShiftSubpixel -= float(shift.shape[0])/2.0
                jShiftSubpixel -= float(shift.shape[1])/2.0
    
                # Convert unsigned shifts to signed shifts. 
                height = float(shift00.shape[0])
                width  = float(shift00.shape[1])
                iShiftSubpixel  = ((iShiftSubpixel+height/2.0) % height) - height/2.0
                jShiftSubpixel  = ((jShiftSubpixel+width/2.0) % width) - width/2.0
                
                rv = np.array([iShiftSubpixel, jShiftSubpixel])

            
        return rv
Esempio n. 48
0
def apply_filter(img, ftype, flen, ksize=-1, ori=0):
    """
        Apply a particular deblur filter.

        :param str ftype:
            Filter type.

        :param int ksize:
            Kernel size.

        :param int flen:
            Filter length (must be strictly smaller than the kernel size).

        :param int ori:
            Optional kernel orientation. Unused for circular kernels.
    """

    if ksize < 0:
        ksize = flen

    assert ksize >= flen, "kernel size must not be less than filter length"

    img = np.array(img, dtype=np.float32)

    # frequency domain representation of the image
    img = blur_edge(img, flen)
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    IMG = cv2.dft(img_gray, flags=cv2.DFT_COMPLEX_OUTPUT)
    print(IMG)

    # make the PSF for deblurring
    if ftype == "linear":
        psf = makeLinearKernel(ksize=ksize, flen=flen, angle=ori)
    else:
        psf = makeCircularKernel(ksize=ksize, flen=flen)

    # perform the deconvolution
    psf_pad = np.zeros_like(img_gray)
    kh, kw = psf.shape
    psf_pad[:kh, :kw] = psf
    PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=kh)
    PSF2 = (PSF**2).sum(-1)
    iPSF = PSF / (PSF2 + 10**5)[...,np.newaxis]
    RES = cv2.mulSpectrums(IMG, iPSF, 0)
    res = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
    res = np.roll(res, -kh//2, 0)
    res = np.roll(res, -kw//2, 1)
    def correlate(self, img,size):
        #print "new size in correlate",size
        self.H = self.resizeFFT(self.H,size)

##        H1 = cv2.resize(self.H[...,0],None,fx=lk_ratio[0], fy=lk_ratio[1], interpolation = cv2.INTER_CUBIC)
##        H2 = cv2.resize(self.H[...,1],None,fx=lk_ratio[0], fy=lk_ratio[1], interpolation = cv2.INTER_CUBIC)
##        self.H = np.dstack([H1,H2]).copy()
        FFT_img = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        #print "imgF:",FFT_img.shape,"filterF:",self.H.shape
        C = cv2.mulSpectrums(FFT_img, self.H, 0, conjB=True)
        resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        h, w = resp.shape
        #print "resp shape:",resp.shape
        _, mval, _, (mx, my) = cv2.minMaxLoc(resp)
        side_resp = resp.copy()
        cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)
        smean, sstd = side_resp.mean(), side_resp.std()
        psr = (mval-smean) / (sstd+eps)
        return resp, (mx-w//2, my-h//2), psr
Esempio n. 50
0
    def correlate(self, img):
        # Multiply the image with the filter (in frequency space)
        C = cv2.mulSpectrums(cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)

        # Convert back to RGB space
        resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)

        # Look for peak
        h, w = resp.shape
        _, mval, _, (mx, my) = cv2.minMaxLoc(resp)

        # Get the isolated peak response (should be a Gaussian point)
        side_resp = resp.copy()
        cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)

        # Calculate the match probability
        smean, sstd = side_resp.mean(), side_resp.std()
        psr = (mval-smean) / (sstd+eps)

        return resp, (mx-w//2, my-h//2), psr
Esempio n. 51
0
    def update(_):
        ang = np.deg2rad( cv2.getTrackbarPos('angle', win) )
        d = cv2.getTrackbarPos('d', win)
        noise = 10**(-0.1*cv2.getTrackbarPos('SNR (db)', win))

        if defocus:
            psf = defocus_kernel(d)
        else:
            psf = motion_kernel(ang, d)
        cv2.imshow('psf', psf)

        psf /= psf.sum()
        psf_pad = np.zeros_like(img)
        kh, kw = psf.shape
        psf_pad[:kh, :kw] = psf
        PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows = kh)
        PSF2 = (PSF**2).sum(-1)
        iPSF = PSF / (PSF2 + noise)[...,np.newaxis]
        RES = cv2.mulSpectrums(IMG, iPSF, 0)
        res = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
        res = np.roll(res, -kh//2, 0)
        res = np.roll(res, -kw//2, 1)
        cv2.imshow(win, res)
Esempio n. 52
0
    def externalCall(self):
        image = self.inputImageName.data


        imagef = np.float32(image)/255.0
        d = 60
        noise = 10**(-0.1*5)
        psf = self.defocus_kernel(d, 100)
        cv2.imshow('psf', psf)


        #cv2.imshow('spektralblur',spektralblur)

        bluredge = self.blur_edge(imagef)
        se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
        bluredge = cv2.erode(bluredge, se)



        dftimg = cv2.dft(bluredge, flags=cv2.DFT_COMPLEX_OUTPUT)

        #----

        psf /= psf.sum()
        psf_pad = np.zeros_like(bluredge)
        kh, kw = psf.shape
        psf_pad[:kh, :kw] = psf
        PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=kh)
        PSF2 = (PSF**2).sum(-1)
        iPSF = PSF / (PSF2 + noise)[..., np.newaxis]
        RES = cv2.mulSpectrums(dftimg, iPSF, 0)
        deconvolve = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
        deconvolve = np.roll(deconvolve, -kh//2, 0)
        deconvolve = np.roll(deconvolve, -kw//2, 1)

        self.outputImageName.data = deconvolve
    def update(self, frame, lk_info):
        #print "update entered"
        lk_centre,lk_ratio = lk_info

        (x, y), (w, h) = self.pos, self.size
        ratio = sum(lk_ratio)/2.0

        w = int(w*ratio)
        h = int(h*ratio)

        if w>max_ratio*self.org_size[0]:
            w=int(max_ratio*self.org_size[0])

        if h>max_ratio*self.org_size[1]:
            h=int(max_ratio*self.org_size[1])

        if w<min_ratio*self.org_size[0]:
            w=int(min_ratio*self.org_size[0])

        if h<min_ratio*self.org_size[1]:
            h=int(min_ratio*self.org_size[1])

        self.size = (w,h)
        self.last_img = img = cv2.getRectSubPix(frame, self.size, (x, y))
        frameVis = frame.copy()
        img = self.preprocess(img,self.size)
        self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
        self.good = self.psr > 8.0

        ######## particle filter implementation #########################
        self.PF_number = int(max_particles*np.arctan(3-self.psr/4)/np.pi+max_particles/2+min_particles)
        self.f = np.array([])

        if(self.PF_number > self.prev_PF_count):
            new_pts = np.ones((self.PF_number-self.prev_PF_count, 2), int) * self.pos
            self.pt = np.vstack([self.pt,new_pts])

        elif(self.PF_number < self.prev_PF_count):
            temp_pts = self.pt.tolist()
            n_pop = self.prev_PF_count - self.PF_number
            for i in range(n_pop):
                temp_pts.pop()
            self.pt = np.asarray(temp_pts)
        else:
            pass

        self.pt += np.random.uniform(-stepsize_PF, stepsize_PF, self.pt.shape)
        self.pt  = self.pt.clip(np.zeros(2), np.array(frame.shape)-1).astype(int)

        PF_good = sum(self.pt.std(axis = 0)<PF_std)>0
        #print PF_good
        #print self.pt.std(axis = 0)

        for i,point in enumerate(self.pt):
            co_xy = tuple(point)
            self.last_img_PF = img_PF = cv2.getRectSubPix(frame,self.size, co_xy)
            img_PF = self.preprocess(img_PF,self.size)
            last_resp_PF, (dx_PF, dy_PF), PF_PSR = self.correlate(img_PF,self.size)
            good_PF = PF_PSR > 8.0
            #self.f[i] = PF_PSR
            self.f = np.append(self.f,PF_PSR)
            cv2.circle(frameVis, co_xy, 1, 255, -1)

        self.f0 = np.ones(self.PF_number)*psr_PF
        ## atan(x-8)/pi+1/2
        ##weights  = 1./(1. + (self.f0-self.f)**2)
        weights  = (np.arctan(wt_param*(self.f-self.f0))/np.pi)+0.5
        weights /= sum(weights)
        new_co_xy = np.sum(self.pt.T*weights, axis=1)
        new_co_xy = tuple(new_co_xy.astype(np.int))

        cv2.circle(frameVis,new_co_xy,3,255,-1)
        cv2.imshow('test',frameVis)

        if 1./sum(weights**2) < n_PF/2.:
            self.pt  = self.pt[resample(weights),:]

        self.prev_PF_count = self.PF_number

        bbc.lk_ready = True
        bbc.restartingLK = True

        if not self.good:
            if PF_good and PF_ON:
                print "using PF"
                self.pos = new_co_xy
            else:
                return
        else:
            self.pos = x+dx, y+dy

##        if not self.good:
##            self.pos = lk_centre
##            self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
##            img = self.preprocess(img,self.size)
##            self.last_resp, (dxxx, dyyy), self.psr = self.correlate(img,self.size)
##            self.good = self.psr > 8.0
##
##            if not self.good:
##                return
##            else:
##                self.using_lk = True
##
##        if not self.using_lk:
##            self.pos = (x+dx, y+dy)

        self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
        img = self.preprocess(img,self.size)
        #print "img shape:",img.shape

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        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)


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

        self.H1 = self.resizeFFT(self.H1,self.size)
        self.H2 = self.resizeFFT(self.H2,self.size)

        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate
        self.update_kernel()
Esempio n. 54
0
def shc_opencv(d0,d1):

    '''
    Read DocString for SHC
    '''
    
    d0 = np.float32(d0 - d0.mean())
    d1 = np.float32(d1 - d1.mean())
    
    if d0.shape != d1.shape:
        raise ValueError("Input shapes do not match")

    if d0.ndim > 2:
        raise ValueError("Only 1-d or 2-d data supported")
        
    ## one dimensional case 
    if d0.ndim == 1:
        ## Manually Pad Arrays for DFT Optimization
        rows = (d0.shape)[0]
        nrows = cv2.getOptimalDFTSize(rows)
        d0n = np.zeros((nrows))
        d1n = np.zeros((nrows))
        d0n[:rows] = d0
        d1n[:rows] = d1
        ## Fourier Phase Correlation
        d0_dft = cv2.dft(d0, flags = cv2.DFT_COMPLEX_OUTPUT)
        d1_dft = cv2.dft(d1, flags = cv2.DFT_COMPLEX_OUTPUT)
        d01 = cv2.mulSpectrums(d0_dft,d1_dft,flags = 0, conjB = True)
        id01 = cv2.idft(d01, flags = cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        cc = np.fft.fftshift(np.abs(id01))
        xmax = cc.argmax()
        ## Polyfit of degree 2 for three points, extremum
        c1 = (cc[xmax+1]-cc[xmax-1])/2.
        c2 = cc[xmax+1]-c1-cc[xmax]
        xmax = xmax - c1/c2/2.
        return  xmax - d0.shape[0]/2
        
    ## two dimensional case
    if d0.ndim == 2:    
        ## Manually Pad Arrays for DFT Optimization
        rows, cols = d0.shape
        nrows = cv2.getOptimalDFTSize(rows)
        ncols = cv2.getOptimalDFTSize(cols)
        d0n = np.zeros((nrows,ncols))
        d1n = np.zeros((nrows,ncols))
        d0n[:rows,:cols] = d0
        d1n[:rows,:cols] = d1
        ## Fourier Phase Correlation
        d0_dft = cv2.dft(np.float32(d0n), flags = cv2.DFT_COMPLEX_OUTPUT)
        d1_dft = cv2.dft(np.float32(d1n), flags = cv2.DFT_COMPLEX_OUTPUT)
        d01 = cv2.mulSpectrums(d0_dft,d1_dft,flags = 0, conjB = True)
        id01 = cv2.idft(d01, flags = cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        cc = np.fft.fftshift(np.abs(id01))
        indices = np.where(cc == cc.max())
        rmax = (indices[0])[0]
        cmax = (indices[1])[0]
        ## Interpolate to sub-pixel accuracy 
        if (rmax*cmax >= 0) and (rmax < d0n.shape[0]) and (cmax < d0n.shape[1]):
            ## interpolate to sub-pixel accuracy
            ## we use a quadratic estimator as in Tian and Huhns (1986) pg 222
            denom = 2.*(2.*cc.max() - cc[rmax+1,cmax] - cc[rmax-1, cmax])
            rfra = (rmax) + (cc[rmax+1,cmax] -cc[rmax-1,cmax])/denom
            denom = 2.*(2.*cc.max() - cc[rmax,cmax+1] - cc[rmax, cmax-1])
            cfra = (cmax) + (cc[rmax,cmax+1] -cc[rmax,cmax-1])/denom
            rmax = rfra
            cmax = cfra
        return np.array([rmax - d0n.shape[0]/2. , cmax - d0n.shape[1]/2.])
Esempio n. 55
0
def convolveWithDFT(a, b, verbose=False):
  '''
  Returns a convolved with b. Uses DFT and Convolution Theorem.
  
  :type a: numpy.ndarray
  :type b: numpy.ndarray
  :rtype: numpy.ndarray
  '''
  
  if verbose:
    pbar = progressbar.ProgressBar(widgets=[progressbar.Percentage(), progressbar.Bar()], maxval=4).start() 
 
  # Based on: docs.opencv.org/modules/core/doc/operations_on_arrays.html#dft
  
  # Create result and work out correct size.
  a_rows, a_cols = a.shape
  b_rows, b_cols = b.shape 
  result = np.empty((abs(a_rows-b_rows)+1, abs(a_cols-b_cols)+1))
  
  # Calculate size for DFT
  dft_width = cv2.getOptimalDFTSize((a_cols-b_cols)+1)
  dft_height = cv2.getOptimalDFTSize((a_rows-b_rows)+1)
  
  # Tranform a and b to fourier domain.
  # Complexity: O(n**2 * log2(n)
  
  # Create temporary buffers.
  temp_a = np.zeros((dft_height, dft_width))
  temp_b = np.zeros((dft_height, dft_width))
  
  # Copy a and b to top left conerns of temporary buffers.
  temp_a[0:a_cols, 0:a_rows] = a
  temp_b[0:b_cols, 0:b_rows] = b
  
  # Transform padded a&b in place.
  # Use nonzeroRows hint for faster processing.
  cv2.dft(temp_a, temp_a, 0, a_rows)
  if verbose:
    pbar.update(1)
  cv2.dft(temp_b, temp_b, 0, b_rows)
  if verbose:
    pbar.update(2)
  
  # By convolution theorem multiplication in fourier domain is equivalent to convolution
  # in original domain.
  # Perform per element multiplication.
  # Complexity: O(n**2)
  temp_a = cv2.mulSpectrums(temp_a, temp_b, False)
  if verbose:
    pbar.update(3)
  
  # Transform result from fourier domain to original domain.
  # Only need first abs(image_rows-kernel_rows)+1 rows of result.
  # Complexity: O(n**2 * log2(n)
  cv2.dft(temp_a, temp_a, cv2.DFT_INVERSE + cv2.DFT_SCALE, result.shape[1])

  result = temp_a[0:result.shape[0], 0:result.shape[1]]
  
  if verbose:
    pbar.finish()
  
  return result
    def update(self, frame, rate = 0.125):
        (x, y), (w, h) = self.pos, self.size
        self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
        img = self.preprocess(img)
        frameVis = frame.copy()
        self.last_resp, (dx, dy), self.psr = self.correlate(img)
        self.good = self.psr > 8.0

        ######## particle filter implementation #########################
        self.PF_number = int(50*np.arctan(3-self.psr/4)/np.pi+30)
        self.f = np.array([])

        if(self.PF_number > self.prev_PF_count):
            new_pts = np.ones((self.PF_number-self.prev_PF_count, 2), int) * self.pos
            self.pt = np.vstack([self.pt,new_pts])

        elif(self.PF_number < self.prev_PF_count):
            temp_pts = self.pt.tolist()
            n_pop = self.prev_PF_count - self.PF_number
            for i in range(n_pop):
                temp_pts.pop()
            self.pt = np.asarray(temp_pts)
        else:
            pass

        self.pt += np.random.uniform(-stepsize_PF, stepsize_PF, self.pt.shape)
        self.pt  = self.pt.clip(np.zeros(2), np.array(frame.shape)-1).astype(int)

        PF_good = sum(self.pt.std(axis = 0)<PF_std)>0
        #print PF_good
        #print self.pt.std(axis = 0)

        for i,point in enumerate(self.pt):
            co_xy = tuple(point)
            self.last_img_PF = img_PF = cv2.getRectSubPix(frame, (w, h), co_xy)
            img_PF = self.preprocess(img_PF)
            last_resp_PF, (dx_PF, dy_PF), PF_PSR = self.correlate(img_PF)
            good_PF = PF_PSR > 8.0
            #self.f[i] = PF_PSR
            self.f = np.append(self.f,PF_PSR)
            cv2.circle(frameVis, co_xy, 1, 255, -1)
## atan(x-8)/pi+1/2
        #weights  = 1./(1. + (self.f0-self.f)**2)
        self.f0 = np.ones(self.PF_number)*psr_PF
        #print self.f.shape,self.f0.shape

        weights  = (np.arctan(wt_param*(self.f-self.f0))/np.pi)+0.5
        weights /= sum(weights)
        new_co_xy = np.sum(self.pt.T*weights, axis=1)
        new_co_xy = tuple(new_co_xy.astype(np.int))

        cv2.circle(frameVis,new_co_xy,3,255,-1)
        cv2.imshow('test',frameVis)

        if 1./sum(weights**2) < n_PF/2.:
            self.pt  = self.pt[resample(weights),:]

        self.prev_PF_count = self.PF_number

        if not self.good:
            if PF_good and PF_ON:
                print "using PF"
                self.pos = new_co_xy
            else:
                return
        else:
            self.pos = x+dx, y+dy

        self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
        img = self.preprocess(img)

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
        H2 = cv2.mulSpectrums(     A, A, 0, conjB=True)
        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate
        self.update_kernel()
Esempio n. 57
0
'''
fn = '../deblurring/2.png'
ang=0
d=27.3
noise=0.0001

#Calculation
ang=np.deg2rad(ang)
img = cv2.imread(fn, 0)
img = np.float32(img)/255.0
cv2.imshow('input', img)
IMG = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
psf = motion_kernel(ang, d)
#cv2.imshow('psf', psf)
psf /= psf.sum()
psf_pad = np.zeros_like(img)
kh, kw = psf.shape
psf_pad[:kh, :kw] = psf
PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows = kh)
PSF2 = (PSF**2).sum(-1)
iPSF = PSF / (PSF2 + noise)[...,np.newaxis]
RES = cv2.mulSpectrums(IMG, iPSF, 0)
res = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
res = np.roll(res, -kh//2, 0)
res = np.roll(res, -kw//2, 1)

#Result
cv2.imshow('Output',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
Esempio n. 58
0
def convolute(spec1, spec2):
    flags = cv2.DFT_COMPLEX_OUTPUT | cv2.DFT_SCALE
    return cv2.mulSpectrums(spec1, spec2, flags=flags)
    def update(self, frame, lk_info):
        #print "update entered"
        lk_centre, lk_wh, lk_angle,lk_ratio = lk_info

        (x, y), (w, h) = self.pos, self.size
##        (x, y), (w, h) = lk_centre, self.size

        w = int(w*lk_ratio[0])
        h = int(h*lk_ratio[1])

        if w>max_ratio*self.org_size[0]:
            w=int(max_ratio*self.org_size[0])

        if h>max_ratio*self.org_size[1]:
            h=int(max_ratio*self.org_size[1])

        if w<min_ratio*self.org_size[0]:
            w=int(min_ratio*self.org_size[0])

        if h<0.5*self.org_size[1]:
            h=int(0.5*self.org_size[1])

        self.size = (w,h)
        self.last_img = img = cv2.getRectSubPix(frame, self.size, (x, y))
        frameVis = frame.copy()
        cv2.rectangle(frameVis, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), 255)

        img = self.preprocess(img,self.size)

        self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
        self.good = self.psr > PSR_mosse

        if not self.good:
##            theta = np.arctan2(dy,dx)
##            for i in range(-5,6):
##                self.tx,self.ty = (2.0*x+dx)/2.0 + r*np.cos(theta+i*delta),(2.0*y+dy)/2.0 + r*np.sin(theta+i*delta)
##
##                cv2.rectangle(frameVis, (int(self.tx-w/2),int(self.ty-h/2)), (int(self.tx+w/2),int(self.ty+h/2)), 255)
##                cv2.circle(frameVis, (int(self.tx), int(self.ty)), 2, 255, -1)
##
##                self.last_img = img = cv2.getRectSubPix(frame, self.size, (self.tx, self.ty))
##                img = self.preprocess(img,self.size)
##                self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
##                self.good = self.psr>PSR_local
##                cv2.imshow('test',frameVis)
##                if self.good:
##                    self.ImpUsed = True
##                    print 'i :',i
##                    break
            if not self.ImpUsed:
                self.pos = lk_centre
                self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
                img = self.preprocess(img,self.size)
                self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
                self.good = self.psr > PSR_lk
                if not self.good:
                    return
                else:
                    self.using_lk = True

        if self.ImpUsed:
            self.pos = self.tx,self.ty
            self.ImpUsed = False

        if not self.using_lk:
            self.pos = x+dx, y+dy
        self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
        img = self.preprocess(img,self.size)
        #print "img shape:",img.shape

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        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)


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

        self.H1 = self.resizeFFT(self.H1,self.size)
        self.H2 = self.resizeFFT(self.H2,self.size)

        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate
        self.update_kernel()
    def update(self, frame, lk_info, rate = 0.125):
        #print "update entered"
        lk_centre, lk_wh, lk_angle,lk_ratio = lk_info

        (x, y), (w, h) = self.pos, self.size

        w = int(w*lk_ratio[0])
        h = int(h*lk_ratio[1])

        if w>2*self.org_size[0]:
            w=int(2*self.org_size[0])

        if h>2*self.org_size[1]:
            h=int(2*self.org_size[1])

        if w<0.5*self.org_size[0]:
            w=int(0.5*self.org_size[0])

        if h<0.5*self.org_size[1]:
            h=int(0.5*self.org_size[1])



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

        img = self.preprocess(img,self.size)

        self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
        self.good = self.psr > 9.0
        if not self.good:
            self.pos = lk_centre

            self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
            img = self.preprocess(img,self.size)
            self.last_resp, (dx, dy), self.psr = self.correlate(img,self.size)
            self.good = self.psr > 8.0
            if not self.good:
                return
            else:
                print "using lk"
                self.using_lk = True

        if not self.using_lk:
            self.pos = x+dx, y+dy
        self.last_img = img = cv2.getRectSubPix(frame, self.size, self.pos)
        img = self.preprocess(img,self.size)
        #print "img shape:",img.shape

        A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
        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)


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

        self.H1 = self.resizeFFT(self.H1,self.size)
        self.H2 = self.resizeFFT(self.H2,self.size)

        self.H1 = self.H1 * (1.0-rate) + H1 * rate
        self.H2 = self.H2 * (1.0-rate) + H2 * rate
        self.update_kernel()