ZhangSuen法:

论文连接:A fast parallel algorithm for thinning digital patterns
代码连接:https://github.com/bsdnoobz/zhang-suen-thinning

上代码:

代码1

from scipy import weave
import numpy as np
import cv2
import sysdef _thinningIteration(im, iter):I, M = im, np.zeros(im.shape, np.uint8)expr = """for (int i = 1; i < NI[0]-1; i++) {for (int j = 1; j < NI[1]-1; j++) {int p2 = I2(i-1, j);int p3 = I2(i-1, j+1);int p4 = I2(i, j+1);int p5 = I2(i+1, j+1);int p6 = I2(i+1, j);int p7 = I2(i+1, j-1);int p8 = I2(i, j-1);int p9 = I2(i-1, j-1);int A  = (p2 == 0 && p3 == 1) + (p3 == 0 && p4 == 1) +(p4 == 0 && p5 == 1) + (p5 == 0 && p6 == 1) +(p6 == 0 && p7 == 1) + (p7 == 0 && p8 == 1) +(p8 == 0 && p9 == 1) + (p9 == 0 && p2 == 1);int B  = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);if (A == 1 && B >= 2 && B <= 6 && m1 == 0 && m2 == 0) {M2(i,j) = 1;}}} """weave.inline(expr, ["I", "iter", "M"])return (I & ~M)def thinning(src):dst = src.copy() / 255prev = np.zeros(src.shape[:2], np.uint8)diff = Nonewhile True:dst = _thinningIteration(dst, 0)dst = _thinningIteration(dst, 1)diff = np.absolute(dst - prev)prev = dst.copy()if np.sum(diff) == 0:breakreturn dst * 255if __name__ == "__main__":src = cv2.imread("kanji.png")if src == None:sys.exit()bw = cv2.cvtColor(src, cv2.cv.CV_BGR2GRAY)_, bw2 = cv2.threshold(bw, 10, 255, cv2.THRESH_BINARY)bw2 = thinning(bw2)cv2.imshow("src", bw)cv2.imshow("thinning", bw2)cv2.waitKey()

代码2

