def myFiniteFractal2(N, K, sortBy=lambda p, q: abs(p) + abs(q), twoQuads=True, centered=False): fareyVectors = farey.Farey() fareyVectors.compactOn() fareyVectors.generate(N, octants=1) # Just give me one octant fareyVectors.generateFiniteWithCoverage(N) #sort to reorder result for prettier printing finiteAnglesSorted, anglesSorted = fareyVectors.sortCustom(sortBy) ## Rotate the whole fractal #anglesSorted = [angle * P2R(1, 63) for angle in anglesSorted] #for index in range(len(anglesSorted)): # angle = anglesSorted[index] # p = np.imag(angle) # q = np.real(angle) # p = p + N if p < 0 else p # q = q + N if q < 0 else q # anglesSorted[index] = q + 1j * p #print(anglesSorted) #finiteAnglesSorted = [farey.toFinite(angle, N) for angle in anglesSorted] kSpace = np.zeros((N, N)) lines, angles, mValues = finite.computeKatzLines(kSpace, anglesSorted, finiteAnglesSorted, K, centered, twoQuads) mu = len(lines) print("Number of finite lines in fractal:", mu) samplesImage1 = np.zeros((N, N), np.float32) for line in lines: u, v = line for x, y in zip(u, v): # Rotate #[x, y] = rotate(30, np.array([[x], [y]])) #x = int(x) % N #y = int(y) % N samplesImage1[x, y] += 1 #determine oversampling because of power of two size #this is fixed for choice of M and m values oversamplingFilter = np.zeros((N, N), np.uint32) onesSlice = np.ones(N, np.uint32) for m in mValues: radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter == 0] = 1 samplesImage1 /= oversamplingFilter # samplesImage1 = fftpack.fftshift(samplesImage1) return lines, angles, mValues, samplesImage1, oversamplingFilter
def fullySampledFilter(N): if N % 2 == 0: p = N + int(N / 2) else: p = N + 1 oversamplingFilter = np.zeros((N, N), np.complex) onesSlice = np.ones(N, np.uint32) for m in range(0, p): radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter == 0] = 1 return oversamplingFilter
def ifrt_complex(bins, N, norm=True, center=False, Isum=-1, mValues=None, oversampleFilter=None): ''' Compute the inverse DRT in O(n logn) complexity using the discrete Fourier slice theorem and the FFT. Input should be DRT projections (bins) to recover an NxN image, where N is prime. projNumber is the number of non-zero projections in bins. This useful for backprojecting mu projections where mu < N. Isum is computed from first row if -1, otherwise provided value is used Finite angles not in mValues are ignored Coefficients are filtered using the exact oversampling filter if provided ''' if Isum < 0: Isum = bins[0, :].sum() # print "ISUM:", Isum dftSpace = np.zeros((N, N), dtype=np.complex) p = 0 if N % 2 == 0: p = 2 #Set slices (0 <= m <= N) for k, row in enumerate(bins): #iterate per row if mValues and k not in mValues: continue slice = fftpack.fft(row) radon.setSlice(k, dftSpace, slice, p) #exact filter (usually for non-prime sizes) # print("Oversampling Shape:", oversampleFilter.shape) if oversampleFilter is not None: # print("OVERSAMPLING!") dftSpace /= oversampleFilter else: dftSpace[0, 0] -= float(Isum) * N #iFFT 2D image result = fftpack.ifft2(dftSpace) if not norm: result *= N #ifft2 already divides by N**2 if center: result = fftpack.ifftshift(result) return result
def finiteFractal(N, K, sortBy='Euclidean', twoQuads=True, centered=False): ''' Create the finite fractal for image size N given the Katz criterion K sortBy can be 'Euclidean' for L2 norm or 'length' for L1 norm twoQuads can be used to cover the half plane Returns lines, angles, mValues, fractal formed (as an image), oversampling filter if applicable ''' fareyVectors = farey.Farey() fareyVectors.compactOn() fareyVectors.generateFiniteWithCoverage(N) #sort to reorder result for prettier printing finiteAnglesSorted, anglesSorted = fareyVectors.sort(sortBy) kSpace = np.zeros((N,N)) print("N: ", N) lines, angles, mValues = computeKatzLines(kSpace, anglesSorted, finiteAnglesSorted, K, centered, twoQuads) mu = len(lines) print("Number of finite lines in fractal:", mu) samplesImage1 = np.zeros((N,N), np.float32) for line in lines: u, v = line for x, y in zip(u, v): samplesImage1[x, y] += 1 #determine oversampling because of power of two size #this is fixed for choice of M and m values oversamplingFilter = np.zeros((N,N), np.uint32) onesSlice = np.ones(N, np.uint32) for m in mValues: radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter==0] = 1 samplesImage1 /= oversamplingFilter # samplesImage1 = fftpack.fftshift(samplesImage1) return lines, angles, mValues, samplesImage1, oversamplingFilter
def finiteFractal(N, K, sortBy='Euclidean', twoQuads=True, centered=False): ''' Create the finite fractal for image size N given the Katz criterion K sortBy can be 'Euclidean' for L2 norm or 'length' for L1 norm twoQuads can be used to cover the half plane Returns lines, angles, mValues, fractal formed (as an image), oversampling filter if applicable ''' fareyVectors = farey.Farey() fareyVectors.compactOn() fareyVectors.generateFiniteWithCoverage(N) #sort to reorder result for prettier printing finiteAnglesSorted, anglesSorted = fareyVectors.sort(sortBy) kSpace = np.zeros((N,N)) lines, angles, mValues = computeKatzLines(kSpace, anglesSorted, finiteAnglesSorted, K, centered, twoQuads) mu = len(lines) print("Number of finite lines in fractal:", mu) #i = 0 samplesImage1 = np.zeros((N,N), np.float32) print(angles) for i in range(0, N-1): for angle in angles: #print(line) samplesImage1[int((np.real(angle) * i)) % N, (int(np.imag(angle) * i)) % N] = 1 samplesImage1[int((np.real(angle) * -i)) % N, (int(np.imag(angle) * -i)) % N] = 1 #for line in lines: # u, v = line # for x, y in zip(u, v): # samplesImage1[x, y] += 1 # NEW BIT # a colormap and a normalization instance cmap = plt.cm.gray norm = plt.Normalize(vmin=samplesImage1.min(), vmax=samplesImage1.max()) # map the normalized data to colors # image is now RGBA (512x512x4) image = cmap(norm(fftpack.fftshift(samplesImage1))) # save the image #plt.imsave("tmp{}.png".format(i), image) #plt.imshow(fftpack.fftshift(samplesImage1), cmap = 'gray') #plt.tick_params( # axis='both', # changes apply to the x-axis # which='both', # both major and minor ticks are affected # bottom=False, # ticks along the bottom edge are off # top=False, # ticks along the top edge are off # labelbottom=False) # labels along the bottom edge are off #plt.show() #samplesImage1 = np.zeros((N,N), np.float32) #i += 1 #plt.imshow(fftpack.fftshift(samplesImage1), cmap = 'gray') #plt.show() #determine oversampling because of power of two size #this is fixed for choice of M and m values oversamplingFilter = np.zeros((N,N), np.uint32) onesSlice = np.ones(N, np.uint32) for m in mValues: radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter==0] = 1 samplesImage1 /= oversamplingFilter # samplesImage1 = fftpack.fftshift(samplesImage1) return lines, angles, mValues, samplesImage1, oversamplingFilter
def farey_fractal(N, F, twoQuads=True, centered=False): ''' Create finite fractal for image size N given Farey order F (creates square fractal) Returns lines, angles, mValues, fractal formed (as an image), R (reduction) ''' # Generate farey_sequence of order N octAngles = np.array(farey_sequence(F), dtype=np.complex) # Get dimensions of farey vector collection [l] = octAngles.shape # Create array to store farey vectors in the half-plane angles = np.zeros(l * 2, dtype=np.complex) for i in range(0, 2): if i == 0: angles[int(l * i):int(l * (i + 1))] = octAngles elif i == 1: angles[int(l * i):int(l * (i + 1))] = octAngles.imag + 1j * octAngles.real # Due to symmetry we will get more than one of the same vector angles = np.unique(angles) # Generate kSpace grid fractal = np.zeros((N, N)) # Compute lines and mValues lines, mValues = finite.computeLines(fractal, angles) mValues = np.unique(mValues) # Compute extra lines for mValues > N lines, angles, mValues = finite.computeKatzLines(fractal, angles, mValues, 60, twoQuads=twoQuads, centered=centered) lines = np.array(lines) # Obtain unique lines and mValues newM = [] indexStore = [] for i, m in enumerate(mValues): if m not in newM: newM.append(m) indexStore.append(i) mValues = newM lines = lines[indexStore, :, :].tolist() mu = len(lines) print("Number of finite lines in fractal: ", mu) # Paint fractal onto kSpace grid for line in lines: u, v = line for x, y in zip(u, v): fractal[x, y] = 1 R = np.sum(np.sum(fractal)) / (N * N) # This has the number of times a point is sampled oversamplingFilter = np.zeros((N, N), np.complex) onesSlice = np.ones(N, np.uint32) for m in mValues: radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter == 0] = 1 return lines, angles, mValues, fractal, R, oversamplingFilter
def myFiniteFractal(N, K, sortBy=lambda p, q: abs(p) + abs(q), twoQuads=True, centered=False, pure=False): ''' Create the finite fractal for image size N given the Katz criterion K sortBy can be 'Euclidean' for L2 norm or 'length' for L1 norm twoQuads can be used to cover the half plane Returns lines, angles, mValues, fractal formed (as an image), oversampling filter if applicable ''' fareyVectors = farey.Farey() fareyVectors.compactOn() fareyVectors.generateFiniteWithCoverage(N) #plt.scatter(np.real(fareyVectors.vectors), np.imag(fareyVectors.vectors)) #plt.show() #fareyVectors.generateFinite(N) #print(fareyVectors.vectors) vecs = fareyVectors.vectors vecs_2q = [farey.farey(-np.imag(vec), np.real(vec)) for vec in vecs] fareyVectors.vectors.extend(vecs_2q) fareyVectors.generateFinite(N) #fareyVectors.vectors = filter(is_gaussian_prime, fareyVectors.vectors) #sort to reorder result for prettier printing finiteAnglesSorted, anglesSorted = fareyVectors.sortCustom(sortBy) #finiteAnglesSorted = [finiteAnglesSorted[20]] #anglesSorted = [anglesSorted[20]] ## Rotate the whole fractal #anglesSorted = [angle * P2R(1, 63) for angle in anglesSorted] #for index in range(len(anglesSorted)): # angle = anglesSorted[index] # p = np.imag(angle) # q = np.real(angle) # p = p + N if p < 0 else p # q = q + N if q < 0 else q # anglesSorted[index] = q + 1j * p #print(anglesSorted) #finiteAnglesSorted = [farey.toFinite(angle, N) for angle in anglesSorted] kSpace = np.zeros((N, N)) lines, angles, mValues = finite.computeKatzLines(kSpace, anglesSorted, finiteAnglesSorted, K, centered, twoQuads=False) mu = len(lines) print("Number of finite lines in fractal:", mu) samplesImage1 = np.zeros((N, N), np.float32) for line in lines: u, v = line for x, y in zip(u, v): # Rotate #[x, y] = rotate(30, np.array([[x], [y]])) #x = int(x) % N #y = int(y) % N samplesImage1[x, y] += 1 #determine oversampling because of power of two size #this is fixed for choice of M and m values oversamplingFilter = np.zeros((N, N), np.uint32) onesSlice = np.ones(N, np.uint32) for m in mValues: radon.setSlice(m, oversamplingFilter, onesSlice, 2) oversamplingFilter[oversamplingFilter == 0] = 1 samplesImage1 /= oversamplingFilter # samplesImage1 = fftpack.fftshift(samplesImage1) return lines, angles, mValues, samplesImage1, oversamplingFilter