.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/spatially_adapted_total_variation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_spatially_adapted_total_variation.py: 05. Spatially Adapted Total Variation ===================================== Here a locally adapted regularization is shown. For this purpose the SATV algorithm was implemented. The application and the benefit are shown. .. GENERATED FROM PYTHON SOURCE LINES 10-41 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from matplotlib import image from recon.utils.utils import psnr from recon.interfaces import SATV, Smoothing gt = image.imread("../data/phantom.png") gt = gt/np.max(gt) gt = gt noise_sigma = 0.1*np.max(gt) noisy_image = gt + np.random.normal(0, noise_sigma, size=gt.shape) # TV smoothing small alpha tv_smoothing = Smoothing(domain_shape=gt.shape, reg_mode='tv', alpha=1, lam=8) u_tv = tv_smoothing.solve(data=noisy_image, max_iter=5000, tol=1e-4) f = plt.figure(figsize=(6, 3)) f.add_subplot(1, 2, 1) plt.axis('off') plt.imshow(gt, vmin=0, vmax=np.max(gt)) plt.title("GT - PSNR: "+str(psnr(gt, gt))) f.add_subplot(1, 2, 2) plt.imshow(u_tv, vmin=0, vmax=np.max(gt)) plt.title("TV - PSNR: "+str(psnr(gt, u_tv))) plt.axis('off') plt.show(block=False) .. image:: /tutorials/images/sphx_glr_spatially_adapted_total_variation_001.png :alt: GT - PSNR: -1, TV - PSNR: 29.34 :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Early stopping. .. GENERATED FROM PYTHON SOURCE LINES 42-43 ... .. GENERATED FROM PYTHON SOURCE LINES 43-70 .. code-block:: default satv_obj = SATV(domain_shape=gt.shape, reg_mode='tv', lam=1, alpha=1, plot_iteration=False, noise_sigma=noise_sigma, window_size=10, assessment=noise_sigma*np.sqrt(np.prod(gt.shape))) satv_solution = satv_obj.solve(noisy_image, max_iter=5000, tol=1e-4) f = plt.figure(figsize=(9, 3)) f.add_subplot(1, 3, 1) plt.axis('off') plt.imshow(noisy_image, vmin=0, vmax=np.max(gt)) plt.title("Noisy - PSNR: "+str(psnr(gt, noisy_image))) f.add_subplot(1, 3, 2) plt.imshow(satv_solution, vmin=0, vmax=np.max(gt)) plt.title("SATV - PSNR: "+str(psnr(gt, satv_solution))) plt.axis('off') f.add_subplot(1, 3, 3) plt.imshow(np.reshape(satv_obj.lam, gt.shape)) plt.title("SATV-weight $\lambda$") plt.axis('off') plt.show() .. image:: /tutorials/images/sphx_glr_spatially_adapted_total_variation_002.png :alt: Noisy - PSNR: 19.98, SATV - PSNR: 31.97, SATV-weight $\lambda$ :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0-Iteration of SATV 97.3679717517 25.6 Early stopping. 1-Iteration of SATV 41.8916064116 25.6 Early stopping. 2-Iteration of SATV 26.8865308612 25.6 Early stopping. .. GENERATED FROM PYTHON SOURCE LINES 71-72 Not important -> maybe later. .. GENERATED FROM PYTHON SOURCE LINES 72-101 .. code-block:: default """ lam = 0.3 satv_obj = SATV(domain_shape=image.shape, reg_mode='tgv', lam=lam, plot_iteration=False, tau='auto', alpha=(0.3, 0.6), noise_sigma=noise_sigma, assessment=noise_sigma*np.sqrt(np.prod(image.shape))) satv_solution = satv_obj.solve(noisy_image, max_iter=5000, tol=1e-4) f = plt.figure(figsize=(9, 3)) f.add_subplot(1, 3, 1) plt.gray() plt.axis('off') plt.imshow(noisy_image, vmin=0, vmax=np.max(image)) plt.title("Noisy - PSNR: "+str(psnr(image, noisy_image))) f.add_subplot(1, 3, 2) plt.gray() plt.imshow(satv_solution, vmin=0, vmax=np.max(image)) plt.title("SATGV - PSNR: "+str(psnr(image, satv_solution))) plt.axis('off') f.add_subplot(1, 3, 3) plt.gray() plt.imshow(np.reshape(satv_obj.lam, image.shape)) plt.title("SATGV-weight $\lambda$") plt.axis('off') plt.show() """ .. rst-class:: sphx-glr-script-out Out: .. code-block:: none '\nlam = 0.3\nsatv_obj = SATV(domain_shape=image.shape,\n reg_mode=\'tgv\',\n lam=lam,\n plot_iteration=False,\n tau=\'auto\',\n alpha=(0.3, 0.6),\n noise_sigma=noise_sigma,\n assessment=noise_sigma*np.sqrt(np.prod(image.shape)))\nsatv_solution = satv_obj.solve(noisy_image, max_iter=5000, tol=1e-4)\n\nf = plt.figure(figsize=(9, 3))\nf.add_subplot(1, 3, 1)\nplt.gray()\nplt.axis(\'off\')\nplt.imshow(noisy_image, vmin=0, vmax=np.max(image))\nplt.title("Noisy - PSNR: "+str(psnr(image, noisy_image)))\nf.add_subplot(1, 3, 2)\nplt.gray()\nplt.imshow(satv_solution, vmin=0, vmax=np.max(image))\nplt.title("SATGV - PSNR: "+str(psnr(image, satv_solution)))\nplt.axis(\'off\')\nf.add_subplot(1, 3, 3)\nplt.gray()\nplt.imshow(np.reshape(satv_obj.lam, image.shape))\nplt.title("SATGV-weight $\\lambda$")\nplt.axis(\'off\')\nplt.show()\n' .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 44.853 seconds) .. _sphx_glr_download_tutorials_spatially_adapted_total_variation.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: spatially_adapted_total_variation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spatially_adapted_total_variation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_