Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 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. # labels = labels + 1
  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.     # maskedLayer = cv2.bitwise_and(mask, imLayer1)
  42.     maskedLayer = cv2.bitwise_and(mask, imLayerBw)
  43.     cnts = cv2.findContours(maskedLayer.copy(),
  44.                             cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
  45.     print(cnts)
  46.     for c in cnts:
  47.         M = cv2.moments(c)
  48.         if M["m00"] != 0:
  49.             cX = int(M["m10"] / M["m00"])
  50.             cY = int(M["m01"] / M["m00"])
  51.         else:
  52.             cX, cY = 0, 0
  53.     peri = cv2.arcLength(c, True)
  54.     approx = cv2.approxPolyDP(c, 0.05 * peri, True)
  55.     if (len(approx) == 3):
  56.         cv2.drawContours(imMesh, [c], -1, (0, 255, 0), 1)
  57.         cv2.putText(imMesh, "*", (cX, cY),
  58.                     cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement