1
import scipy.ndimage.morphology as m
import numpy as np
import cv2

def skeletonize(img):
h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) 
m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) 
h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) 
m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]])    
hit_list = [] 
miss_list = []
for k in range(4): 
    hit_list.append(np.rot90(h1, k))
    hit_list.append(np.rot90(h2, k))
    miss_list.append(np.rot90(m1, k))
    miss_list.append(np.rot90(m2, k))    
img = img.copy()
while True:
    last = img
    for hit, miss in zip(hit_list, miss_list): 
        hm = m.binary_hit_or_miss(img, hit, miss) 
        img = np.logical_and(img, np.logical_not(hm)) 
    if np.all(img == last):  
        break
return img

img = cv2.imread("e_5.jpg",0)
ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
img = 255 - img
img = cv2.dilate(img, element, iterations=3)

skel = skeletonize(img)
imshow(skel, cmap="gray", interpolation="nearest")

I have been trying this code inorder to skeletonize an image without any gaps in between the skeleton lines. But whenever I run the program , an error is hitting"imshow in not defined".

I tried plt.imshow, at this point, there was neither an error nor an output image. Can anyone tell me where am going wrong.

cfh
  • 4,576
  • 1
  • 24
  • 34
ANUSHA DEVI
  • 225
  • 1
  • 4
  • 17

1 Answers1

1

You may have forgotten to import the module using as short alias plt (note that the name can be replaced by what you wants), AND call the show() command so add the following to your code:

import matplotlib.pyplot as plt

plt.imshow()
plt.show()

see for more information: https://stackoverflow.com/a/3497922/4716013

Community
  • 1
  • 1
prodev_paris
  • 495
  • 1
  • 4
  • 17