利用cv2.morphologyEx提取图像边界
本文最后更新于:2023年4月7日 下午
cv2.morphologyEx(image, cv2.MORPH_GRADIENT, SE)可以参考这篇博客
cv2.MORPH_GRADIENT:形态学梯度(morph-grad),可以突出团块(blob)的边缘, 保留物体的边缘轮廓。
#coding:utf-8 import SimpleITK as sitk import numpy as np import cv2 def edge(image):
SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
img_grad = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, SE)
return img_grad def save(x, path):
predict_seg = sitk.GetImageFromArray(x)
sitk.WriteImage(predict_seg, path)
def read_nii(path):
image = sitk.ReadImage(path)
img_num = sitk.GetArrayFromImage(image)
return img_num
if name == “main“:
path = r“D:\myProject\HDC_vessel_seg\datasets\nii\vessel_1.nii“
img_num = read_nii(path)
img_num = img_num[20:84,…]
slices = img_num.shape[0]
result = np.zeros(img_num.shape)
for i in range(slices):
sli = img_num[i:i+1, …]
s = sli[0,…]
slice = edge(s)
result[i,…] = slice
save(result, path.replace(“vessel_1“, “edge_vessel_1“))
save(img_num, path.replace(“vessel_1“, “pre_vessel_1“))
print(slices)
结果:
原始图像:
提取边缘:
打赏支持
“如果你觉得我的文章不错,不妨鼓励我继续写作。”
利用cv2.morphologyEx提取图像边界
https://dreamoneyou.github.io/2022/利用cv2.morphologyEx提取图像边界/