日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单像素骨架提取算法c语言实现,【图像】骨架提取与分水岭算法

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单像素骨架提取算法c语言实现,【图像】骨架提取与分水岭算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、骨架提取

骨架提取,也叫二值圖像細化。這種算法能將一個連通區域細化成一個像素的寬度,用于特征提取和目標拓撲表示。

morphology子模塊提供了兩個函數用于骨架提取,分別是Skeletonize()函數和medial_axis()函數。

1.1. Skeletonize()函數

輸入和輸出都是一幅二值圖像。

from skimage import morphology,data,color

import matplotlib.pyplot as plt

image=color.rgb2gray(data.horse())

image=1-image #反相

#實施骨架算法

skeleton =morphology.skeletonize(image)

#顯示結果

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))

ax1.imshow(image, cmap=plt.cm.gray)

ax1.axis('off')

ax1.set_title('original', fontsize=20)

ax2.imshow(skeleton, cmap=plt.cm.gray)

ax2.axis('off')

ax2.set_title('skeleton', fontsize=20)

fig.tight_layout()

plt.show()

1.2. medial_axis(img,mask=None,return_distance=False)

mask: 掩模。默認為None, 如果給定一個掩模,則在掩模內的像素值才執行骨架算法。

return_distance: bool型值,默認為False. 如果為True, 則除了返回骨架,還將距離變換值也同時返回。這里的距離指的是中軸線上的所有點與背景點的距離。

import numpy as np

import scipy.ndimage as ndi

from skimage import morphology

import matplotlib.pyplot as plt

#編寫一個函數,生成測試圖像

def microstructure(l=256):

n = 5

x, y = np.ogrid[0:l, 0:l]

mask = np.zeros((l, l))

generator = np.random.RandomState(1)

points = l * generator.rand(2, n**2)

mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1

mask = ndi.gaussian_filter(mask, sigma=l/(4.*n))

return mask > mask.mean()

data = microstructure(l=64) #生成測試圖像

#計算中軸和距離變換值

skel, distance =morphology.medial_axis(data, return_distance=True)

#中軸上的點到背景像素點的距離

dist_on_skel = distance * skel

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))

ax1.imshow(data, cmap=plt.cm.gray, interpolation='nearest')

#用光譜色顯示中軸

ax2.imshow(dist_on_skel, cmap=plt.cm.spectral, interpolation='nearest')

ax2.contour(data, [0.5], colors='w') #顯示輪廓線

fig.tight_layout()

plt.show()

2. 分水嶺算法

分水嶺算法是一種用于圖像分割的經典算法,是基于拓撲理論的數學形態學的分割方法。如果圖像中的目標物體是連在一起的,則分割起來會更困難,分水嶺算法經常用于處理這類問題,通常會取得比較好的效果。

2.1.?基于距離變換的分山嶺圖像分割

import numpy as np

import matplotlib.pyplot as plt

from scipy import ndimage as ndi

from skimage import morphology,feature

#創建兩個帶有重疊圓的圖像

x, y = np.indices((80, 80))

x1, y1, x2, y2 = 28, 28, 44, 52

r1, r2 = 16, 20

mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2

mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2

image = np.logical_or(mask_circle1, mask_circle2)

#現在我們用分水嶺算法分離兩個圓

distance = ndi.distance_transform_edt(image) #距離變換

local_maxi =feature.peak_local_max(distance, indices=False, footprint=np.ones((3, 3)),

labels=image) #尋找峰值

markers = ndi.label(local_maxi)[0] #初始標記點

labels =morphology.watershed(-distance, markers, mask=image) #基于距離變換的分水嶺算法

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))

axes = axes.ravel()

ax0, ax1, ax2, ax3 = axes

ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')

ax0.set_title("Original")

ax1.imshow(-distance, cmap=plt.cm.jet, interpolation='nearest')

ax1.set_title("Distance")

ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')

ax2.set_title("Markers")

ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')

ax3.set_title("Segmented")

for ax in axes:

ax.axis('off')

fig.tight_layout()

plt.show()

2.2.?基于梯度的分水嶺圖像分割

import matplotlib.pyplot as plt

from scipy import ndimage as ndi

from skimage import morphology,color,data,filter

image =color.rgb2gray(data.camera())

denoised = filter.rank.median(image, morphology.disk(2)) #過濾噪聲

#將梯度值低于10的作為開始標記點

markers = filter.rank.gradient(denoised, morphology.disk(5)) <10

markers = ndi.label(markers)[0]

gradient = filter.rank.gradient(denoised, morphology.disk(2)) #計算梯度

labels =morphology.watershed(gradient, markers, mask=image) #基于梯度的分水嶺算法

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6))

axes = axes.ravel()

ax0, ax1, ax2, ax3 = axes

ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')

ax0.set_title("Original")

ax1.imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest')

ax1.set_title("Gradient")

ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')

ax2.set_title("Markers")

ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')

ax3.set_title("Segmented")

for ax in axes:

ax.axis('off')

fig.tight_layout()

plt.show()

總結

以上是生活随笔為你收集整理的单像素骨架提取算法c语言实现,【图像】骨架提取与分水岭算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。