当前位置:网站首页>Pytorch extract skeleton (differentiable)
Pytorch extract skeleton (differentiable)
2022-07-06 16:10:00 【Little Aries in the mountains】
Preface
There are many ready-made packages for extracting skeletons , The simplest and most direct is :
from skimage.morphology import skeletonize, skeletonize_3d
But today we will introduce another method of extracting skeleton ! It can also be understood as refinement
Use pytorch The goal is , This process is differentiable , In other words , You can reverse the gradient , For network prediction mask, You can extract the skeleton through this function , Then constrain the topology of the object on the skeleton
This method comes from the literature CVPR 2021:clDice - a Novel Topology-Preserving Loss Function for Tubular Structure Segmentation
Source code :https://github.com/jocpae/clDice
( The source code is very, very simple , This is one of the few so concise CVPR The source )
Source code
import torch
import torch.nn as nn
import torch.nn.functional as F
def soft_erode(img):
if len(img.shape)==4:
p1 = -F.max_pool2d(-img, (3,1), (1,1), (1,0))
p2 = -F.max_pool2d(-img, (1,3), (1,1), (0,1))
return torch.min(p1,p2)
elif len(img.shape)==5:
p1 = -F.max_pool3d(-img,(3,1,1),(1,1,1),(1,0,0))
p2 = -F.max_pool3d(-img,(1,3,1),(1,1,1),(0,1,0))
p3 = -F.max_pool3d(-img,(1,1,3),(1,1,1),(0,0,1))
return torch.min(torch.min(p1, p2), p3)
def soft_dilate(img):
if len(img.shape)==4:
return F.max_pool2d(img, (3,3), (1,1), (1,1))
elif len(img.shape)==5:
return F.max_pool3d(img,(3,3,3),(1,1,1),(1,1,1))
def soft_open(img):
return soft_dilate(soft_erode(img))
def soft_skel(img, iter_):
img1 = soft_open(img)
skel = F.relu(img-img1)
for j in range(iter_):
img = soft_erode(img)
img1 = soft_open(img)
delta = F.relu(img-img1)
skel = skel + F.relu(delta-skel*delta)
return skel
This is all the code for extracting the skeleton in this article , It's really simple
test
Take a CVPPP Of label For example :
To test the code that extracts the skeleton , I take the boundary of the above image as the foreground , To refine the boundary .
Use the following function to extract the boundary ( Expand three times at the same time ):
import numpy as np
from PIL import Image
from scipy import ndimage
from skimage.segmentation import find_boundaries
def extract_boundary(label, dilation=False):
boundary = np.zeros_like(label, dtype=np.uint8)
ids, counts = np.unique(label, return_counts=True)
for i, id in enumerate(ids):
if id == 0:
boundary[label == 0] = 0
else:
tmp = np.zeros_like(label)
tmp[label == id] = 1
tmp_bound = find_boundaries(tmp != 0, mode='outer')
if dilation:
tmp_bound = ndimage.binary_dilation(tmp_bound, iterations=3, border_value=1)
boundary[tmp_bound == 1] = 1
return boundary
Be ready , Upper main function :
if __name__ == "__main__":
label = np.asarray(Image.open('plant154_label.png'))
boundary = extract_boundary(label, dilation=True)
boundary_uint8 = boundary.astype(np.uint8) * 255
Image.fromarray(boundary_uint8).save('boundary.png')
boundary = boundary.astype(np.float32)
boundary = boundary[np.newaxis, np.newaxis, ...]
boundary = torch.from_numpy(boundary)
skel = soft_skel(boundary, 10)
skel = np.squeeze(skel.numpy())
skel = (skel * 255).astype(np.uint8)
Image.fromarray(skel).save('skel.png')
Running results :
The boundary visualization results before extracting the skeleton :
Visualization results of extracted skeleton :
use skimage.morphology Medium skeletonize The result of function extraction :
summary
- The proposed method is different from the traditional skeletonize(skimage) There is still a certain gap , especially , From the visualization results above , We can also see that , There are many faults in the estimation extracted by this method , This is unbearable for the skeleton
- This method has a super parameter , That is, specify the number of iterations , This parameter also greatly affects the result , The above example is used 10, I tried 5 The situation of , The generated skeleton is even worse
- But this method still has one biggest advantage , It is differentiable , Derivable , It can be used to output the skeleton in the network and then as a loss
边栏推荐
- 快速转 TypeScript 指南
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- Perform general operations on iptables
- 969. Pancake sorting
- [exercise-6] (PTA) divide and conquer
- 1005. Maximized array sum after K negations
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- [exercise-4] (UVA 11988) broken keyboard = = (linked list)
- 读取和保存zarr文件
- 渗透测试 ( 1 ) --- 必备 工具、导航
猜你喜欢
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)

Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B

PySide6 信号、槽

Information security - Analysis of security orchestration automation and response (soar) technology

969. Pancake sorting

Radar equipment (greedy)

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers

Data storage in memory & loading into memory to make the program run
随机推荐
Opencv learning log 26 -- detect circular holes and mark them
The most complete programming language online API document
Auto.js入门
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Web based photo digital printing website
Opencv learning log 27 -- chip positioning
Openwrt build Hello ipk
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
【练习-8】(Uva 246)10-20-30==模拟
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Sanic异步框架真的这么强吗?实践中找真理
Information security - security professional name | CVE | rce | POC | Vul | 0day
7-1 understand everything (20 points)
Essai de pénétration (1) - - outils nécessaires, navigation
Opencv learning log 28 -- detect the red cup cover
【高老师软件需求分析】20级云班课习题答案合集
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Penetration test (8) -- official document of burp Suite Pro