import cv2
import numpy as np
import time
import matplotlib.pyplot as pltdef ROI(img):indexXY = np.argwhere(img > 0)minxy = np.min(indexXY, axis=0)maxxy = np.max(indexXY, axis=0)return minxy,maxxy
def neighbours(x,y,img):i = imgx1,y1,x_1, y_1 = x+1, y-1, x-1, y+1return [i[y1][x],  i[y1][x1],   i[y][x1],  i[y_1][x1],  # P2,P3,P4,P5i[y_1][x], i[y_1][x_1], i[y][x_1], i[y1][x_1]]  # P6,P7,P8,P9
def transitions(neighbours):n = neighbours + neighbours[0:1]  # P2, ... P9, P2return sum((n1, n2) == (0, 1) for n1, n2 in zip(n, n[1:]))
def ZhangSuenPlus(image):"""运行时间55s:param image::return:"""changing1 = changing2 = [(-1, -1)]while changing1 or changing2:# Step 1changing1 = []for y in range(1, len(image) - 1):for x in range(1, len(image[0]) - 1):P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, image)if (image[y][x] == 1 and    # (Condition 0)P4 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P6 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing1.append((x,y))for x, y in changing1: image[y][x] = 0# Step 2changing2 = []for y in range(1, len(image) - 1):for x in range(1, len(image[0]) - 1):P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, image)if (image[y][x] == 1 and    # (Condition 0)P2 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P8 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing2.append((x,y))for x, y in changing2: image[y][x] = 0#print changing1#print changing2flags = image>0image[flags] = 255#cv2.imshow("res",image)return image
def ZhangSuenPlus02(image):"""# 运行时间12.135秒:param image::return:"""indexXY = np.argwhere(image>0)minxy = np.min(indexXY,axis=0)maxxy = np.max(indexXY,axis=0)roi = image[minxy[0]-1:maxxy[0]+2,minxy[1]-1:maxxy[1]+2]changing1 = changing2 = [(-1, -1)]while changing1 or changing2:# Step 1changing1 = []for y in range(1, len(roi) - 1):for x in range(1, len(roi[0]) - 1):if roi[y][x] == 1:P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, roi)if (    # (Condition 0)P4 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P6 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing1.append((x,y))for x, y in changing1: roi[y][x] = 0# Step 2changing2 = []for y in range(1, len(roi) - 1):for x in range(1, len(roi[0]) - 1):if roi[y][x] == 1:P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, roi)if (   # (Condition 0)P2 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P8 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing2.append((x,y))for x, y in changing2: roi[y][x] = 0#print changing1#print changing2flags = roi>0roi[flags] = 255#cv2.imshow("res",image)return image
def ZhangSuenPlus03(image):"""# 运行时间9秒:param image::return:"""indexXY = np.argwhere(image>0)minxy = np.min(indexXY,axis=0)maxxy = np.max(indexXY,axis=0)roi = image[minxy[0]-1:maxxy[0]+2,minxy[1]-1:maxxy[1]+2]changing1 = changing2 = [(-1, -1)]while changing1 or changing2:indexXY = np.argwhere(roi>0)minxy = np.min(indexXY, axis=0)maxxy = np.max(indexXY, axis=0)roi = roi[minxy[0] - 1:maxxy[0] + 2, minxy[1] - 1:maxxy[1] + 2]# Step 1changing1 = []for y in range(1, len(roi) - 1):for x in range(1, len(roi[0]) - 1):if roi[y][x] == 1:P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, roi)if (    # (Condition 0)P4 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P6 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing1.append((x,y))for x, y in changing1: roi[y][x] = 0# Step 2changing2 = []for y in range(1, len(roi) - 1):for x in range(1, len(roi[0]) - 1):if roi[y][x] == 1:P2,P3,P4,P5,P6,P7,P8,P9 = n = neighbours(x, y, roi)if (   # (Condition 0)P2 * P6 * P8 == 0 and   # Condition 4P2 * P4 * P8 == 0 and   # Condition 3transitions(n) == 1 and # Condition 22 <= sum(n) <= 6):      # Condition 1changing2.append((x,y))for x, y in changing2: roi[y][x] = 0#print changing1#print changing2flags = roi>0roi[flags] = 255#cv2.imshow("res",image)return imagedef ZhangSuen_Bad(img):"""将灰度值转化为0和1,企图加速计算过程运行时间21.28sthresh计算失败:param img::return:"""copyMat = img.copy()k = 0row,col= img.shaperow = row-1col = col-1while(True):k= k+1stop= False# step1for i in range(1,row):for j in range(1,col):if img[i,j]>0:print(">0")p1 = 1 if img[i,j]>0 else 0p2 = 1 if img[i-1,j]>0 else 0p3 = 1 if img[i-1,j+1]>0 else 0p4 = 1 if img[i, j+1] > 0 else 0p5 = 1 if img[i+1, j+1] > 0 else 0p6 = 1 if img[i+1, j] > 0 else 0p7 = 1 if img[i+1, j-1] > 0 else 0p8 = 1 if img[i,j-1] > 0 else 0p9 = 1 if img[i-1, j-1] > 0 else 0np1 = p2+p3+p4+p5+p6+p7+p8+p9sp2 = 1 if (p2 == 0 and p3 == 1) else 0sp3 = 1 if (p3 == 0 and p4 == 1) else 0sp4 = 1 if (p4 == 0 and p5 == 1) else 0sp5 = 1 if (p5 == 0 and p6 == 1) else 0sp6 = 1 if (p6 == 0 and p7 == 1) else 0sp7 = 1 if (p7 == 0 and p8 == 1) else 0sp8 = 1 if (p8 == 0 and p9 == 1) else 0sp9 = 1 if (p9 == 0 and p2 == 1) else 0sp1 = sp2 + sp3 + sp4 + sp5 + sp6 + sp7 + sp8 + sp9if np1>=2 and np1<=6 and sp1==1 and(p2*p4*p6)==0 and (p4*p6*p8)==0:stop = TruecopyMat[i,j] = 0print("success")img = copyMat.copy()# step2for i in range(1,row):for j in range(1,col):if img[i,j]>0:print(">>")p2 = 1 if img[i - 1, j] > 0 else 0p3 = 1 if img[i - 1, j + 1] > 0 else 0p4 = 1 if img[i, j + 1] > 0 else 0p5 = 1 if img[i + 1, j + 1] > 0 else 0p6 = 1 if img[i + 1, j] > 0 else 0p7 = 1 if img[i + 1, j - 1] > 0 else 0p8 = 1 if img[i, j - 1] > 0 else 0p9 = 1 if img[i - 1, j - 1] > 0 else 0np1 = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9sp2 = 1 if (p2 == 0 and p3 == 1) else 0sp3 = 1 if (p3 == 0 and p4 == 1) else 0sp4 = 1 if (p4 == 0 and p5 == 1) else 0sp5 = 1 if (p5 == 0 and p6 == 1) else 0sp6 = 1 if (p6 == 0 and p7 == 1) else 0sp7 = 1 if (p7 == 0 and p8 == 1) else 0sp8 = 1 if (p8 == 0 and p9 == 1) else 0sp9 = 1 if (p9 == 0 and p2 == 1) else 0sp1 = sp2 + sp3 + sp4 + sp5 + sp6 + sp7 + sp8 + sp9if np1 >= 2 and np1 <= 6 and sp1 == 1 and (p2*p4*p8) == 0 and (p2*p6*p8) == 0:stop = TruecopyMat[i,j] = 0print("success")img = copyMat.copy()if(not stop):breakresImg = copyMat.copy()flags = resImg>0resImg[flags] = 255#print(k)# cv2.imshow("res",resImg)return resImgdef ZhangSuen(img):"""运行时间20.7s:param img::return:"""#indexXY = np.argwhere(img>0)#minxy = np.min(indexXY,axis=0)#maxxy = np.max(indexXY,axis=0)#roi = img[minxy[0]-3:maxxy[0]+4,minxy[1]-3:maxxy[1]+4]#flags = roi>0#roi[flags] = 255#cv2.imshow("roi",roi)#print(roi.shape)roi = imgk = 0row,col= roi.shapechanging1 = changing2 = [(-1, -1)]while changing1 or changing2:changing1 = []for i in range(1,row-1):for j in range(1,col-1):if roi[i,j]==1:p2 = roi[i-1,j]p3 = roi[i-1,j+1]p4 = roi[i, j+1]p5 = roi[i+1, j+1]p6 = roi[i+1, j]p7 = roi[i+1, j-1]p8 = roi[i,j-1]p9 = roi[i-1, j-1]np1 = p2+p3+p4+p5+p6+p7+p8+p9sp2 = 1 if (p2,p3)==(0,1) else 0sp3 = 1 if (p3,p4)==(0,1) else 0sp4 = 1 if (p4,p5)==(0,1) else 0sp5 = 1 if (p5,p6)==(0,1) else 0sp6 = 1 if (p6,p7)==(0,1) else 0sp7 = 1 if (p7,p8)==(0,1) else 0sp8 = 1 if (p8,p9)==(0,1) else 0sp9 = 1 if (p9,p2)==(0,1) else 0sp1 = sp2 + sp3 + sp4 + sp5 + sp6 + sp7 + sp8 + sp9if 2<=np1<=6 and sp1==1 and(p2*p4*p6)==0 and (p4*p6*p8)==0:changing1.append([i,j])for x,y in changing1:roi[x,y] = 0# step2changing2 = []for i in range(1,row-1):for j in range(1,col-1):if roi[i,j]==1:p2 = roi[i - 1, j]p3 = roi[i - 1, j + 1]p4 = roi[i, j + 1]p5 = roi[i + 1, j + 1]p6 = roi[i + 1, j]p7 = roi[i + 1, j - 1]p8 = roi[i, j - 1]p9 = roi[i - 1, j - 1]np1 = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9sp2 = 1 if (p2, p3) == (0, 1) else 0sp3 = 1 if (p3, p4) == (0, 1) else 0sp4 = 1 if (p4, p5) == (0, 1) else 0sp5 = 1 if (p5, p6) == (0, 1) else 0sp6 = 1 if (p6, p7) == (0, 1) else 0sp7 = 1 if (p7, p8) == (0, 1) else 0sp8 = 1 if (p8, p9) == (0, 1) else 0sp9 = 1 if (p9, p2) == (0, 1) else 0sp1 = sp2 + sp3 + sp4 + sp5 + sp6 + sp7 + sp8 + sp9if 2<=np1<= 6 and sp1 == 1 and (p2*p4*p8) == 0 and (p2*p6*p8) == 0:#roi[i,j] = 0changing2.append([i,j])#print("success")for x,y in changing2:roi[x,y] = 0flags = roi>0roi[flags] = 255return roidef Draw():plt.figure()plt.subplot(131)plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))plt.title("origin image")plt.axis("off")plt.subplot(132)# plt.imshow(res,"gray")plt.title("res image")plt.axis("off")plt.subplot(133)# plt.imshow(resP,"gray")plt.title("resP image")plt.axis("off")plt.show()
# Draw()def XiHua(img):kernel_d = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))image_d = cv2.dilate(image, kernel_d, iterations=8)# cv2.namedWindow('dilate', cv2.WINDOW_NORMAL)# cv2.imshow('dilate', image_d)kernel_e = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))image_e = cv2.erode(image_d, kernel_e)image_ske = morphology.skeletonize(image_e)image_e = np.multiply(image_e, image_ske)return image_eif __name__ == "__main__":import osimport timefrom skimage import morphologyimage_path = '../images/'save_path = '../train/do/'per_time = 0sum_time = 0if not os.path.isdir(save_path): os.makedirs(save_path)for img in os.listdir(image_path):image = cv2.imread(os.path.join(image_path, img))gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 125, 255, cv2.THRESH_BINARY)time1 = time.time()result = ZhangSuen_Bad(thresh)time2 = time.time()temp_time = time2 - time1sum_time += temp_timeprint("one image time: ", temp_time)cv2.namedWindow('result', 0)cv2.imshow('result', result)cv2.waitKey(10)cv2.imwrite(os.path.join(save_path, img), result)per_time = sum_time / len(os.listdir(image_path))print("average image time:", per_time)

贴结果:

激光条纹中心提取——ZhangSuen法python相关推荐

  1. 激光条纹中心提取——Zhang-Suen法python

    Zhang-Suen法 原理-- Zhang-Suen法 代码--python代码 原理-- Zhang-Suen法 细化法(又称形态学骨架法)是通过对光条纹不断地进行腐蚀操作,剥离光条纹边界,得到单 ...

  2. 激光条纹中心提取——灰度中心法python

    激光条纹中心提取--灰度中心法python 灰度中心法 python代码 灰度中心法 灰度重心法是根据每行光条纹横截面内的灰度分布特征逐行进行处理,通过在行坐标的方向上,逐行计算提取光条纹区域的灰度重 ...

  3. 传统激光条纹中心提取算法研究现状

    传统激光条纹中心提取算法研究现状 前言 一.边缘法 二.中心法 三.阈值法 四. 细化法 五.极值法 六.灰度重心法 七.方向模板 八.曲线拟合法 九.Steger 前言 光条中心提取是将宽度大于1的 ...

  4. 激光条纹中心提取——方法总结

    激光条纹中心提取--方法总结 算法 优势 缺点 边缘法 处理速度快:适用于精度要求低的大型物体测量 存在很大误差:要求图像质量较好且结构光特性较高 中心法 适用于条纹质量好且形状规则的物体测量:精度高 ...

  5. 【必备知识】:线激光条纹中心线提取算法导读

    线激光条纹特性 线激光器是由点激光器和前置透镜组成的.点激光器可以为He-Ne激光器或半导体激光器.相比较He-Ne激光器,半导体激光器因其输出光源具有发散性,更适合用于制作线激光器.需要说明的是,半 ...

  6. 中线提取算法_综述|线结构光中心提取算法研究发展

    摘 要: 线结构光扫描是三维重建领域的关键技术.光条纹中心提取算法是决定线结构光三维重建精度以及光条纹轮廓定位准确性的重要因素.本文详细阐述了光条纹中心提取算法的理论基础及发展历程,将现有算法分为三类 ...

  7. 灰度重心法提取光条纹中心

    灰度重心法提取激光光条纹中心其实是将光条纹截面的灰度值分布中的质心记作为光条纹的中心. 在一列线激光中先利用极值法求取光强最大的一点gmax,然后确定一个阀值K=gmax-g(g取10-20),在 ...

  8. opencv双目视觉标定,激光结构光提取,指定特征点获取世界坐标

    双目视觉标定,激光结构光提取,指定特征点获取世界坐标 标定方面 校正 结构光提取 二维点转换为三维点 总结 这学期在做双目视觉方面的事情,因为没人带,自己一个人踩了很多坑,因此在这写一点自己的总结心得 ...

  9. 灰色关联与TOPSIS法 —— python

    目录 1.简介 2.算法详解 2.1 指标正向化及标准化 2.2 找到最大最小参考向量 2.3 计算与参考向量的相关系数 2.4 求评分 3.实例分析 3.1 读取数据 3.2 数据标准化 3.3 得 ...

最新文章

  1. java培训班如何选择
  2. R语言dplyr包的slice函数提取数据集的数据行实战
  3. Android调用WebService
  4. Spring Boot教程(7) – 直观地理解Spring容器
  5. Python 中的 os 模块常见方法?
  6. 使用Lock and Load X 插件时导致Final Cat Pro意外退出的解决办法
  7. 电平通讯速度_飞凌干货丨几种常见的电平转换电路分析及应用
  8. 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
  9. 【codevs1576】最长严格上升子序列
  10. unity3D【全版本】设置中文
  11. 一、思科模拟器教程了解软件
  12. linux系统安装kms,Linux安装部署KMS服务器
  13. 直接寻址、间接寻址、立即数寻址、寄存器寻址
  14. 我爱淘冲刺阶段站立会议每天任务5
  15. 360云盘上传 计算机文件格式,360云盘怎么上传文件 360云盘上传文件方法
  16. 小D的一串数字(简单DP)
  17. Python代码写好了怎么运行?
  18. PC端、手机端在线预览文档组件react-file-viewer与npm构建内存溢出
  19. 关于IE下载文件无法弹出下载框
  20. 基础篇(二).ARMv8寄存器(2)

热门文章

  1. 【Windows】Win 10 无法访问同一网络中Mac 电脑
  2. 痔疮最佳治疗方法 十人九痔 不必害羞
  3. PYTHON-模块 re subprocess
  4. lerna import报错
  5. 学画画要花多少钱_孩子学画画大约需要多少钱呢?
  6. macbook 如何稳定的使用第三方鼠标
  7. 企业上云,打造数字经济新动能
  8. 线性代数1:向量、线性组合、张成的空间和基
  9. 八. geotrellis使用 矢量数据栅格化
  10. 美国加州华裔人口最多的32个城市排名(根据最新统计数字计算)