[英]How to smooth a skeletonized image and search for a point on the smoothed line?
如果我理解正確,您對骨架的對角線感興趣。 這是一個想法,它可能需要一些工作才能完全滿足您的需求。
我想通過優化一些起始線的長度和亮度來找到圖片為白色的線。 為此,如果骨架稍厚一點,它會在遠離中心的地方慢慢變成黑色,這將是有用的。 所以我做了以下預處理
import numpy as np
import matplotlib.pyplot as plt
import cv2
from scipy.ndimage import map_coordinates
img = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE)
img = ((img>100)*255).astype('uint8')
n = 20
img = cv2.dilate(img, np.ones((n+5,n+5)))
img = cv2.blur(img, (n+1,n+1))
現在我做上面提到的優化
def get_points_on_line(x):
p = x[:2].reshape(-1,1)
q = x[2:].reshape(-1,1)
t = np.linspace(0,1,10**3)
coords = t*p+(1-t)*q
length = np.linalg.norm(p-q)
values = (map_coordinates(img, coords, prefilter=False)-255*9/10)
value = values.mean()
return -value*length
res = minimize(get_points_on_line, [250,200,500,700], method = 'Nelder-Mead')
並繪制結果
x = res.x
p = x[:2].reshape(-1,1)
q = x[2:].reshape(-1,1)
t = np.linspace(0,1,10**3)
coords = t*p+(1-t)*q
plt.imshow(img, cmap='gray')
plt.plot(coords[1], coords[0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.