1. 程式人生 > >Python: scikit-image Blob detection

Python: scikit-image Blob detection

ont lis spl 圖像 sdn text plot laplacian org

這個用例主要介紹利用三種算法對含有blob的圖像進行檢測。blob 或者叫斑點,就是在一幅圖像上,暗背景上的亮區域。或者亮背景上的暗區域,都能夠稱為blob。

主要利用blob與背景之間的對照度來進行檢測。

這個用例介紹了三種算法;

Laplacian of Gaussian (LoG)
這是速度最慢,可是最準確的一種算法。簡單來說,就是對一幅圖先進行一系列不同尺度的高斯濾波,然後對濾波後的圖像做Laplacian運算。將全部的圖像進行疊加。局部最大值就是所要檢測的blob,這個算法對於大的blob檢測會非常慢,還有就是該算法適合於檢測暗背景下的亮blob。

Difference of Gaussian (DoG)
這是LoG算法的一種高速近似,對圖像進行高斯濾波之後,不做Laplacian運算,直接做減法。相減後的圖做疊加。找到局部最大值,這個算法的缺陷與LoG相似。

Determinant of Hessian (DoH)
這是最快的一種算法,不須要做多尺度的高斯濾波,運算速度自然提升非常多,這個算法對暗背景上的亮blob或者亮背景上的暗blob都能檢測。

缺點是小尺寸的blob檢測不準確。

P.S. LoG 和 DoG 假設想檢測亮背景上的暗blob,能夠將圖像做反相,這樣亮背景就變成了暗背景,而暗blob就變成了亮blob,然後就能夠用這兩個算法了,檢測完之後再反回來就好了。

from matplotlib import pyplot as plt
from skimage import data
from skimage.feature import
blob_dog, blob_log, blob_doh from math import sqrt from skimage.color import rgb2gray image = data.hubble_deep_field()[0:500, 0:500] image_gray = rgb2gray(image) plt.imshow(image) blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.1) # Compute radii in the 3rd column. blobs_log[:, 2
] = blobs_log[:, 2] * sqrt(2) blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.1) blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2) blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01) blobs_list = [blobs_log, blobs_dog, blobs_doh] colors = [‘yellow‘, ‘lime‘, ‘red‘] titles = [‘Laplacian of Gaussian‘, ‘Difference of Gaussian‘, ‘Determinant of Hessian‘] sequence = zip(blobs_list, colors, titles) fig,axes = plt.subplots(1, 3, sharex=True, sharey=True, subplot_kw={‘adjustable‘:‘box-forced‘}) axes = axes.ravel() for blobs, color, title in sequence: ax = axes[0] axes = axes[1:] ax.set_title(title) ax.imshow(image, interpolation=‘nearest‘) for blob in blobs: y, x, r = blob c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False) ax.add_patch(c) plt.show()

參考來源: http://scikit-image.org/docs/dev/auto_examples/

原圖:

技術分享

效果圖:

技術分享

Python: scikit-image Blob detection