コード例 #1
1
ファイル: QoE_algorithm.py プロジェクト: buptbubble/qoe
def get_filtered_CSF_img(img_in):
    img_dft = cv2.dft(np.float32(img_in), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(img_dft)
    height = img_dft.shape[0]
    weight = img_dft.shape[1]
    M = weight / 2
    N = height / 2
    H_matrix = np.zeros((height, weight))

    for h_idx in range(height):
        for w_idx in range(weight):
            m = -M + w_idx + 0.5
            n = -N + h_idx + 0.5
            freq, theta = get_freq_dirc(m, n, weight, height)
            multiVal = freq_trans_func(freq, theta)
            H_matrix[h_idx][w_idx] = multiVal

    img_magi = cv2.magnitude(img_dft[:, :, 0], img_dft[:, :, 1])
    img_magi *= H_matrix
    img_phase = cv2.phase(img_dft[:, :, 0], img_dft[:, :, 1])

    img_re = img_magi * np.cos(img_phase)
    img_im = img_magi * (np.sin(img_phase))

    img_dft2 = np.dstack((img_re, img_im))

    imgback = cv2.idft(img_dft2)
    imgback = cv2.magnitude(imgback[:, :, 0], imgback[:, :, 1])

    return imgback
コード例 #2
0
ファイル: motion.py プロジェクト: pi2grupo3/sav
    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
ファイル: filters.py プロジェクト: sergey-lebedev/set
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
コード例 #4
0
def focousing_degree_map(src,n=8):
	try:
		src = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
		src = src.astype(float)
		focousing_degrees = np.zeros((len(src),len(src[0])))
		x,y,var,sumF = 0,0,0,0
		while x+n <= len(src[0]):
			while y+n <= len(src):
				block = src[y:y+n,x:x+n]
				cv2.dft(block)
				for j in range(n):
					for i in range(n):
						F = abs(block[j,i])
						sumF += F
						W = np.exp(-((n-j)*(n-j)+(n-i)*(n-i))/(0.25*n*n))
						var += F*W
				f = var/(sumF)
				for v in range(n):
					for u in range(n):
						focousing_degrees[y+v,x+u] = f
				f,var,sumF = 0,0,0
				y+=n
			x+=n
			y=0
		out_dict = {"name": "focousing_degree_map", "value" : focousing_degrees}
	except:
		out_dict = {}
		raise
	return out_dict
コード例 #5
0
    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()
コード例 #6
0
ファイル: mosse.py プロジェクト: mattalexpugh/mphil-neptune
    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 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
コード例 #8
0
ファイル: filters.py プロジェクト: sergey-lebedev/set
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
コード例 #9
0
    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()
コード例 #10
0
ファイル: imageprocessing.py プロジェクト: ptweir/Kinefly
    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
コード例 #11
0
def saliency_feature(img):
    img_orig = img
    img = cv2.resize(img, (64, 64))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

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

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

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

    return S
コード例 #12
0
ファイル: pcd.py プロジェクト: azmihafizha/imgprocessing
def fft(img,x,y,w,h):
	rows, cols = img.shape
	crow,ccol = rows/2 , cols/2
	dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
	dft_shift = np.fft.fftshift(dft)
	magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
	mask = np.zeros((rows,cols,2),np.uint8)
	mask[crow-x:crow+w, ccol-y:ccol+h] = 1

	mag_mask = copy.copy(magnitude_spectrum)
	mag_mask[crow-x:crow+w, ccol-y:ccol+h] = 0

	fshift = dft_shift*mask
	f_ishift = np.fft.ifftshift(fshift)
	img_back = cv2.idft(f_ishift)
	img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
	plt.subplot(121),plt.imshow(img, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
	plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
	plt.show()

	plt.subplot(121),plt.imshow(img_back, cmap = 'gray')
	plt.title('Output Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(mag_mask, cmap = 'gray')
	plt.title('Cut Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
	plt.show()
コード例 #13
0
ファイル: image_utils.py プロジェクト: Cosijopiii/EyeTab
def measure_blurriness_DFT(img):
    
    """ More complex blurriness measure averaging top 90% of frequencies in image
    """
    
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur_img = cv2.GaussianBlur(img, (3, 3), 0)
    
    dftHeight = cv2.getOptimalDFTSize(blur_img.shape[0])
    dftWidth = cv2.getOptimalDFTSize(blur_img.shape[1])
    
    complexImg = np.zeros([dftHeight, dftWidth, 2], dtype=float)
    complexImg[0:img.shape[0], 0:img.shape[1], 0] = img / 255.0
            
    dft_img = cv2.dft(complexImg)
    dft_img = cv2.magnitude(dft_img[:, :, 0], dft_img[:, :, 1])
    dft_img = cv2.log(dft_img + 1)
    cv2.normalize(dft_img, dft_img, 0, 1, cv2.NORM_MINMAX)
    
    dft_img_h, dft_img_w = dft_img.shape[:2]
    win_size = dft_img_w * 0.55
    window = dft_img[dft_img_h / 2 - win_size:dft_img_h / 2 + win_size,
                     dft_img_w / 2 - win_size:dft_img_w / 2 + win_size]
 
    return np.mean(np.abs(window))
コード例 #14
0
ファイル: saliency.py プロジェクト: xenron/sandbox-da-python
    def PlotPowerSpectrum(self):
        # convert the frame to grayscale if necessary
        if len(self.frameOrig.shape)>2:
            frame = cv3.cvtColor(self.frameOrig, cv3.COLOR_BGR2GRAY)
        else:
            frame = self.frameOrig

        # expand the image to an optimal size for FFT
        rows,cols = self.frameOrig.shape[:2]
        nrows = cv3.getOptimalDFTSize(rows)
        ncols = cv3.getOptimalDFTSize(cols)
        frame = cv3.copyMakeBorder(frame, 0, ncols-cols, 0, nrows-rows, 
            cv3.BORDER_CONSTANT, value = 0)

        # do FFT and get log-spectrum
        if self.useNumpyFFT:
            imgDFT = np.fft.fft2(frame)
            spectrum = np.log10(np.real(np.abs(imgDFT))**2)
        else:
            imgDFT = cv3.dft(np.float32(frame), flags=cv3.DFT_COMPLEX_OUTPUT)
            spectrum = np.log10(imgDFT[:,:,0]**2+imgDFT[:,:,1]**2)

        # radial average
        L = max(frame.shape)
        freqs = np.fft.fftfreq(L)[:L/2]
        dists = np.sqrt(np.fft.fftfreq(frame.shape[0])[:,None]**2
            + np.fft.fftfreq(frame.shape[1])**2)
        dcount = np.histogram(dists.ravel(), bins=freqs)[0]
        histo,bins = np.histogram(dists.ravel(), bins=freqs, weights=spectrum.ravel())

        centers = (bins[:-1] + bins[1:]) / 2
        plt.plot(centers, histo/dcount)
        plt.xlabel('frequency')
        plt.ylabel('log-spectrum')
        plt.show()
コード例 #15
0
ファイル: test_dft.py プロジェクト: 007Indian/opencv
    def test_dft(self):

        img = self.get_sample('samples/data/rubberwhale1.png', 0)
        eps = 0.001

        #test direct transform
        refDft = np.fft.fft2(img)
        refDftShift = np.fft.fftshift(refDft)
        refMagnitide = np.log(1.0 + np.abs(refDftShift))

        testDft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
        testDftShift = np.fft.fftshift(testDft)
        testMagnitude = np.log(1.0 + cv2.magnitude(testDftShift[:,:,0], testDftShift[:,:,1]))

        refMagnitide = cv2.normalize(refMagnitide, 0.0, 1.0, cv2.NORM_MINMAX)
        testMagnitude = cv2.normalize(testMagnitude, 0.0, 1.0, cv2.NORM_MINMAX)

        self.assertLess(cv2.norm(refMagnitide - testMagnitude), eps)

        #test inverse transform
        img_back = np.fft.ifft2(refDft)
        img_back = np.abs(img_back)

        img_backTest = cv2.idft(testDft)
        img_backTest = cv2.magnitude(img_backTest[:,:,0], img_backTest[:,:,1])

        img_backTest = cv2.normalize(img_backTest, 0.0, 1.0, cv2.NORM_MINMAX)
        img_back = cv2.normalize(img_back, 0.0, 1.0, cv2.NORM_MINMAX)

        self.assertLess(cv2.norm(img_back - img_backTest), eps)
コード例 #16
0
ファイル: script.py プロジェクト: polakluk/fourier-mellin
def calculateFft(img):
    imgTmp  = np.float32(img)
    # FFT of the image
    imgFft = cv2.dft(imgTmp,flags = cv2.DFT_COMPLEX_OUTPUT)
    # the FFT shift is needed in order to center the results
    imgFftShifted = np.fft.fftshift(imgFft)
    return (imgFft, imgFftShifted)
コード例 #17
0
ファイル: MOSSE.py プロジェクト: Isaac-W/CS510_Assignments
    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
コード例 #18
0
def doDFT():
	# read as gray image.
	img = cv2.imread('1.jpg', 0)
	# cv2.imwrite('gray.jpg', img)

	# t1 = dct(dct(img.T, norm='ortho').T, norm='ortho')
	dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
	dft_shift = np.fft.fftshift(dft)

	magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))


	rows, cols = img.shape
	crow,ccol = rows/2 , cols/2

	# create a mask first, center square is 1, remaining all zeros
	mask = np.zeros((rows,cols,2),np.uint8)
	mask[crow-5:crow+5, ccol-5:ccol+5] = 1

	# apply mask and inverse DFT
	fshift = dft_shift*mask
	print fshift[:, :, 0]
	print fshift[:, :, 0].shape
	f_ishift = np.fft.ifftshift(fshift)
	img_back = cv2.idft(f_ishift)
	img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

	plt.subplot(121),plt.imshow(img, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
	plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
	plt.show()
	return
コード例 #19
0
ファイル: MOSSE.py プロジェクト: Isaac-W/CS510_Assignments
    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()
コード例 #20
0
ファイル: fft_enchance_image.py プロジェクト: danielbicho/PIB
def main():
    parser = argparse.ArgumentParser(
        description='Enhance fingerprint image with diferent parameters.')
    parser.add_argument(
        "image_path", help="Specify image path location")

    args = parser.parse_args()

    pre_processor = preprocessing.PreProcessFingerImage(args.image_path)
    pre_processor.process_image()
    image_pre = pre_processor.get_preprocessed_image()

    #gaussian_3 = cv2.GaussianBlur(image, (9,9), 10.0)
    #unsharp_image = cv2.addWeighted(image, 1.5, gaussian_3, -0.5, 0, image)

    dft = cv2.dft(np.float32(image_pre), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    dft_filtered = preprocessing.frequency_filters.blpf(dft_shift, 70, 20)
    f_ishift = np.fft.ifftshift(dft_filtered)
    image_pre = cv2.idft(
        f_ishift, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)

    for k in np.linspace(0, 2, 5):
        for window in [20, 25, 35, 40, image_pre.shape[0]]:
            image_fft = enhance_image(image_pre, window, 2, k)
            cv2.imshow('FF Enhanced Image with k=' + str(k) +
                       ' window=' + str(window), image_fft)
            cv2.waitKey()
            cv2.destroyAllWindows()
コード例 #21
0
ファイル: mosse.py プロジェクト: mattalexpugh/mphil-neptune
    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()
コード例 #22
0
ファイル: deconv.py プロジェクト: fzliu/radiant
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)
コード例 #23
0
ファイル: MOSSE.py プロジェクト: Isaac-W/CS510_Assignments
    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*
コード例 #24
0
ファイル: MOSSE.py プロジェクト: Isaac-W/CS510_Assignments
    def apply_sample(self, sample):
        # Preprocess, get DFT
        img = self.preprocess(sample)
        self.F = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)

        self.count += 1

        # Update filter
        self.update_kernel()
コード例 #25
0
ファイル: mosse.py プロジェクト: AnnaPetrovicheva/opencv
 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
コード例 #26
0
ファイル: dft.py プロジェクト: alesanmed/MC-Deliverable-2
def run(in_file, out_file):
	img = cv2.imread(in_file)
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	rows, cols = gray.shape

	dft = cv2.dft(np.float32(gray),flags = cv2.DFT_COMPLEX_OUTPUT)
	dft_shift = np.fft.fftshift(dft)

	magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

	(_, thresh) = cv2.threshold(magnitude_spectrum, 230, 255, cv2.THRESH_BINARY)

	thresh = np.uint8(thresh)

	lines = cv2.HoughLines(thresh,1,np.pi/180,30)
	magnitude_spectrum_lines = np.copy(magnitude_spectrum)
	
	for rho,theta in lines[0]:
		a = np.cos(theta)
		b = np.sin(theta)
		x0 = a*rho
		y0 = b*rho
		x1 = int(x0 + 1000*(-b))
		y1 = int(y0 + 1000*(a))
		x2 = int(x0 - 1000*(-b))
		y2 = int(y0 - 1000*(a))

		m_numerator = y2 - y1
		m_denominator = x2 - x1

		angle = np.rad2deg(math.atan2(m_numerator, m_denominator))

		M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)

		if cols > rows:
			out_dims = (cols, cols)
		else:
			out_dims = (rows, rows)

		rotated_img = cv2.warpAffine(img, M, out_dims)

		cv2.line(magnitude_spectrum_lines,(x1,y1),(x2,y2),(0,0,255),2)

	b,g,r = cv2.split(rotated_img)
	rotated_img = cv2.merge([r,g,b])  
	rotated_img = Image.fromarray(rotated_img)
	rotated_img.save(out_file)

	magnitude_spectrum = Image.fromarray(magnitude_spectrum).convert('RGB')
	magnitude_spectrum.save('results/fourier.png')

	magnitude_spectrum_lines = Image.fromarray(magnitude_spectrum_lines).convert('RGB')
	magnitude_spectrum_lines.save('results/fourier_lines.png')

	"""
コード例 #27
0
ファイル: MOSSE.py プロジェクト: Isaac-W/CS510_Assignments
    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)
コード例 #28
0
ファイル: main.py プロジェクト: katejim/CV
def furieTransform(size):
    dftImage = cv2.dft(np.float32(inImg))
    dftShiftImage = np.fft.fftshift(dftImage)
    rows, cols = inImg.shape
    centralRow, centralCol = rows / 2, cols / 2
    mask = np.ones((rows, cols), np.uint8)
    mask[centralRow - size:centralRow + size, centralCol - size:centralCol + size] = 0
    fshift = dftShiftImage * mask
    fIShift = np.fft.ifftshift(fshift)
    imgResult = cv2.idft(fIShift)
    cv2.imwrite("out_furie.bmp", imgResult)
コード例 #29
0
ファイル: pdifun.py プロジェクト: pdifich/pdi_functions
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)
コード例 #30
0
ファイル: imagecompress.py プロジェクト: phuston/ImageCloud
    def __init__(self, img_file):
        self.img = cv2.imread(img_file,0)

        # Convert image to np array, shift DC to center of image
        dft = cv2.dft(np.float32(self.img),flags = cv2.DFT_COMPLEX_OUTPUT) 
        self.dft_shift = np.fft.fftshift(dft)

        self.rows, self.cols = self.img.shape
        self.img_size = np.array([self.rows,self.cols])
        self.crow,self.ccol = self.rows/2 , self.cols/2
        # apply mask and inverse DFT
        self.fshift = self.dft_shift
コード例 #31
0
 radius = cv2.getTrackbarPos("radius","lpFilterSpectrum")
 lpType = cv2.getTrackbarPos("lpType","lpFilterSpectrum")
 #第五步:构建低通滤波器
 lpFilter = createLPFilter(spectrum.shape,maxLoc,radius,lpType)
 #第六步:低通滤波器和快速傅里叶变换对应位置相乘(点乘)
 rows,cols = spectrum.shape[:2]
 fImagefft2_lpFilter = np.zeros(fImagefft2.shape,fImagefft2.dtype)
 for i in xrange(2):
     fImagefft2_lpFilter[:rows,:cols,i] = fImagefft2[:rows,:cols,i]*lpFilter
 #低通傅里叶变换的傅里叶谱
 lp_amplitude = amplitudeSpectrum(fImagefft2_lpFilter)
 #显示低通滤波后的傅里叶谱的灰度级
 lp_spectrum = graySpectrum(lp_amplitude)
 cv2.imshow("lpFilterSpectrum", lp_spectrum)
 #第七和八步:对低通傅里叶变换执行傅里叶逆变换,并只取实部
 cv2.dft(fImagefft2_lpFilter,result,cv2.DFT_REAL_OUTPUT+cv2.DFT_INVERSE+cv2.DFT_SCALE)
 #第九步:乘以(-1)^(r+c)
 for r in xrange(rows):
     for c in xrange(cols):
         if (r+c)%2:
             result[r][c]*=-1
 #第十步:数据类型转换,并进行灰度级显示,截取左上角,大小和输入图像相等
 for r in xrange(rows):
     for c in xrange(cols):
         if result[r][c] < 0:
             result[r][c] = 0
         elif result[r][c] > 255:
             result[r][c] = 255
 lpResult = result.astype(np.uint8)
 lpResult = lpResult[:image.shape[0],:image.shape[1]]
 cv2.imshow("LPFilter",lpResult)             
コード例 #32
0
ファイル: highPass.py プロジェクト: pondz1/027
        radius = cv2.getTrackbarPos("Radius", "tracks")
        lpType = cv2.getTrackbarPos("Filter type", "tracks")
        nrows, ncols = img_fft.shape[:2]
        # x, y = int(ncols / 2), int(nrows / 2)  # Notice here are the coordinates
        # ilpFilter = createHPFilter(img_fft.shape, (x, y), radius, lpType)
        ilpFilter = createHPFilter(img_fft.shape, maxLoc, radius, lpType)

        # 3.High pass filter
        img_filter = ilpFilter * img_fft

        _, gray_spectrum = graySpectrum(
            img_filter)  # Observe the change of the filter

        # 4. Inverse Fourier transform, and take the real part for cutting, And decentralize
        img_ift = cv2.dft(img_filter,
                          flags=cv2.DFT_INVERSE + cv2.DFT_REAL_OUTPUT +
                          cv2.DFT_SCALE)
        ori_img = np.copy(img_ift[:rows, :cols])
        for r in range(rows):
            for c in range(cols):
                if (r + c) % 2:
                    ori_img[r][c] = -1 * ori_img[r][c]
                # Truncate high and low values
                if ori_img[r][c] < 0:
                    ori_img[r][c] = 0
                if ori_img[r][c] > 255:
                    ori_img[r][c] = 255
        # ori_img[ori_img < 0] = 0
        # ori_img[ori_img > 255] = 255
        ori_img = ori_img.astype(np.uint8)
コード例 #33
0
#negy.show()
#posx.show()
#posy.show()
#negx.show()
#negz.show()


cv2.imshow("Input",input)
outputImage.show()


img=cv2.imread('images/xpos.jpg',0)

# the following section can be removed as per wish as it contains some frequency analysis I had done on the image faces.
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

plt.imshow(magnitude_spectrum,cmap='gray')
plt.show()
plt.imshow(img,cmap='gray')
plt.show()

hist,bins = np.histogram(magnitude_spectrum.ravel(),256,[0,256])
print hist
plt.plot(hist) 
plt.show() 

blur1 = cv2.GaussianBlur(img,(5,5),0)
コード例 #34
0
import cv2
import numpy as np
from matplotlib import pyplot as plt

img_1 = cv2.imread('apple.jpeg', 0)
img_2 = cv2.imread('orange.jpeg', 0)

plt.subplot(121), plt.imshow(img_1, cmap='gray')
plt.title('Input Image_1'), plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(img_2, cmap='gray')
plt.title('Input Image_2'), plt.xticks([]), plt.yticks([])
plt.show()
# fft on image_1
dft_1 = cv2.dft(np.float32(img_1), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift_1 = dft_1
magnitude_spectrum_1 = cv2.magnitude(dft_shift_1[:, :, 0], dft_shift_1[:, :,
                                                                       1])
dft_complex_1 = dft_shift_1[:, :, 0] + 1j * dft_shift_1[:, :, 1]
phase_1 = np.angle(dft_complex_1)

#plt.subplot(132),plt.imshow(magnitude_spectrum_1, cmap = 'gray')
#plt.title('Magnitude Spectrum_1'), plt.xticks([]), plt.yticks([])
#plt.subplot(133),plt.imshow(phase_1,cmap='gray')
#plt.title('Phase_1'),plt.xticks([]),plt.yticks([])

#fft on image_2
dft_2 = cv2.dft(np.float32(img_2), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift_2 = dft_2
magnitude_spectrum_2 = cv2.magnitude(dft_shift_2[:, :, 0], dft_shift_2[:, :,
コード例 #35
0
    if len(sys.argv) > 1:
        image = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
    else:
        print "usage:python fft2.py imageFile"
    cv2.imshow("image", image)
    fft2 = fft2Image(image)
    amplitude = amplitudeSpectrum(fft2)
    logAmplitude = graySpectrum(amplitude)
    phase = phaseSpectrum(fft2)
    cosSpectrum = np.cos(phase)
    sinSectrum = np.sin(phase)
    meanLogAmplitude = cv2.boxFilter(logAmplitude, cv2.CV_32FC1, (3, 3))
    spectralResidual = logAmplitude - meanLogAmplitude
    expSR = np.exp(spectralResidual)
    real = expSR * cosSpectrum
    imaginary = expSR * sinSectrum
    com = np.zeros((real.shape[0], real.shape[1], 2), np.float32)
    com[:, :, 0] = real
    com[:, :, 1] = imaginary
    ifft2 = np.zeros(com.shape, np.float32)
    cv2.dft(com, ifft2, cv2.DFT_COMPLEX_OUTPUT + cv2.DFT_INVERSE)
    saliencymap = np.power(ifft2[:, :, 0], 2) + np.power(ifft2[:, :, 1], 2)
    saliencymap = cv2.GaussianBlur(saliencymap, (31, 31), 2.5)
    saliencymap = saliencymap / np.max(saliencymap)
    saliencymap = np.power(saliencymap, 0.5)
    saliencymap = np.round(saliencymap * 255)
    saliencymap = saliencymap.astype(np.uint8)
    cv2.imshow("saliencymap", saliencymap)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
コード例 #36
0
def fftd(img, backwards=False):
    # shape of img can be (m,n), (m,n,1) or (m,n,2)
    # in my test_videos, fft provided by numpy and scipy are slower than cv2.dft
    return cv2.dft(np.float32(img), flags=(
        (cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT))  # 'flags =' is necessary!
コード例 #37
0
ファイル: PopiPy.py プロジェクト: WaqSid99/PopiPy
def fourierFilter(img):
    """
    fourierFilter()
    Usage: Applies fourier filter to the image for noise reduction.
    Image needs to be filtered since origanl image is very noisey which causes issue with fringe finding
    Source: https://github.com/bnsreenu/python_for_image_processing_APEER/blob/master/tutorial41_image_filters_using_fourier_transform_DFT.py

    [In]: img -> Input Image for noise reduction
    [Out]: img_back -> Image with fourier filter applied 

    TODO: 
    1- Fourier filter is not working as expected and work needs to be done on this. 
    Could look for some other ways to reduce noise if fft doesnt work
    2- It applys a circular mask on the origanl image but could also do with a cross (as done
    in mathematica). The code commented out does apply a cross mask but that doesn't work either

    """

    dft=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift=np.fft.fftshift(dft)
    magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
   
   
    dia=int(diameter.get())    
    rows, cols = img.shape
    mask = np.zeros((rows, cols, 2), np.uint8)
    r = 100
    x, y = np.ogrid[:rows, :cols]
    mask_area = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= r*r
    mask[mask_area] = 1
    """
    np_mask_horizontal=np.array([])
    mask=[]
    
    for i in range(rows):
        for j in range(cols):
            a=np.array([1-math.exp(-(i-cols/2)**2/9)])
            b=np.array([1-math.exp(-(i-cols/2)**2/9)])
            np_mask_horizontal=np.concatenate((a,b))
            mask.append(np_mask_horizontal)
    horizontal_line=np.array([mask])
    horizontal_line=np.reshape(horizontal_line,(dia*2,dia*2,2))

    np_mask_vertical=np.array([])
    mask.clear()
    for i in range (rows):
        for j in range (cols):
            a=np.array([1-math.exp(-(j-cols/2)**2/9)])
            b=np.array([1-math.exp(-(j-cols/2)**2/9)])
            np_mask_vertical=np.concatenate((a,b))
            mask.append(np_mask_vertical)
    vertical_line=np.array([mask])
    vertical_line=np.reshape(vertical_line,(dia*2,dia*2,2))

    fourierfactor3=0.005
    fourierfactor4=0.05
    np_mask=np.array([])
    mask.clear()
    for i in range(rows):
        for j in range(cols):
            a=np.array([1-(1-fourierfactor4)*math.exp(-(math.sqrt(min(j,cols-j)**2 + min(i,rows-i)**2)/(0.5*rows))**2/fourierfactor3**2)])
            b=np.array([1-(1-fourierfactor4)*math.exp(-(math.sqrt(min(j,cols-j)**2 + min(i,rows-i)**2)/(0.5*rows))**2/fourierfactor3**2)])
            np_mask=np.concatenate((a,b))
            mask.append(np_mask)
    additional_filter=np.array([mask])
    additional_filter=np.reshape(additional_filter,(dia*2,dia*2,2))
    #print(additional_filter)
    """
    # apply mask and inverse DFT: Multiply fourier transformed image (values)
    #with the mask values. 
    fshift =  dft_shift* mask
    print(fshift)
    #Get the magnitude spectrum (only for plotting purposes)
    fshift_mask_mag = 20 * np.log(cv2.magnitude(fshift[:, :, 0], fshift[:, :, 1]))

    #Inverse shift to shift origin back to top left.
    f_ishift = np.fft.ifftshift(fshift)

    #Inverse DFT to convert back to image domain from the frequency domain. 
    #Will be complex numbers
    img_back = cv2.idft(f_ishift)

    #Magnitude spectrum of the image domain
    img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

    fig = plt.figure(figsize=(12, 12))
    ax1 = fig.add_subplot(2,2,1)
    ax1.imshow(img, cmap='gray')
    ax1.title.set_text('Input Image')
    ax2 = fig.add_subplot(2,2,2)
    ax2.imshow(magnitude_spectrum, cmap='gray')
    ax2.title.set_text('FFT of image')
    ax3 = fig.add_subplot(2,2,3)
    ax3.imshow(fshift_mask_mag, cmap='gray')
    ax3.title.set_text('FFT + Mask')
    ax4 = fig.add_subplot(2,2,4)
    ax4.imshow(img_back, cmap='gray')
    ax4.title.set_text('After inverse FFT')
    plt.show()

    return img_back
コード例 #38
0
def Fourier(img):
    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    spectrum = cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])
    log_spectrum = 20 * np.log1p(spectrum)
    return log_spectrum
コード例 #39
0
 def calculate_fourier(img):
     """
         Method calculates thr Fourier coefficients of an 2-D numpy array
     """
     dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
     return np.fft.fftshift(dft)
コード例 #40
0
ファイル: pre_processing.py プロジェクト: levskaya/CaImAn-1
def get_noise_fft(Y,
                  noise_range=[0.25, 0.5],
                  noise_method='logmexp',
                  max_num_samples_fft=3072,
                  opencv=True):
    """Estimate the noise level for each pixel by averaging the power spectral density.

    Inputs:
    -------

    Y: np.ndarray

    Input movie data with time in the last axis

    noise_range: np.ndarray [2 x 1] between 0 and 0.5
        Range of frequencies compared to Nyquist rate over which the power spectrum is averaged
        default: [0.25,0.5]

    noise method: string
        method of averaging the noise.
        Choices:
            'mean': Mean
            'median': Median
            'logmexp': Exponential of the mean of the logarithm of PSD (default)

    Output:
    ------
    sn: np.ndarray
        Noise level for each pixel
    """
    T = Y.shape[-1]
    # Y=np.array(Y,dtype=np.float64)

    if T > max_num_samples_fft:
        Y = np.concatenate(
            (Y[..., 1:max_num_samples_fft // 3 + 1],
             Y[...,
               np.int(T // 2 - max_num_samples_fft / 3 /
                      2):np.int(T // 2 + max_num_samples_fft / 3 / 2)],
             Y[..., -max_num_samples_fft // 3:]),
            axis=-1)
        T = np.shape(Y)[-1]

    # we create a map of what is the noise on the FFT space
    ff = np.arange(0, 0.5 + 1. / T, 1. / T)
    ind1 = ff > noise_range[0]
    ind2 = ff <= noise_range[1]
    ind = np.logical_and(ind1, ind2)
    # we compute the mean of the noise spectral density s
    if Y.ndim > 1:
        if opencv:
            import cv2
            psdx = []
            for y in Y.reshape(-1, T):
                dft = cv2.dft(
                    y, flags=cv2.DFT_COMPLEX_OUTPUT).squeeze()[:len(ind)][ind]
                psdx.append(np.sum(1. / T * dft * dft, 1))
            psdx = np.reshape(psdx, Y.shape[:-1] + (-1, ))
        else:
            xdft = np.fft.rfft(Y, axis=-1)
            xdft = xdft[..., ind[:xdft.shape[-1]]]
            psdx = 1. / T * abs(xdft)**2
        psdx *= 2
        sn = mean_psd(psdx, method=noise_method)

    else:
        xdft = np.fliplr(np.fft.rfft(Y))
        psdx = 1. / T * (xdft**2)
        psdx[1:] *= 2
        sn = mean_psd(psdx[ind[:psdx.shape[0]]], method=noise_method)

    return sn, psdx
コード例 #41
0
ファイル: fourier.py プロジェクト: sammy4321/OpenCV-Tutorial
import cv2
from matplotlib import pyplot as plt

# Load the image in gray scale
img = cv2.imread('../data/messi5.jpg', 0)
rows, cols = img.shape

# Transform the image to improve the speed in the fourier transform calculation
optimalRows = cv2.getOptimalDFTSize(rows)
optimalCols = cv2.getOptimalDFTSize(cols)
optimalImg = np.zeros((optimalRows, optimalCols))
optimalImg[:rows, :cols] = img
crow, ccol = optimalRows / 2, optimalCols / 2

# Calculate the discrete Fourier transform
dft = cv2.dft(np.float32(optimalImg), flags=cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)

# Mask everything except the center
mask = np.zeros((optimalRows, optimalCols, 2), np.uint8)
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1
dftShift = dftShift * mask

# Rescale the values for visualization purposes
magnitudeSpectrum = 20 * np.log(
    cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1]))

# Reconstruct the image using the inverse Fourier transform
newDft = np.fft.ifftshift(dftShift)
result = cv2.idft(newDft)
result = cv2.magnitude(result[:, :, 0], result[:, :, 1])
コード例 #42
0
m = cv.getOptimalDFTSize(rows)
n = cv.getOptimalDFTSize(cols)
padded = cv.copyMakeBorder(img,
                           0,
                           m - rows,
                           0,
                           n - cols,
                           cv.BORDER_CONSTANT,
                           value=[0, 0, 0])
# print(padded.shape)
# padded.dtype = np.float32
# print(padded.shape)
#print(padded.dtype)
planes = [np.float32(padded), np.zeros(padded.shape, np.float32)]
complexI = cv.merge(planes)
cv.dft(complexI, complexI)
cv.split(complexI, planes)
# print(planes[0])
cv.magnitude(planes[0], planes[1], planes[0])  #magnitude()的意义为根号下平方和 即 计算辐值
magI = planes[0]

matOfones = np.ones(magI.shape, dtype=magI.dtype)
cv.add(matOfones, magI, magI)
cv.log(magI, magI)
#print(magI.dtype)
magI_rows, magI_cols = magI.shape
magI = magI[0:(magI_rows & -2), 0:(magI_cols & -2)]
cx = int(magI_rows / 2)
cy = int(magI_cols / 2)

q0 = magI[0:cx, 0:cy]  # Top-Left - Create a ROI per quadrant
def main():  #argv):
    timeStart = time.time()
    # Step 1 - Apply FFT on both images and get their magnitude spectrums
    # image (we are looking for), lets call it original
    imgOriginal, imgOriginalFft, imgOriginalMags = readImage(
        "D:/Maria/April/fourier_mellin/test_data/Mio1.jpg"
    )  #("D:/Maria/April/fourier_mellin/test_data/orig.png")#readImage(argv[1])
    # image (we are searching in), lets call it transformed
    imgTransformed, imgTransformedFft, imgTransformedMags = readImage(
        "D:/Maria/April/fourier_mellin/test_data/sdddd.png"
    )  #("D:/Maria/April/fourier_mellin/test_data/test.png")#readImage(argv[2])

    # Step 2 - Apply highpass filter on their magnitude spectrums
    highPassFilter = prepareHighPassFilter(imgOriginalMags)
    imgOriginalMagsFilter = imgOriginalMags * highPassFilter
    imgTransformedMagsFilter = imgTransformedMags * highPassFilter

    # Step 3 - Convert magnitudes both images to log-polar coordinates
    # Step 3.1 - Precompute parameters (both images have the same dimensions)
    centerTrans, angleStep, logBase = computeLogPolarParameters(
        imgOriginalMagsFilter)
    imgOriginalLogPolar = convertToLogPolar(imgOriginalMagsFilter, centerTrans,
                                            angleStep, logBase, polarMode)
    imgTransformedLogPolar = convertToLogPolar(imgTransformedMagsFilter,
                                               centerTrans, angleStep, logBase,
                                               polarMode)

    # Step 3.1 - Apply FFT on magnitude spectrums in log polar coordinates (in this case, not using FFT shift as it leads to computing [180-angle] results)
    imgOriginalLogPolarComplex = cv2.dft(np.float32(imgOriginalLogPolar),
                                         flags=cv2.DFT_COMPLEX_OUTPUT)
    imgTransformedLogPolarComplex = cv2.dft(np.float32(imgTransformedLogPolar),
                                            flags=cv2.DFT_COMPLEX_OUTPUT)

    # Step 4 - Apply phase corelation on both images (FFT applied on log polar images) to retrieve rotation (angle) and scale factor
    angle, scale = phaseCorrelation(imgOriginalLogPolarComplex,
                                    imgTransformedLogPolarComplex)
    # Step 4.1 Convert to degrees based on formula in paper (26) and adjust it to (-pi/2, pi/2) range
    angleDeg = -(float(angle) * 180.0) / imgOriginalLogPolarComplex.shape[0]
    if angleDeg < -45:
        angleDeg += 180
    else:
        if angleDeg > 90.0:
            angleDeg -= 180

    # Step 4.2 Calculate scale factor based on formula in paper (25)
    scaleFactor = logBase**scale

    # Step 5 - Apply rotation and scaling on transformed image
    transformMatrix = cv2.getRotationMatrix2D((centerTrans[0], centerTrans[1]),
                                              angleDeg, scaleFactor)
    imgTransformedNew = cv2.warpAffine(
        imgTransformed, transformMatrix,
        (imgTransformed.shape[1], imgTransformed.shape[0]))

    # Step 6 - Apply phase corelation on both images to retrieve translation
    # Step 6.1 Apply FFT to newly created transformed image
    imgTransformedNewFft, imgTransformedNewftShifted = calculateFft(
        imgTransformedNew)
    # Step 6.2 - Use phase corelation to get translation coordinates
    y, x = phaseCorrelation(imgTransformedNewftShifted, imgOriginalFft)
    # Step 6.3 Apply translation on the final image
    if x > imgOriginal.shape[0] // 2:
        x -= imgOriginal.shape[0]
    if y > imgOriginal.shape[1] // 2:
        y -= imgOriginal.shape[1]

    translationMatrix = np.float32([[1, 0, -x], [0, 1, -y]])
    imgFinal = cv2.warpAffine(
        imgTransformedNew, translationMatrix,
        (imgTransformed.shape[1], imgTransformed.shape[0]))
    timeEnd = time.time()

    # Step 7 - Return final results (rotation, scale factor, translation)
    print("Angle = " + str(angleDeg) + " Deg")
    print("Scale = " + str(scaleFactor))
    print("Translation")
    print("X = " + str(-x))
    print("Y = " + str(-y))
    print("Time = " + str(timeEnd - timeStart))

    if resultsComparation:
        plt.subplot(221), plt.imshow(imgOriginal, cmap='gray')
        plt.subplot(222), plt.imshow(imgTransformed, cmap='gray')
        plt.subplot(223), plt.imshow(imgOriginal - imgFinal, cmap='bwr')
        plt.subplot(224), plt.imshow(imgFinal, cmap='gray')
        plt.show()
    else:
        plt.subplot(521), plt.imshow(imgOriginal, cmap='gray')
        plt.subplot(522), plt.imshow(imgTransformed, cmap='gray')
        plt.subplot(523), plt.imshow(imgOriginalMagsFilter, cmap='gray')
        plt.subplot(524), plt.imshow(imgTransformedMagsFilter, cmap='gray')
        plt.subplot(525), plt.imshow(imgOriginalLogPolar, cmap='gray')
        plt.subplot(526), plt.imshow(imgTransformedLogPolar, cmap='gray')
        plt.subplot(527), plt.imshow(imgTransformedNew, cmap='gray')
        plt.subplot(528), plt.imshow(imgOriginal - imgFinal, cmap='bwr')
        plt.subplot(529), plt.imshow(imgFinal, cmap='gray')
        plt.show()
コード例 #44
0
    padded = cv.copyMakeBorder(image,
                               0,
                               M - image.shape[0],
                               0,
                               N - image.shape[1],
                               cv.BORDER_CONSTANT,
                               value=0)

    planes_0 = np.array(padded, dtype='float32')
    planes_1 = np.zeros(padded.shape, dtype='float32')
    planes2_0 = np.array(padded, dtype='float32')
    planes2_1 = np.zeros(padded.shape, dtype='float32')

    complexImg = cv.merge((planes_0, planes_1))
    complexImg = cv.dft(complexImg)

    complexImg2 = cv.merge((planes2_0, planes2_1))
    complexImg2 = cv.dft(complexImg2)

    filter1 = complexImg.copy()
    filter1 = create_ButterworthLowpassFilter(filter1, radius, order, width)
    filter2 = complexImg2.copy()
    filter2 = create_ButterworthHighpassFilter(filter2, radius2, order2, width)

    complexImg = shiftDFT(complexImg)
    complexImg = cv.mulSpectrums(complexImg, filter1, 0)
    complexImg = shiftDFT(complexImg)

    complexImg2 = shiftDFT(complexImg2)
    complexImg2 = cv.mulSpectrums(complexImg2, filter2, 0)
コード例 #45
0
     [[34, 56, 1, 0, 255, 230, 45, 12], [0, 201, 101, 125, 52, 12, 124, 12],
      [3, 41, 42, 40, 12, 90, 123, 45], [5, 245, 98, 32, 34, 234, 90, 123],
      [12, 12, 10, 41, 56, 89, 189, 5], [112, 87, 12, 45, 78, 45, 10, 1],
      [42, 123, 234, 12, 12, 21, 56, 43], [1, 2, 45, 123, 10, 44, 123, 90]],
     np.float64)
 #卷积核
 kernel = np.array([[1, 0, -1], [1, 0, 1], [1, 0, -1]], np.float64)
 # I 与 kernel 进行全卷积
 confull = signal.convolve2d(I,
                             kernel,
                             mode='full',
                             boundary='fill',
                             fillvalue=0)
 # I 的傅里叶变换
 FT_I = np.zeros((I.shape[0], I.shape[1], 2), np.float64)
 cv2.dft(I, FT_I, cv2.DFT_COMPLEX_OUTPUT)
 # kernel 的傅里叶变换
 FT_kernel = np.zeros((kernel.shape[0], kernel.shape[1], 2), np.float64)
 cv2.dft(kernel, FT_kernel, cv2.DFT_COMPLEX_OUTPUT)
 # 傅里叶变换
 fft2 = np.zeros((confull.shape[0], confull.shape[1]), np.float64)
 #对 I 进行右侧和下侧补 0
 I_Padded = np.zeros(
     (I.shape[0] + kernel.shape[0] - 1, I.shape[1] + kernel.shape[1] - 1),
     np.float64)
 I_Padded[:I.shape[0], :I.shape[1]] = I
 FT_I_Padded = np.zeros((I_Padded.shape[0], I_Padded.shape[1], 2),
                        np.float64)
 cv2.dft(I_Padded, FT_I_Padded, cv2.DFT_COMPLEX_OUTPUT)
 #对 kernel 进行右侧和下侧补 0
 kernel_Padded = np.zeros(
コード例 #46
0
def process(ip_image):
    ###########################
    ## Your Code goes here
    print("in loop")
    b, g, r = cv2.split(ip_image)

    # defining an array consisting of b,g,r channles of the blur image
    img_array = [b, g, r]

    # defining an empty array to store the final image
    final_img = np.empty((840, 1600, 3))

    # defining the angel, dis, load/noise values to be used in the program
    # below for restoring the image of different channels
    angle = [87, 90, 93]
    dis = [20, 22, 20]
    load = [22, 17, 18]
    i = 0
    # now applying the restoration algo on each channel one by one
    for color in img_array:
        img_channel = np.float32(color) / 255.0

        # forming a blur image from the given image
        d = 31
        # returns a tuple of number of rows, columns and channels of the image
        height, width = img_channel.shape[:2]
        # Creating a border around the image
        img_pad = cv2.copyMakeBorder(img_channel, d, d, d, d, cv2.BORDER_WRAP)
        # Blurring bordered image using a gaussian function and storing it in img_blur
        img_blur = cv2.GaussianBlur(img_pad, (2 * d + 1, 2 * d + 1), -1)[d:-d,
                                                                         d:-d]
        # Returning an a array representing the indices of matrix
        y, x = np.indices((height, width))
        # returning an array formed by stacking x and y and concatenating along 3rd dimension
        dist = np.dstack([x, width - x - 1, y, height - y - 1]).min(-1)
        # Comparing both the arrays and returning the element wise minimum value to array elements
        w = np.minimum(np.float32(dist) / d, 1.0)
        blur_img = img_channel * w + img_channel * (1 - w)
        #----------------------------------------------

        # Applying discrete fourier transform on blur image since images are collection of discrete values in time domain
        # so we are converting them into frequency domain

        dft_image = cv2.dft(blur_img, flags=cv2.DFT_COMPLEX_OUTPUT)

        # Defining angle ang and converting it from degree to radian assigning variable d and noise with respective values

        ang = np.deg2rad(angle[i])
        d = dis[i]
        noise = 10**(-0.1 * load[i])

        # Defining degradation_kernel using motion kernel

        # making a kernel of size 20x20
        sz = 20
        # Creating an array of the given parameters with ones
        kernel = np.ones((1, d), np.float32)
        # Defining trigonometric cosine as c and trigonoetric sine as s
        c, s = np.cos(ang), np.sin(ang)
        # Assigning matrix A with given parameters defind above with 32-bit floating values
        A = np.float32([[c, -s, 0], [s, c, 0]])
        sz2 = sz // 2
        A[:, 2] = (sz2, sz2) - np.dot(A[:, :2], ((d - 1) * 0.5, 0))
        # transforming the kern image using the specified matrix
        deg_kernel = cv2.warpAffine(kernel, A, (sz, sz), flags=cv2.INTER_CUBIC)
        #--------------------------------------------------------

        deg_kernel /= deg_kernel.sum()
        # creating a zero matrix of the size of img that will be used for padding
        deg_pad = np.zeros_like(img_channel)
        kh, kw = deg_kernel.shape
        # padding the kernel to get the degradation function of the same size as that of the image
        deg_pad[:kh, :kw] = deg_kernel

        # Performing the DFT on the psf_pad and saving it in deg_kernel_dft
        # First channel will have the real part of the result and second channel will have the imaginary part of the result
        deg_dft = cv2.dft(deg_pad,
                          flags=cv2.DFT_COMPLEX_OUTPUT,
                          nonzeroRows=kh)
        # taking magnitude of the complex values
        deg_dft_2 = (deg_dft**2).sum(-1)
        i_deg_dft_2 = deg_dft / (deg_dft_2 + noise)[..., np.newaxis]

        # Performing element wise multiplication of the two matrix IMG and iPSF that are results of a real or complex fourier transform
        Restored_img = cv2.mulSpectrums(dft_image, i_deg_dft_2, 0)
        # RES is our restored image in frequency domain now converting it in time domain by performing idft
        restored_img = cv2.idft(Restored_img,
                                flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
        # Rolling res array elements along a given axis i.e 0 and 1
        restored_img = np.roll(restored_img, -kh // 2, 0)
        restored_img = np.roll(restored_img, -kw // 2, 1)

        final_img[:, :, i] = restored_img
        i += 1

    final_img = (final_img / np.max(final_img)) * 255
    final_img = final_img.astype(np.uint8)

    brightness = 95
    contrast = 85
    highlight = 255
    shadow = brightness
    alpha_b = (highlight - shadow) / 255
    gamma_b = shadow

    buf = cv2.addWeighted(final_img, alpha_b, final_img, 0, gamma_b)

    f = float(131 * (contrast + 127)) / (127 * (131 - contrast))
    alpha_c = f
    gamma_c = 127 * (1 - f)

    final_img = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)

    det_aruco_list = detect_Aruco(final_img)
    # print(det_aruco_list)
    if det_aruco_list:
        ip_image = mark_Aruco(final_img, det_aruco_list)
        robot_state = calculate_Robot_State(final_img, det_aruco_list)
        print(robot_state)

        ###########################
    id_list = robot_state[25]

    return ip_image, id_list
コード例 #47
0
ファイル: ps2.py プロジェクト: thiagofigcosta/computer-vision
def fftGetMagAndPse(img_grey):
    img_freq = cv2.dft(np.float64(img_grey), flags=cv2.DFT_COMPLEX_OUTPUT)
    r, i = cv2.split(img_freq)
    return cv2.magnitude(r, i), cv2.phase(r, i)
コード例 #48
0
ファイル: 15图像变换.py プロジェクト: Zsx3/opencv-byZsx
# =============================================================================
# cv2.dft()
# np.fft.fftshift()

# np.fft.ifftshift()
# cv2.idft()
# magnitude(img_back[:,:,0],img[:,:,1]) 从复数变成实数
# =============================================================================
openCv中相应的函数时cv2.dft()和cv2.idft()。和前面输出的结果一样,但是双通道的。第一个通道是结果的实数
部分,第二个通道是结果的虚数部分。输入图像首先转换成np.float32格式,我们来看看如何操作

import cv2
import numpy as np
from matplotlib import pyplot as plt
img=cv2.imread('messi.jpg',0)
dft=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT) 
dft_shift=np.fft.fftshift(dft)
magnitude_spectrum=20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))#cv2.magnitude计算梯度/幅度 等价于np.abs  验证确实如此

#plt.subplot(121),plt.imshow(img,cmap='gray')
#plt.title('input image'),plt.xticks([]),plt.yticks([])
#plt.subplot(122),plt.imshow(magnitude_spectrum,cmap='gray')
#plt.title('M s'),plt.xticks([]),plt.yticks([])
#plt.show()
##这里也可以用cv2.cartToPalar(),它会同时返回幅度和相位
#现在我们来做逆变换DFT,在前面的部分,我们实现了一个HPF(高频滤波),现在我们来做LPF(低通
#滤波)将高频部分去除,其实就是对图像进行模糊操作。首先我们需要构建一个掩码,与低频区域对应的地方设置1
#高频区域对于地方设为0
rows,cols=img.shape
crow,ccol=np.uint8(rows/2),np.uint8(cols/2)
#mask
コード例 #49
0
def DFT(image):
    fft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
    return np.array([fft[:, :, 0], fft[:, :, 1]])
コード例 #50
0
def main(argv):

    print_help()

    filename = argv[0] if len(argv) > 0 else "../../../../data/lena.jpg"

    I = cv.imread(filename, cv.IMREAD_GRAYSCALE)
    if I is None:
        print('Error opening image')
        return -1
    ## [expand]
    rows, cols = I.shape
    m = cv.getOptimalDFTSize(rows)
    n = cv.getOptimalDFTSize(cols)
    padded = cv.copyMakeBorder(I,
                               0,
                               m - rows,
                               0,
                               n - cols,
                               cv.BORDER_CONSTANT,
                               value=[0, 0, 0])
    ## [expand]
    ## [complex_and_real]
    planes = [np.float32(padded), np.zeros(padded.shape, np.float32)]
    complexI = cv.merge(planes)  # Add to the expanded another plane with zeros
    ## [complex_and_real]
    ## [dft]
    cv.dft(complexI,
           complexI)  # this way the result may fit in the source matrix
    ## [dft]
    # compute the magnitude and switch to logarithmic scale
    # = > log(1 + sqrt(Re(DFT(I)) ^ 2 + Im(DFT(I)) ^ 2))
    ## [magnitude]
    cv.split(complexI, planes)  # planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    cv.magnitude(planes[0], planes[1], planes[0])  # planes[0] = magnitude
    magI = planes[0]
    ## [magnitude]
    ## [log]
    matOfOnes = np.ones(magI.shape, dtype=magI.dtype)
    cv.add(matOfOnes, magI, magI)  #  switch to logarithmic scale
    cv.log(magI, magI)
    ## [log]
    ## [crop_rearrange]
    magI_rows, magI_cols = magI.shape
    # crop the spectrum, if it has an odd number of rows or columns
    magI = magI[0:(magI_rows & -2), 0:(magI_cols & -2)]
    cx = int(magI_rows / 2)
    cy = int(magI_cols / 2)

    q0 = magI[0:cx, 0:cy]  # Top-Left - Create a ROI per quadrant
    q1 = magI[cx:cx + cx, 0:cy]  # Top-Right
    q2 = magI[0:cx, cy:cy + cy]  # Bottom-Left
    q3 = magI[cx:cx + cx, cy:cy + cy]  # Bottom-Right

    tmp = np.copy(q0)  # swap quadrants (Top-Left with Bottom-Right)
    magI[0:cx, 0:cy] = q3
    magI[cx:cx + cx, cy:cy + cy] = tmp

    tmp = np.copy(q1)  # swap quadrant (Top-Right with Bottom-Left)
    magI[cx:cx + cx, 0:cy] = q2
    magI[0:cx, cy:cy + cy] = tmp
    ## [crop_rearrange]
    ## [normalize]
    cv.normalize(
        magI, magI, 0, 1,
        cv.NORM_MINMAX)  # Transform the matrix with float values into a
    ## viewable image form(float between values 0 and 1).
    ## [normalize]
    cv.imshow("Input Image", I)  # Show the result
    cv.imshow("spectrum magnitude", magI)
    cv.waitKey()
コード例 #51
0
ファイル: a.py プロジェクト: LucasZatta/ComputerVision
def transform(img):
    dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)
    return dft
コード例 #52
0
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    h, w = im.shape[:2]

    realInput = im.astype(np.float64)

    # perform an optimally sized dft
    dft_M = cv2.getOptimalDFTSize(w)
    dft_N = cv2.getOptimalDFTSize(h)

    # copy A to dft_A and pad dft_A with zeros
    dft_A = np.zeros((dft_N, dft_M, 2), dtype=np.float64)
    dft_A[:h, :w, 0] = realInput

    # no need to pad bottom part of dft_A with zeros because of
    # use of nonzeroRows parameter in cv2.dft()
    cv2.dft(dft_A, dst=dft_A, nonzeroRows=h)

    cv2.imshow("win", im)

    # Split fourier into real and imaginary parts
    image_Re, image_Im = cv2.split(dft_A)

    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    magnitude = cv2.sqrt(image_Re**2.0 + image_Im**2.0)

    # Compute log(1 + Mag)
    log_spectrum = cv2.log(1.0 + magnitude)

    # Rearrange the quadrants of Fourier image so that the origin is at
    # the image center
    shift_dft(log_spectrum, log_spectrum)
コード例 #53
0
for i in range(10): plt.close()



image1_filename = "../test_images/consecutive_frames_before.jpg"
image2_filename = "../test_images/consecutive_frames_after.jpg"
image1 = cv2.imread(image1_filename)[:,:,0]
image2 = cv2.imread(image2_filename)[:,:,0]

crop_size = 1800
image1_cp = image1[:crop_size, :crop_size]
image2_cp = image2[:crop_size, :crop_size]



f1 = cv2.dft(image1_cp.astype(np.float32), flags=cv2.DFT_COMPLEX_OUTPUT)
f2 = cv2.dft(image2_cp.astype(np.float32), flags=cv2.DFT_COMPLEX_OUTPUT)

f1_shf = np.fft.fftshift(f1)
f2_shf = np.fft.fftshift(f2)

f1_shf_cplx = f1_shf[:,:,0]*1j + 1*f1_shf[:,:,1]
f2_shf_cplx = f2_shf[:,:,0]*1j + 1*f2_shf[:,:,1]

f1_shf_abs = np.abs(f1_shf_cplx)
f2_shf_abs = np.abs(f2_shf_cplx)
total_abs = f1_shf_abs * f2_shf_abs

P_real = (np.real(f1_shf_cplx)*np.real(f2_shf_cplx) +
          np.imag(f1_shf_cplx)*np.imag(f2_shf_cplx))/total_abs
P_imag = (np.imag(f1_shf_cplx)*np.real(f2_shf_cplx) +
コード例 #54
0
from mpl_toolkits.mplot3d import axes3d

# MARK: image
img = cv2.imread(
    '/Users/ariazare/Projects/Python/DEP_C4/Fig0438(a)(bld_600by600).tif')
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img_padded = np.zeros((602, 602), 'float32')
img_padded[:-2, 0:-2] = img

img_centered = utilities.center_frequency(img_padded)
complex_img = [
    np.array(img_centered, 'float32'),
    np.zeros(img_centered.shape, 'float32')
]
complex_img = cv2.merge(complex_img)
cv2.dft(complex_img, complex_img)

# img_mag = utilities.get_magnitude(complex_img)
# img_mag = cv2.log(img_mag,img_mag)
# img_phase = utilities.get_phase_angel(complex_img)

# plt.imshow(img_mag, 'Greys')
# plt.show()

# MARK: sobel filter kernal
sobel_filter = np.array(
    [[0, 0, 0, 0], [0, -1, 0, 1], [0, -2, 0, 2], [0, -1, 0, 1]], 'float32')

sobel_filter_sp = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], 'float32')

sobel_filter_zeros = np.zeros((602, 602), 'float32')
コード例 #55
0
ファイル: import cv2.py プロジェクト: AnushkaX/Image_Deblur
    win = 'deconvolution'

    img = cv2.imread(fn, 0)                                         #read image
    if img is None:
        print('Failed to load image!:', fn)
        sys.exit(1)

    img = np.float32(img)/255.0
    
    cv2.imshow('input', img)                                        #show input

    img = blur_edge(img)



    IMG = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)                #fourier transformation

    defocus = '--circle' in opts

    def update(_):
        ang = np.deg2rad( cv2.getTrackbarPos('Angle', win) )        #get values according to the trackbar position
        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()
コード例 #56
0
ファイル: DFT.py プロジェクト: liu1073811240/Opencv-3
import cv2
import matplotlib.pyplot as plt
import numpy as np

# opencv中的DFT(Discrete Fourier Transform)
img = cv2.imread("./images/1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rows, cols = gray.shape

# 1.DFT离散傅里叶变换:空域-->频域
dft = cv2.dft(
    src=np.float32(gray),
    flags=cv2.DFT_COMPLEX_OUTPUT)  # cv2.DFT_COMPLEX_OUTPUT表示进行傅里叶变化的方法
print(dft.shape)  # (540, 960, 2)  2表示两个通道

# 2.中心化:将低频移动到图像中心
fftshift = np.fft.fftshift(dft)
# 获取振幅谱(展示图片用):20*np.log()是为了将值限制在[0, 255]
magnitude_spectrum = 20 * np.log(
    cv2.magnitude(fftshift[:, :, 0], fftshift[:, :, 1]))

# 3.滤波操作之低通滤波(去高频,保低频)
mask = np.zeros((rows, cols, 2), dtype=np.uint8)
mask[(rows // 2 - 30):(rows // 2 + 30), (cols // 2 - 30):(cols // 2 + 30)] = 1
fftshift = fftshift * mask

# 4.去中心化:将低频和高频的位置还原
ifftshift = np.fft.ifftshift(fftshift)

# 5.逆傅里叶变换:频域-->空域
idft = cv2.idft(ifftshift)
コード例 #57
0
        fn = args[0]
    except:
        fn = '../data/licenseplate_motion.jpg'

    win = 'deconvolution'

    img = cv.imread(fn, 0)
    if img is None:
        print('Failed to load fn1:', fn1)
        sys.exit(1)

    img = np.float32(img)/255.0
    cv.imshow('input', img)

    img = blur_edge(img)
    IMG = cv.dft(img, flags=cv.DFT_COMPLEX_OUTPUT)

    defocus = '--circle' in opts

    def update(_):
        ang = np.deg2rad( cv.getTrackbarPos('angle', win) )
        d = cv.getTrackbarPos('d', win)
        noise = 10**(-0.1*cv.getTrackbarPos('SNR (db)', win))

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

        psf /= psf.sum()
コード例 #58
0
# You Can Think Of Frequency In An Image As The Rate Of Change. Parts Of The Image
# That Change Rapidly From One Color To Another (Sharp Edges) Contain High Frequencies
# And Parts That Change Gradually (E.G., Large Surgaces With Solid Colors) Contain
# Only Low Frequencies

# Import Needed Packages
import matplotlib.pyplot as plt
import numpy as np
import cv2

# Load Image As GS, Convert, Scale
image = cv2.imread('../data/Lena.png', 0).astype(np.float32) / 255

# Apply Discrete Fourier Transform
# Print Shape And Data Type
fft = cv2.dft(image, flags=cv2.DFT_COMPLEX_OUTPUT)
print("FFT Shape: ", fft.shape)
print("FFT DType: ", fft.dtype)

# Visualize Image Spectrum
# Shift In Such A Way That The Amplitude Corresponding To Zero
# Frequency Becomes Located At Center Of Image
shifted = np.fft.fftshift(fft, axes=[0, 1])
magnitude = cv2.magnitude(shifted[:, :, 0], shifted[:, :, 1])
magnitude = np.log(magnitude)
magnitude -= magnitude.min()
magnitude /= magnitude.max()

# Convert From Frequency Spectrum Back To Spatial Representation
restored = cv2.idft(fft, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
コード例 #59
0
import cv2
import numpy as np
from matplotlib import pyplot as plt

# le imagem quad
img = cv2.imread('quad.bmp', 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

# multiplicacao
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))

rows, cols = img.shape
crow, ccol = int(rows / 2), int(cols / 2)
mask = np.zeros((rows, cols, 2), np.uint8)
num = 10
mask[crow - num:crow + num, ccol - num:ccol + num] = 1
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
コード例 #60
0
        # Arrays whose size is a product of 2's, 3's, and 5's are also processed quite efficiently.
        # Hence ee modify the size of the array tothe optimal size (by padding zeros) before finding DFT.

        pad_right = nwidth - width
        pad_bottom = nheight - hieght
        nframe = cv2.copyMakeBorder(gray_frame,
                                    0,
                                    pad_bottom,
                                    0,
                                    pad_right,
                                    cv2.BORDER_CONSTANT,
                                    value=0)

        # perform the DFT and get complex output

        dft = cv2.dft(np.float32(nframe), flags=cv2.DFT_COMPLEX_OUTPUT)

        # shift it so that we the zero-frequency, F(0,0), DC component to the center of the spectrum.

        dft_shifted = np.fft.fftshift(dft)

        # perform high pass filtering

        radius = cv2.getTrackbarPos("radius", windowName2)
        hp_filter = create_high_pass_filter(nwidth, nheight, radius)

        dft_filtered = cv2.mulSpectrums(dft_shifted, hp_filter, flags=0)

        # shift it back to original quaderant ordering

        dft = np.fft.fftshift(dft_filtered)