资源简介
基于opencv和python的骨架提取代码,利用击中不击中变化实现细化操作。切分以后,结合深度学习识别技术,可以对字符 数字等印刷体有一个比较理想的分割和识别效果。本人亲测 有生成结果图片和原图片对照实例,可以动态展示骨架提取过程,具体参照rar文件包。有问题随时沟通。
代码片段和文件信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
from imutils import resize
from imutils.contours import sort_contours
from skimage.morphology import skeletonize as skl
import numpy as np
img = cv2.imread(‘D:\job\img\kongquekaiping\\301\\301.jpg‘0)
#path = ‘D:\job\img\kongquekaiping\\301\\301.png‘
#path = ‘cat.jpg‘
#img = cv2.imread(path 0)
# Some smoothing to get rid of the noise
# img = cv2.bilateralFilter(img 5 35 10)
img = cv2.GaussianBlur(img (3 3) 3)
img = resize(img width=700)
# Preprocessing to get the shapes
th = cv2.adaptiveThreshold(img 255 cv2.ADAPTIVE_THRESH_GAUSSIAN_C
cv2.THRESH_BINARY 35 11)
# Invert to hightligth the shape
th = cv2.bitwise_not(th)
# Text has mostly vertical and right-inclined lines. This kernel seems to
# work quite well
kernel = np.array([[0 1 1]
[0 1 0]
[1 1 0]] dtype=‘uint8‘)
th = cv2.morphologyEx(th cv2.MORPH_CLOSE kernel)
cv2.imshow(‘mask‘ th)
cv2.waitKey(0)
#def contour_sorter(contours):
# ‘‘‘Sort the contours by multiplying the y-coordinate and sorting first by
# x then by y-coordinate.‘‘‘
# boxes = [cv2.boundingRect(c) for c in contours]
# cnt = [4*y x for y x _ _ in ]
# Skeletonize the shapes
# Skimage function takes image with either True False or 01
# and returns and image with values 0 1.
th = th == 255
th = skl(th)
th = th.astype(np.uint8)*255
# Find contours of the skeletons
_ contours _ = cv2.findContours(th.copy() cv2.RETR_EXTERNAL
cv2.CHAIN_APPROX_NONE)
# Sort the contours left-to-rigth
contours _ = sort_contours(contours )
#
# Sort them again top-to-bottom
def skeleton_endpoints(skel):
# Function source: https://stackoverflow.com/questions/26537313/
# how-can-i-find-endpoints-of-binary-skeleton-image-in-opencv
# make out input nice possibly necessary
skel = skel.copy()
skel[skel != 0] = 1
skel = np.uint8(skel)
# apply the convolution
kernel = np.uint8([[1 1 1]
[1 10 1]
[1 1 1]])
src_depth = -1
filtered = cv2.filter2D(skel src_depthkernel)
# now look through to find the value of 11
# this returns a mask of the endpoints but if you just want the
# coordinates you could simply return np.where(filtered==11)
out = np.zeros_like(skel)
out[np.where(filtered == 11)] = 1
rows cols = np.where(filtered == 11)
coords = list(zip(cols rows))
return coords
# List for endpoints
endpoints = []
# List for (x y) coordinates of the skeletons
skeletons = []
for contour in contours:
if cv2.arcLength(contour True) > 100:
# Initialize mask
mask = np.zeros(img.shape np.uint8)
# Bounding rect of the contour
x y w h = cv2.boundingRect(contour)
mask[y:y+h x:x+w] = 255
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 42701 2018-04-12 10:37 ske618.png
文件 3882 2018-04-11 19:15 skeleton.py
----------- --------- ---------- ----- ----
46583 2
相关资源
- python3+Tkinter+GUI界面+pyserial+串口+串口
- annoy-1.15.2-cp36-cp36m-win_amd64.whl
- caffe_log绘制accuracy和loss曲线python3
- python零基础入门视频 百度云资源
- Python3.x+Pyqt5实现界面左侧导航栏的抽
- Python3.x+Pyqt5制作GUI界面的案例
- python3的ARP简单攻击脚本
- Anaconda Python3.6 安装包32bit +64bit
- python3实现word转txt
- python36实现打外星人小游戏图形界面游
- 西电数据挖掘作业——k中心聚类pyt
- python3 HTMLTestRunner截图&美化&优化
- python3爬取中国天气网天气并写入csv
- SIFT算法特征提取的python实现
- 最新Python3.6网络爬虫实战案例5章(基
- Python3.x+Pyqt5实现绘图界面matplotlib绘图
- Python3入门与进阶
- pyltp python3.7可用版本,已编译好的.
- python3网络爬虫开发实战 无密码
-
gameob
jects-0.0.3 for python3 - python3实现多线程破解tomcat简单密码
- Python3 实现SM3国产哈希算法
- 汉字书法图像骨架提取
- python3.4爬取网络图片
- PyQt4-4.11.4 win32 python3.4直接安装版(
- python3零基础学习视频共20周带源码
- 《Python3网络爬虫开发实战》中文PDF
- numpy-1.17.0+mkl-cp37-cp37m-win_amd64.whl百度云
- Python3网络爬虫实战思维导图
- python3 包装的httpclient,支持session
评论
共有 条评论