def make_blur_matrix(img, kernel_size=12, debug=True): n = kernel_size # k2 = np.zeros(shape=(n,n)) # k2[n//2,n//2] = 1 # sigma = kernel_size/5.0 ## 2.5 sigma # testk=scipy.ndimage.gaussian_filter(k2,sigma) ## already normalized r = n // 3 P = fpm.create_source_pattern(shape='circle', ledmat_shape=[n, n], radius=r) S1 = fpm.create_source_pattern(shape='semicircle', angle=0, ledmat_shape=[n, n], radius=r) # S2 = fpm.create_source_pattern(shape='semicircle', angle=180, ledmat_shape=[n, n], radius=r) # Hph = 1j*(signal.correlate2d(P, S1*P)-signal.correlate2d(P, S2*P)) # Hph /= np.pi*np.max(np.imag(Hph)) H = fftshift(fft2(fftshift(P))) H_real = np.real(H) H_imag = np.imag(H) if (debug): plt.imshow(fftshift(P)) plt.show() # plotkernel(np.abs(Hph), n) b_real = make_kernel_2D(H_real, img.shape) b_imag = make_kernel_2D(H_imag, img.shape) return b_real, b_imag
def get_dpc_reconstruction(color, angles): image_dict = np.load(get_color_file(color)) image_list = [] pupil_list = [] try: image_blanks = np.load(get_color_background(color)) except: image_blanks = np.zeros((1900, 1900)) image_blanks = image_blanks = np.load(get_color_background('R')) # Get a list of background corrected images for angle in angles: image = image_dict[()][angle] blank = image_blanks[()][angle] image_corr = image-.3*blank image_corr -= np.min(image_corr) # image_corr = image image_list.append(image_corr) pupil_list.append(fpm.create_source_pattern(shape='semicircle', angle=angle, ledmat_shape=[1900, 1900], radius=600, int_radius=60)) # Apply the DPC reconstructon to that list dpc_total = np.zeros_like(image) for j in range(2): dpc_total += st.dpc_difference(image_list[i], image_list[i+1]) return dpc_total
def make_blur_matrix(img, kernel_size=12, debug=True): n = kernel_size # k2 = np.zeros(shape=(n,n)) # k2[n//2,n//2] = 1 # sigma = kernel_size/5.0 ## 2.5 sigma # testk=scipy.ndimage.gaussian_filter(k2,sigma) ## already normalized r = n // 3 P = fpm.create_source_pattern(shape='circle', ledmat_shape=[n, n], radius=r) # S1 = fpm.create_source_pattern(shape='semicircle', angle=0, ledmat_shape=[n, n], radius=r) # S2 = fpm.create_source_pattern(shape='semicircle', angle=180, ledmat_shape=[n, n], radius=r) # Hph = 1j*(signal.correlate2d(P, S1*P)-signal.correlate2d(P, S2*P)) # Hph /= np.pi*np.max(np.imag(Hph)) Hph = fftshift(fft2(P)) Hph = np.arange(1, n**2 + 1).reshape(n, -1) * (1 + 2 * 1j) mydebug("kernel: ", Hph) # Hph = Hph if (debug): plt.imshow(np.abs(Hph)) # plt.show() # plotkernel(np.abs(Hph), n) blurmat = make_kernel_2D(Hph, img.shape) mydebug("Convolution matrix: ", blurmat.toarray()) return (blurmat)
def create_hph(angle1, angle2, npx, radius): radius = radius // 2 n = npx // 2 + 1 P = fpm.create_source_pattern(shape='circle', ledmat_shape=[n, n], radius=radius) S1 = fpm.create_source_pattern(shape='semicircle', angle=angle1, ledmat_shape=[n, n], radius=radius) S2 = fpm.create_source_pattern(shape='semicircle', angle=angle2, ledmat_shape=[n, n], radius=radius) Hph = 1j * (signal.correlate2d(P, S1 * P) - signal.correlate2d(P, S2 * P)) Hph /= np.pi * np.max(np.imag(Hph)) return Hph[0:npx, 0:npx]
def mencoded(angle): matrix = fpm.create_source_pattern(shape='semicircle', angle=angle, int_radius=2, radius=5,) pattern = fpm.hex_encode(matrix) return pattern
# xoff=1250, yoff=950 def acquire_image_pattern(ss, pattern, Nmean=1): image_mean = np.zeros(cfg.patch_size) for i in range(Nmean): image_response = client.acquire_ledmatrix_pattern(pattern=pattern, power=255, color='R', shutter_speed=ss, iso=400, xoff=0, yoff=0) image_i = np.array(image_response).reshape(cfg.patch_size) image_mean += image_i return image_mean/(Nmean) def mencoded(angle): matrix = fpm.create_source_pattern(shape='semicircle', angle=angle, int_radius=2, radius=5,) pattern = fpm.hex_encode(matrix) return pattern circ_matrix = fpm.create_source_pattern(shape='circle') N = 1 # angles = [0, 180, 90, 270, 45, 225, 135, 315] angles = [0, 0] pupil_list = [] image_dict = dict() fig, (ax) = plt.subplots(1, 1, figsize=(25, 15)) fig.show() fig.canvas.draw() for angle in angles: image_dict[angle] = acquire_image_pattern(ss=1E4, pattern=mencoded(angle=angle), Nmean=N) ax.cla() ax.imshow(image_dict[angle], cmap=cm.gray) fig.canvas.draw()
elapsed2 = time.time() - t2 print("Time: %2f s constructing the matrix ; %2f s solving it\n" % (elapsed1, elapsed2)) return deblurred npx = 100 radius = 60 mag_array = scipy.misc.imread('sandbox/alambre.png', 'F')[:npx, :npx] image_phase = scipy.misc.imread('sandbox/lines0_0.png', 'F')[:npx, :npx] ph_array = np.pi * (image_phase) / np.amax(image_phase) image_array = mag_array * np.exp(1j * ph_array) image_fft = fftshift(fft2(image_array)) b = make_complex_blur_matrix(image_array, kernel_size=12, debug=True) I1 = blur_noise_image(image_array, b, noise_scale=0.0) N = np.prod(I1.shape) deblur_tikh = deblur_tikh_sparse(I1, b, 0.025, method='Lap') fig, (axes) = plt.subplots(2, 2, figsize=(25, 15)) fig.show() # S1 = fpm.create_source_pattern(shape='semicircle', angle=180, ledmat_shape=[npx, npx], radius=npx//3) S1 = fpm.create_source_pattern(shape='circle', angle=180, ledmat_shape=[npx, npx], radius=npx // 6) Ilp1 = np.abs(ifft2(S1 * image_fft)) axes[0][0].imshow(np.abs(I1)) axes[0][1].imshow(np.abs(deblur_tikh)) plt.show()
npx = 32 radius = 10 mag_array = misc.imread('sandbox/alambre.png', 'F')[:npx, :npx] image_phase = misc.imread('sandbox/lines0_0.png', 'F')[:npx, :npx] ph_array = np.pi * (image_phase) / np.amax(image_phase) image_array = mag_array * np.exp(1j * ph_array) image_fft = fftshift(fft2(image_array)) angles = [0, 180, 90, 270, 45, 225, 135, 315] pupil_list = [] image_list = [] for angle in angles: H = (fpm.create_source_pattern(shape='semicircle', angle=angle, ledmat_shape=[npx, npx], radius=radius, int_radius=20)) pupil_list.append(H) image_list.append(np.abs(ifft2(H * image_fft))) fig, (axes) = plt.subplots(2, 2, figsize=(25, 15)) fig.show() # IDPC2 = np.abs(ifft2(H2*image_fft)) axes[0][0].imshow(pupil_list[0] + pupil_list[2]) axes[0][1].imshow(pupil_list[1] + pupil_list[3]) signal.correlate2d(in1, in2, mode='full', boundary='fill', fillvalue=0) plt.show()
for idpc in image_list: fft_list.append(fftshift(fft2(idpc))) for pupil in pupil_list: H = pupil / (sum_idpc) H_list.append(np.conjugate(H)) tik_den = np.sum([np.abs(H.ravel())**2 for H in H_list]) + alpha print(tik_den) tik_nom = 0 for HC, FFT_IDPC in zip(H_list, fft_list): tik_nom += HC * FFT_IDPC tik = (ifft2(tik_nom / tik_den)) return tik P = fpm.create_source_pattern(shape='circle', ledmat_shape=[npx, npx], radius=radius) S1 = fpm.create_source_pattern(shape='semicircle', angle=0, ledmat_shape=[npx, npx], radius=radius) S2 = fpm.create_source_pattern(shape='semicircle', angle=180, ledmat_shape=[npx, npx], radius=radius) S3 = fpm.create_source_pattern(shape='semicircle', angle=90, ledmat_shape=[npx, npx], radius=radius) S4 = fpm.create_source_pattern(shape='semicircle', angle=270,