{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Blob Detection\n\nBlobs are bright on dark or dark on bright regions in an image. In\nthis example, blobs are detected using 3 algorithms. The image used\nin this case is the Hubble eXtreme Deep Field. Each bright dot in the\nimage is a star or a galaxy.\n\n## Laplacian of Gaussian (LoG)\nThis is the most accurate and slowest approach. It computes the Laplacian\nof Gaussian images with successively increasing standard deviation and\nstacks them up in a cube. Blobs are local maximas in this cube. Detecting\nlarger blobs is especially slower because of larger kernel sizes during\nconvolution. Only bright blobs on dark backgrounds are detected. See\n:py:meth:`skimage.feature.blob_log` for usage.\n\n## Difference of Gaussian (DoG)\nThis is a faster approximation of LoG approach. In this case the image is\nblurred with increasing standard deviations and the difference between\ntwo successively blurred images are stacked up in a cube. This method\nsuffers from the same disadvantage as LoG approach for detecting larger\nblobs. Blobs are again assumed to be bright on dark. See\n:py:meth:`skimage.feature.blob_dog` for usage.\n\n## Determinant of Hessian (DoH)\nThis is the fastest approach. It detects blobs by finding maximas in the\nmatrix of the Determinant of Hessian of the image. The detection speed is\nindependent of the size of blobs as internally the implementation uses\nbox filters instead of convolutions. Bright on dark as well as dark on\nbright blobs are detected. The downside is that small blobs (<3px) are not\ndetected accurately. See :py:meth:`skimage.feature.blob_doh` for usage.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from math import sqrt\nfrom skimage import data\nfrom skimage.feature import blob_dog, blob_log, blob_doh\nfrom skimage.color import rgb2gray\n\nimport matplotlib.pyplot as plt\n\n\nimage = data.hubble_deep_field()[0:500, 0:500]\nimage_gray = rgb2gray(image)\n\nblobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=0.1)\n\n# Compute radii in the 3rd column.\nblobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)\n\nblobs_dog = blob_dog(image_gray, max_sigma=30, threshold=0.1)\nblobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)\n\nblobs_doh = blob_doh(image_gray, max_sigma=30, threshold=0.01)\n\nblobs_list = [blobs_log, blobs_dog, blobs_doh]\ncolors = ['yellow', 'lime', 'red']\ntitles = ['Laplacian of Gaussian', 'Difference of Gaussian', 'Determinant of Hessian']\nsequence = zip(blobs_list, colors, titles)\n\nfig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True)\nax = axes.ravel()\n\nfor idx, (blobs, color, title) in enumerate(sequence):\n    ax[idx].set_title(title)\n    ax[idx].imshow(image)\n    for blob in blobs:\n        y, x, r = blob\n        c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)\n        ax[idx].add_patch(c)\n    ax[idx].set_axis_off()\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}