Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import cv2
  4. import numpy as np
  5.  
  6. # mesh image
  7. imMesh = cv2.imread('new_refined2__DXF.png', -1)
  8.  
  9. # layer image
  10. imLayer1 = cv2.imread('Layer1.png', -1)
  11.  
  12. """
  13. binary conversions
  14. """
  15. imMeshGray = cv2.cvtColor(imMesh, cv2.COLOR_BGR2GRAY)
  16. ret, imMeshBw = cv2.threshold(imMeshGray, 250, 255, cv2.THRESH_BINARY)
  17.  
  18. imLayer1Gray = cv2.cvtColor(imLayer1, cv2.COLOR_BGR2GRAY)
  19. (thresh, imLayerBw) = cv2.threshold(imLayer1Gray, 128, 255,
  20.                                     cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  21.  
  22. connectivity = 4
  23.  
  24. output = cv2.connectedComponentsWithStats(imMeshBw, connectivity, cv2.CV_32S)
  25. num_labels = output[0]
  26. labels = output[1]
  27. stats = output[2]
  28.  
  29. centroids = output[3]
  30.  
  31.  
  32. print(labels)
  33. print(num_labels)
  34.  
  35. # if I want for each label
  36. for label in np.unique(labels):
  37.     if label == 0:
  38.         continue
  39.     mask = np.zeros(imMeshGray.shape, dtype="uint8")
  40.     mask[labels == label] = 255
  41.     cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
  42.     # c = max(cnts, key=cv2.contourArea)
  43.     for c in cnts:
  44.         M = cv2.moments(c)
  45.         if M["m00"] != 0:
  46.             cX = int(M["m10"] / M["m00"])
  47.             cY = int(M["m01"] / M["m00"])
  48.         else:
  49.             cX, cY = 0, 0
  50.  
  51.     peri = cv2.arcLength(c, True)
  52.     approx = cv2.approxPolyDP(c, 0.05 * peri, True)
  53.     if (len(approx) == 3):
  54.         cv2.drawContours(imMesh, [c], -1, (0, 255, 0), 1)
  55.         cv2.putText(imMesh, "*", (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,0.5, (0, 0, 0), 1)
  56.  
  57. cv2.imwrite('tstCountoursConnected.jpg', imMesh)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement