Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 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. cv2.imwrite('bwImlayer.jpg', imLayerBw)
  23. connectivity = 4
  24.  
  25. output = cv2.connectedComponentsWithStats(imMeshBw, connectivity, cv2.CV_32S)
  26. num_labels = output[0]
  27. labels = output[1]
  28. stats = output[2]
  29.  
  30. centroids = output[3]
  31. # labels = labels + 1
  32.  
  33. print(labels)
  34. print(num_labels)
  35.  
  36. # if I want for each label
  37. for label in np.unique(labels):
  38.     if label == 0:
  39.         continue
  40.     mask = np.zeros(imMeshGray.shape, dtype="uint8")
  41.     mask[labels == label] = 255
  42.     # maskedLayer = cv2.bitwise_and(mask, imLayer1)
  43.     maskedLayer = cv2.bitwise_and(mask, imLayerBw)
  44.     cnts = cv2.findContours(maskedLayer.copy(),
  45.                             cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
  46.     # print(cnts)
  47.     if not cnts:
  48.         continue
  49.     else:
  50.         for c in cnts:
  51.             M = cv2.moments(c)
  52.             if M["m00"] != 0:
  53.                 cX = int(M["m10"] / M["m00"])
  54.                 cY = int(M["m01"] / M["m00"])
  55.             else:
  56.                 cX, cY = 0, 0
  57.         peri = cv2.arcLength(c, True)
  58.         approx = cv2.approxPolyDP(c, 0.05 * peri, True)
  59.         cv2.drawContours(imMesh, [c], -1, (0, 255, 0), 1)
  60.         cv2.putText(imMesh, "*",
  61.                     (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
  62. cv2.imwrite('tstCountoursConnected.jpg', imMesh)
  63. #        if (len(approx) == 3):
  64. #            cv2.drawContours(imMesh, [c], -1, (0, 255, 0), 1)
  65. #            cv2.putText(imMesh, "*", (cX, cY),
  66. #                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement