python 骨架提取及细化算法(可直接用)

# 导入库
import matplotlib
import matplotlib.pyplot as plt
import cv2 as cv
# 将图像转为灰度图像
from PIL import Image

path_img = r'C:\Users\Administrator\PycharmProjects\acquire_RGB\picture\B1F\105242247.png'
# 读取灰度图像
img_original = cv.imread(path_img)
gray = cv.cvtColor(img_original, cv.COLOR_BGRA2GRAY)  # 把彩色通道图像转换成灰度图

# 对图像进行预处理,二值化
# 中值滤波
median_img = cv.medianBlur(gray,5)
# 二值化
ret,BW_Original = cv.threshold(median_img,10,255,cv.THRESH_BINARY)
# BW_Original = (BW_Original < 235)
cv.imshow('b',BW_Original)
cv.waitKey()
# 定义像素点周围的8邻域
#                P9 P2 P3
#                P8 P1 P4
#                P7 P6 P5

def neighbours(x, y, image):
    img = image
    x_1, y_1, x1, y1 = x - 1, y - 1, x + 1, y + 1
    return [img[x_1][y], img[x_1][y1], img[x][y1], img[x1][y1],  # P2,P3,P4,P5
            img[x1][y], img[x1][y_1], img[x][y_1], img[x_1][y_1]]  # P6,P7,P8,P9

#N(p1)表示与1相邻的8个像素点中,为前景像素点的个数。
def sum_number(n):
    S = 0
    for i in range(len(n)):
        if (n[i] == 255):
            S += 1
    return S

# 计算邻域像素从0变化到1的次数
def transitions_num(neighbours):
    n = neighbours +neighbours[0:1]
    S = 0
    for i in range(len(n)-1):
        if ((n[i],n[i+1]) == (0,255)):
            S += 1
            # print(S)
    return S

# Zhang-Suen 细化算法
def zhangSuen(image):
    Image_Thinned = image.copy()  # Making copy to protect original image
    changing1 = changing2 = 1
    while changing1 or changing2:  # Iterates until no further changes occur in the image
        # Step 1
        changing1 = []
        rows, columns = Image_Thinned.shape
        for x in range(1, rows - 1):
            for y in range(1, columns - 1):
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(x, y, Image_Thinned)
                # print(P2, P3, P4, P5, P6, P7, P8, P9,P2)
                print(len(n))
                # print(sum_number(n))
                # print(transitions_num(n))
                if (Image_Thinned[x][y] == 255 and  # Condition 0: Point P1 in the object regions
                        2 <= sum_number(n)<= 6 and  # Condition 1: 2<= N(P1) <= 6
                        transitions_num(n) == 1 and  # Condition 2: S(P1)=1
                        P2 * P4 * P6 == 0 and  # Condition 3
                        P4 * P6 * P8 == 0):  # Condition 4
                    changing1.append((x, y))
        for x, y in changing1:
            Image_Thinned[x][y] = 0
        # Step 2
        changing2 = []
        for x in range(1, rows - 1):
            for y in range(1, columns - 1):
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(x, y, Image_Thinned)
                if (Image_Thinned[x][y] == 255 and  # Condition 0
                        2 <= sum_number(n) <= 6 and  # Condition 1
                        transitions_num(n) == 1 and  # Condition 2
                        P2 * P4 * P8 == 0 and  # Condition 3
                        P2 * P6 * P8 == 0):  # Condition 4
                    changing2.append((x, y))
        for x, y in changing2:
            Image_Thinned[x][y] = 0
    return Image_Thinned

# 对染色体图像应用Zhang-Suen细化算法
BW_Skeleton = zhangSuen(BW_Original)
# cv.imshow('BW',BW_Skeleton)
# cv.waitKey()
import numpy as np
BW_Skeleton = np.invert(BW_Skeleton)

# 显示细化结果
fig, ax = plt.subplots(1, 2)
ax1, ax2 = ax.ravel()
ax1.imshow(gray, cmap=plt.cm.gray)
ax1.set_title('Original binary image')
ax1.axis('off')
ax2.imshow(BW_Skeleton, cmap=plt.cm.gray)
ax2.set_title('Skeleton of the image')
ax2.axis('off')
plt.savefig('./thinned.png')
plt.show()

中间参考别人的代码,因代码不全本人补充全代码,可以运行,简单使用,拿走不谢。

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值