{"id":20894084,"url":"https://github.com/gpbonillas/lab_image_enhancement","last_synced_at":"2026-04-26T19:31:29.486Z","repository":{"id":242445922,"uuid":"809566405","full_name":"gpbonillas/lab_image_enhancement","owner":"gpbonillas","description":"Mejora de detalles en imágenes","archived":false,"fork":false,"pushed_at":"2024-06-03T03:50:17.000Z","size":29668,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-18T09:47:55.276Z","etag":null,"topics":["colab","colab-notebook","jupyter-notebook","python3","skimage"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gpbonillas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-03T02:42:06.000Z","updated_at":"2024-06-03T03:51:24.000Z","dependencies_parsed_at":"2024-06-03T04:41:04.753Z","dependency_job_id":null,"html_url":"https://github.com/gpbonillas/lab_image_enhancement","commit_stats":null,"previous_names":["gpbonillas/lab_image_enhancement"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gpbonillas/lab_image_enhancement","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbonillas%2Flab_image_enhancement","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbonillas%2Flab_image_enhancement/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbonillas%2Flab_image_enhancement/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbonillas%2Flab_image_enhancement/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gpbonillas","download_url":"https://codeload.github.com/gpbonillas/lab_image_enhancement/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbonillas%2Flab_image_enhancement/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32310804,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T19:15:34.056Z","status":"ssl_error","status_checked_at":"2026-04-26T19:15:15.467Z","response_time":129,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["colab","colab-notebook","jupyter-notebook","python3","skimage"],"created_at":"2024-11-18T10:18:09.001Z","updated_at":"2026-04-26T19:31:29.471Z","avatar_url":"https://github.com/gpbonillas.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laboratorio para mejorar imágenes usando skimage: Operaciones elementales.\n\nPara este ejercicio hemos usado las imagenes 33, 77, 333 y 777 del dataset [The Dark Face](https://www.kaggle.com/datasets/soumikrakshit/dark-face-dataset). \n\n## Tecnología usadas: \n- Jupyter Notebook\n- Python\n- skimage\n## Funciones aplicadas: \nPrincipalmente, nos centraremos en las siguientes técnicas:\n\n- Funciones de transformación o ajuste de la intensidad.\n- Procesamiento del histograma.\n- Operadores aritméticos.\n\n\n\n\n``` python\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom skimage import io, img_as_float, img_as_ubyte\nfrom skimage import exposure\nfrom skimage import util\nfrom skimage import filters\nfrom skimage.filters.rank import median\nfrom skimage.filters.rank import mean\nfrom skimage.color import rgb2gray\nfrom skimage.color import rgb2yuv\nfrom skimage.morphology import disk, ball\nfrom skimage.exposure import is_low_contrast\n\n\nmatplotlib.rcParams['font.size'] = 8\n```\n\n``` python\ndef plot_img_and_hist(image, axes, bins=256):\n    \"\"\"Plot an image along with its histogram and cumulative histogram.\n\n    \"\"\"\n    image = img_as_float(image)\n    ax_img, ax_hist = axes\n    ax_cdf = ax_hist.twinx()\n\n    # Display image\n    ax_img.imshow(image, cmap=plt.cm.gray)\n    ax_img.set_axis_off()\n\n    # Display histogram\n    ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black')\n    ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))\n    ax_hist.set_xlabel('Pixel intensity')\n    ax_hist.set_xlim(0, 1)\n    ax_hist.set_yticks([])\n\n    # Display cumulative distribution\n    img_cdf, bins = exposure.cumulative_distribution(image, bins)\n    ax_cdf.plot(bins, img_cdf, 'r')\n    ax_cdf.set_yticks([])\n\n    return ax_img, ax_hist, ax_cdf\n```\n\n``` python\n# Load an example image\nimg33 = img_as_float(io.imread(\"33.png\"))\nimg333 = img_as_float(io.imread(\"333.png\"))\nimg77 = img_as_float(io.imread(\"77.png\"))\nimg777 = img_as_float(io.imread(\"777.png\"))\n\n#image2 = io.imread(\"images/test_image.jpg\").astype(np.float)\n#avoid using astype as it violates assumptions about dtype range.\n#for example float should range from 0 to 1 (or -1 to 1) but if you use\n#astype to convert to float, the values do not lie between 0 and 1.\nplt.imshow(img33)\n```\n\n![](/images/1.png)\n\n``` python\nimg33.shape\n```\n\u003e (720, 1080, 3)\n\n\n# FUNCIONES DE TRANSFORMACIÓN O AJUSTE DE LA INTENSIDAD\n\n``` python\n# Gamma (0.10)\ngamma_corrected_01 = exposure.adjust_gamma(img33, 0.10)\n\n# Gamma (0.20)\ngamma_corrected_02 = exposure.adjust_gamma(img33, 0.20)\n\n# Gamma (0.30)\ngamma_corrected_03 = exposure.adjust_gamma(img33, 0.30)\n\n# Gamma (0.40)\ngamma_corrected_04 = exposure.adjust_gamma(img33, 0.40)\n\n# Gamma (0.50)\ngamma_corrected_05 = exposure.adjust_gamma(img33, 0.50)\n\n# Display results\nfig = plt.figure(figsize=(18, 6))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 7))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected_01, axes[:, 1])\nax_img.set_title('Gamma  k=0.10')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected_02, axes[:, 2])\nax_img.set_title('Gamma  k=0.20')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected_03, axes[:, 3])\nax_img.set_title('Gamma  k=0.30')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected_04, axes[:, 4])\nax_img.set_title('Gamma  k=0.40')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected_05, axes[:, 5])\nax_img.set_title('Gamma  k=0.50')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 7))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/2.png)\n\n``` python\n# Logarithmic (k=3)\nlogarithmic_corrected_3 = exposure.adjust_log(img33, 3)\n\n# Logarithmic (k=10)\nlogarithmic_corrected_10 = exposure.adjust_log(img33, 10)\n\n# Logarithmic (k=20)\nlogarithmic_corrected_20 = exposure.adjust_log(img33, 20)\n\n# Logarithmic (k=25)\nlogarithmic_corrected_25 = exposure.adjust_log(img33, 25)\n\n# Logarithmic (k=40)\nlogarithmic_corrected_40 = exposure.adjust_log(img33, 40)\n\n# Display results\nfig = plt.figure(figsize=(18, 6))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 7))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected_3, axes[:, 1])\nax_img.set_title('Logarithmic k=3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected_10, axes[:, 2])\nax_img.set_title('Logarithmic k=10')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected_20, axes[:, 3])\nax_img.set_title('Logarithmic k=20')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected_25, axes[:, 4])\nax_img.set_title('Logarithmic k=25')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected_40, axes[:, 5])\nax_img.set_title('Logarithmic k=40')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 7))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/3.png)\n\n### **Los mejores resultados para las funciones Gamma y Log son:**\n\n``` python\n# Gamma\ngamma_corrected = exposure.adjust_gamma(img33, 0.3)\n\n# Logarithmic\nlogarithmic_corrected = exposure.adjust_log(img33, 25)\n\n# Display results\nfig = plt.figure(figsize=(12, 6))\naxes = np.zeros((2, 3), dtype=object)\naxes[0, 0] = plt.subplot(2, 3, 1)\naxes[0, 1] = plt.subplot(2, 3, 2, sharex=axes[0, 0], sharey=axes[0, 0])\naxes[0, 2] = plt.subplot(2, 3, 3, sharex=axes[0, 0], sharey=axes[0, 0])\naxes[1, 0] = plt.subplot(2, 3, 4)\naxes[1, 1] = plt.subplot(2, 3, 5)\naxes[1, 2] = plt.subplot(2, 3, 6)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 5))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected, axes[:, 1])\nax_img.set_title('Gamma correction (k=0.3)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected, axes[:, 2])\nax_img.set_title('Logarithmic correction (k=25)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 5))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/4.png)\n\n# PROCESAMIENTO DEL HISTOGRAMA\n\n``` python\n# Contrast stretching\np2, p98 = np.percentile(img33, (2, 98))\nimg_rescale2_98 = exposure.rescale_intensity(img33, in_range=(p2, p98))\n\n# Contrast stretching\np5, p95 = np.percentile(img33, (5, 95))\nimg_rescale5_95 = exposure.rescale_intensity(img33, in_range=(p5, p95))\n\n# Contrast stretching\np10, p90 = np.percentile(img33, (10, 90))\nimg_rescale10_90 = exposure.rescale_intensity(img33, in_range=(p10, p90))\n\n# Display results\nfig = plt.figure(figsize=(15, 8))\naxes = np.zeros((2, 4), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 4, 1)\nfor i in range(1, 4):\n    axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 4):\n    axes[1, i] = fig.add_subplot(2, 4, 5+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 5))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale2_98, axes[:, 1])\nax_img.set_title('Contrast stretching 2-98')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale5_95, axes[:, 2])\nax_img.set_title('Contrast stretching 5-95')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale10_90, axes[:, 3])\nax_img.set_title('Contrast stretching 10-90')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 5))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/5.png)\n\n``` python\n# Equalization\nimg_eq = exposure.equalize_hist(img33)\n\n# Equalization 2-98\nimg_eq298 = exposure.equalize_hist(img_rescale2_98)\n\n# Equalization 5-95\nimg_eq595 = exposure.equalize_hist(img_rescale5_95)\n\n# Equalization 10-90\nimg_eq1090 = exposure.equalize_hist(img_rescale10_90)\n\n# Display results\nfig = plt.figure(figsize=(15, 8))\naxes = np.zeros((2, 5), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 5, 1)\nfor i in range(1, 5):\n    axes[0, i] = fig.add_subplot(2, 5, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 5):\n    axes[1, i] = fig.add_subplot(2, 5, 6+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 1])\nax_img.set_title('Histogram equalization')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq298, axes[:, 2])\nax_img.set_title('Histogram equalization (2-98%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq595, axes[:, 3])\nax_img.set_title('Histogram equalization (5-95%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq1090, axes[:, 4])\nax_img.set_title('Histogram equalization (10-90%)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/6.png)\n\n``` python\n# Adaptive Equalization (clip_limit=0.01)\nimg33_clahe_001 = exposure.equalize_adapthist(img33, clip_limit=0.01)\n\n# Adaptive Equalization (clip_limit=0.03)\nimg33_clahe_003 = exposure.equalize_adapthist(img33, clip_limit=0.03)\n\n# Adaptive Equalization (clip_limit=0.1)\nimg33_clahe_01 = exposure.equalize_adapthist(img33, clip_limit=0.1)\n\n# Adaptive Equalization (clip_limit=0.2)\nimg33_clahe_02 = exposure.equalize_adapthist(img33, clip_limit=0.2)\n\n# Adaptive Equalization (clip_limit=0.5)\nimg33_clahe_05 = exposure.equalize_adapthist(img33, clip_limit=0.5)\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 7))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_clahe_001, axes[:, 1])\nax_img.set_title('CLAHE Original (clip_limit=0.01)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_clahe_003, axes[:, 2])\nax_img.set_title('CLAHE Original (clip_limit=0.03)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_clahe_01, axes[:, 3])\nax_img.set_title('CLAHE Original (clip_limit=0.1)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_clahe_02, axes[:, 4])\nax_img.set_title('CLAHE Original (clip_limit=0.2)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_clahe_05, axes[:, 5])\nax_img.set_title('CLAHE Original (clip_limit=0.5)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 7))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/7.png)\n\n**Nos damos cuenta que el valor para el parámetro *clip_limit* que mejores\nresultados arroja es: 0.2**\n\n``` python\n# Adaptive Equalization\nimg_clahe = exposure.equalize_adapthist(img33, clip_limit=0.2)\n\n# Adaptive Equalization (Rescale 2-98%)\nimg_clahe_298 = exposure.equalize_adapthist(img_rescale2_98, clip_limit=0.2)\n\n# Adaptive Equalization (Rescale 5-95%)\nimg_clahe_595 = exposure.equalize_adapthist(img_rescale5_95, clip_limit=0.2)\n\n# Adaptive Equalization (Rescale 10-90%)\nimg_clahe_1090 = exposure.equalize_adapthist(img_rescale10_90, clip_limit=0.2)\n\n# Display results\nfig = plt.figure(figsize=(15, 8))\naxes = np.zeros((2, 5), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 5, 1)\nfor i in range(1, 5):\n    axes[0, i] = fig.add_subplot(2, 5, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 5):\n    axes[1, i] = fig.add_subplot(2, 5, 6+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_clahe, axes[:, 1])\nax_img.set_title('Adaptive equalization')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_clahe_298, axes[:, 2])\nax_img.set_title('Adaptive equalization (Rescale 2-98%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_clahe_595, axes[:, 3])\nax_img.set_title('Adaptive equalization (Rescale 5-95%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_clahe_1090, axes[:, 4])\nax_img.set_title('Adaptive equalization (Rescale 10-99%)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/8.png)\n\n``` python\n# Adaptive Equalization\nimg_adapteq = exposure.equalize_adapthist(img33, clip_limit=0.2)\n\n# Adaptive Equalization (Gamma=0.1)\nimg_adapteq_gamma_01 = exposure.equalize_adapthist(gamma_corrected_01, clip_limit=0.2)\n\n# Adaptive Equalization (Gamma=0.2)\nimg_adapteq_gamma_02 = exposure.equalize_adapthist(gamma_corrected_02, clip_limit=0.2)\n\n# Adaptive Equalization (Gamma=0.3)\nimg_adapteq_gamma_03 = exposure.equalize_adapthist(gamma_corrected_03, clip_limit=0.2)\n\n# Gamma (0.20)\n#gamma_corrected_02 = exposure.adjust_gamma(img33, 0.20)\n\n# Display results\nfig = plt.figure(figsize=(15, 8))\naxes = np.zeros((2, 5), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 5, 1)\nfor i in range(1, 5):\n    axes[0, i] = fig.add_subplot(2, 5, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 5):\n    axes[1, i] = fig.add_subplot(2, 5, 6+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 1])\nax_img.set_title('Adaptive equalization')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq_gamma_01, axes[:, 2])\nax_img.set_title('Adaptive equalization (Gamma=0.1)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq_gamma_02, axes[:, 3])\nax_img.set_title('Adaptive equalization (Gamma=0.2)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq_gamma_03, axes[:, 4])\nax_img.set_title('Adaptive equalization (Gamma=0.3)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/9.png)\n\n### **MEJORES RESULTADOS CON LAS TÉNICAS DE PROCESAMIENTO DEL HISTOGRAMA**\n\n``` python\n# Contrast stretching\np10, p90 = np.percentile(img33, (10, 90))\nimg_rescale_1090 = exposure.rescale_intensity(img33, in_range=(p10, p90))\n\n# Equalization Original\nimg33_eq = exposure.equalize_hist(img33)\n\n# Equalization Rescale\nimg_rescale_eq_1090 = exposure.equalize_hist(img_rescale_1090)\n\n# Adaptive Equalization Original\nimg_adapteq = exposure.equalize_adapthist(img33, clip_limit=0.2)\n\n# Adaptive Equalization (Gamma=0.3)\nimg_adapteq_gamma = exposure.equalize_adapthist(gamma_corrected, clip_limit=0.2)\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Low contrast image')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 7))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale_1090, axes[:, 1])\nax_img.set_title('Contrast stretching (10-90%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_eq, axes[:, 2])\nax_img.set_title('Histogram equalization Original')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale_eq_1090, axes[:, 3])\nax_img.set_title('Histogram equalization (Rescale 10-90%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 4])\nax_img.set_title('CLAHE Original (clip_limit=0.2)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq_gamma, axes[:, 5])\nax_img.set_title('CLAHE (Gamma=0.3)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 7))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/10.png)\n\n# OPERACIONES MATEMÁTICAS\n``` python\nconstante = 0.2\nk = 2.5\n\n# SUMA BLOQUE 1\nimg33_add = img33 + img33\nimg33_add_gamma03 = gamma_corrected_03 + gamma_corrected_03\nimg33_add_log25 = logarithmic_corrected_25 + logarithmic_corrected_25\nimg33_add_rescale1090 = img_rescale10_90 + img_rescale10_90\nimg33_add_adapte_02 = img_adapteq + img_adapteq # Perfecta\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add, axes[:, 1])\nax_img.set_title('Suma Iguales')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_gamma03, axes[:, 2])\nax_img.set_title('Suma Gamma=0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_log25, axes[:, 3])\nax_img.set_title('Suma Log=25')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_rescale1090, axes[:, 4])\nax_img.set_title('Suma Rescale (10-90%)')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapte_02, axes[:, 5])\nax_img.set_title('Suma Ig. CLAHE Optimizada')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/11.png)\n\n``` python\n# SUMA BLOQUE 2\nimg33_add_eq = img_eq + img_eq\nimg33_add_adapteq_gamma_03 = img_adapteq_gamma_03 + img_adapteq_gamma_03\nimg33_add_adapteq298 = img_clahe_298 + img_clahe_298\nimg33_add_adapteq_gamma03 = img_adapteq + img33_add_gamma03\nimg33_add_clahe_opt_k = img_adapteq + constante\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_eq, axes[:, 1])\nax_img.set_title('Suma Histogram Equalization')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapteq_gamma_03, axes[:, 2])\nax_img.set_title('Suma CLAHE Gamma=0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapteq298, axes[:, 3])\nax_img.set_title('Suma CLAHE Rescale 2-98%')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapteq_gamma03, axes[:, 4])\nax_img.set_title('Suma CLAHE Optimizada + Gamma 0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_clahe_opt_k, axes[:, 5])\nax_img.set_title('Suma CLAHE Optimizado + k=0.2')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/12.png)\n\n### **MEJORES RESULTADOS CON LAS TÉNICAS DE OPERADORES MATEMÁTICOS (SUMA)**\n\n``` python\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_gamma03, axes[:, 1])\nax_img.set_title('Suma Gamma=0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapte_02, axes[:, 2])\nax_img.set_title('Suma Ig. CLAHE Optimizada')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapteq298, axes[:, 3])\nax_img.set_title('Suma CLAHE Rescale 2-98%')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_adapteq_gamma03, axes[:, 4])\nax_img.set_title('Suma CLAHE Optimizada + Gamma 0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_add_clahe_opt_k, axes[:, 5])\nax_img.set_title('Suma CLAHE Optimizado + k=0.2')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/13.png)\n\n``` python\n# MULTIPLICACIÓN\nk = 2.5\nimg33_mul_k = img_adapteq * k\nimg33_mul_gamma03 = img33_add_gamma03 * img33_add_gamma03\nimg33_mul_adapteq = img33_add_adapte_02 * img33_add_adapte_02\nimg33_mul_original_k = img33 * k\nimg33_mul_add_clahe_opt_k = img33_add_clahe_opt_k * img33_add_clahe_opt_k\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_mul_k, axes[:, 1])\nax_img.set_title('Mult. CLAHE Opt. k=2.5')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_mul_gamma03, axes[:, 2])\nax_img.set_title('Mult. Gamma=0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_mul_adapteq, axes[:, 3])\nax_img.set_title('Mult. Iguales CLAHE Opt.')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_mul_original_k, axes[:, 4])\nax_img.set_title('Mult. Original por k')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img33_mul_add_clahe_opt_k, axes[:, 5])\nax_img.set_title('Mult. Ig. Suma  CLAHE Opt. (k=0.2)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/14.png)\n\n``` python\nconstante = 0.2\nk = 2.5\n\nimg333_gamma_corrected = exposure.adjust_gamma(img333, 0.3)\nimg333_logarithmic_corrected = exposure.adjust_log(img333, 25)\nimg333_adapteq = exposure.equalize_adapthist(img333, clip_limit=0.2)\nimg333_add_k = img333_adapteq + constante\nimg333_mul_add_k = img333_add_k * img333_add_k\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_gamma_corrected, axes[:, 1])\nax_img.set_title('Gamma 0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_logarithmic_corrected, axes[:, 2])\nax_img.set_title('Log 25')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_adapteq, axes[:, 3])\nax_img.set_title('CLAHE Óptimizada')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_add_k, axes[:, 4])\nax_img.set_title('Suma CLAHE Optimizada + k=2.5')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_mul_add_k, axes[:, 5])\nax_img.set_title('Mult. Suma (k=0.2)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/15.png)\n\n``` python\nconstante = 0.2\nk = 2.5\n\nimg77_gamma_corrected = exposure.adjust_gamma(img77, 0.3)\nimg77_logarithmic_corrected = exposure.adjust_log(img77, 25)\nimg77_adapteq = exposure.equalize_adapthist(img77, clip_limit=0.2)\nimg77_add_k = img77_adapteq + constante\nimg77_mul_add_k = img77_add_k * img77_add_k\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_gamma_corrected, axes[:, 1])\nax_img.set_title('Gamma 0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_logarithmic_corrected, axes[:, 2])\nax_img.set_title('Log 25')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_adapteq, axes[:, 3])\nax_img.set_title('CLAHE Óptimizada')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_add_k, axes[:, 4])\nax_img.set_title('Suma CLAHE Optimizada + k=2.5')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_mul_add_k, axes[:, 5])\nax_img.set_title('Mult. Suma (k=0.2)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/16.png)\n\n``` python\nconstante = 0.2\nk = 2.5\n\nimg777_gamma_corrected = exposure.adjust_gamma(img777, 0.3)\nimg777_logarithmic_corrected = exposure.adjust_log(img777, 25)\nimg777_adapteq = exposure.equalize_adapthist(img777, clip_limit=0.2)\nimg777_add_k = img777_adapteq + constante\nimg777_mul_add_k = img777_add_k * img777_add_k\n\n# Display results\nfig = plt.figure(figsize=(18, 8))\naxes = np.zeros((2, 6), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 6, 1)\nfor i in range(1, 6):\n    axes[0, i] = fig.add_subplot(2, 6, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 6):\n    axes[1, i] = fig.add_subplot(2, 6, 7+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777, axes[:, 0])\nax_img.set_title('Imagen Original')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Número de pixeles')\nax_hist.set_yticks(np.linspace(0, y_max, 6))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_gamma_corrected, axes[:, 1])\nax_img.set_title('Gamma 0.3')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_logarithmic_corrected, axes[:, 2])\nax_img.set_title('Log 25')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_adapteq, axes[:, 3])\nax_img.set_title('CLAHE Óptimizada')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_add_k, axes[:, 4])\nax_img.set_title('Suma CLAHE Optimizada + k=2.5')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_mul_add_k, axes[:, 5])\nax_img.set_title('Mult. Suma (k=0.2)')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 6))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n\n![](/images/17.png)\n\n**Hemos determinado que las imagenes procesadas con la función CLAHE,\nproduce una imagen de mayor calidad en su nitidez, contraste y\nluminosidad**\n\n``` python\n\n# Display results\nfig = plt.figure(figsize=(20, 8))\naxes = np.zeros((2, 4), dtype=object)\naxes[0, 0] = fig.add_subplot(2, 4, 1)\nfor i in range(1, 4):\n    axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0])\nfor i in range(0, 4):\n    axes[1, i] = fig.add_subplot(2, 4, 5+i)\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 0])\nax_img.set_title('Imagen #33')\n\ny_min, y_max = ax_hist.get_ylim()\nax_hist.set_ylabel('Number of pixels')\nax_hist.set_yticks(np.linspace(0, y_max, 5))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img333_adapteq, axes[:, 1])\nax_img.set_title('Imagen #333')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img77_adapteq, axes[:, 2])\nax_img.set_title('Imagen #77')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img777_adapteq, axes[:, 3])\nax_img.set_title('Imagen #777')\n\nax_cdf.set_ylabel('Fraction of total intensity')\nax_cdf.set_yticks(np.linspace(0, 1, 5))\n\n# prevent overlap of y-axis labels\nfig.tight_layout()\nplt.show()\n```\n![](/images/18.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpbonillas%2Flab_image_enhancement","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpbonillas%2Flab_image_enhancement","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpbonillas%2Flab_image_enhancement/lists"}