`
import cv2
import numpy as np
img = cv2.imread('example.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,30,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
image, contours, hierarchy = cv2.findContours(skel, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
draw = cv2.drawContours(skel, contours, 0, (0,255,0), 3)
##color = cv2.cvtColor(skel, cv2.COLOR_GRAY2BGR)
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True
cv2.imshow("skel",skel)
cv2.imshow("contours", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
`[link text](https://www.youtube.com/watch?v=i364EE7pTtg)
import cv2
import numpy as np
img = cv2.imread('example.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,30,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
##image, contours, hierarchy = cv2.findContours(skel, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
##draw = cv2.drawContours(skel, contours, 0, (0,255,0), 3)
##color = cv2.cvtColor(skel, cv2.COLOR_GRAY2BGR)
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
edged = cv2.Canny(skel, 50, 150)
zeros = size - cv2.countNonZero(img)
lines = cv2.HoughLinesP(skel,0.1,np.pi/90,1,100,1)
if(lines != None):
x1,y1,x2,y2 in lines[0]:
cv2.line(resized,(x1,y1),(x2,y2),(0,255,0),2)
if zeros==size:
done = True
cv2.imshow("skel",skel)
cv2.imshow("edged",edged)
cv2.imshow("lines",lines)
cv2.waitKey(0)
cv2.destroyAllWindows()