Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # import os
  4. import cv2
  5. import numpy as np
  6.  
  7. img = cv2.imread('invMehs.png', -1)
  8.  
  9. imGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  10. ret, imBw = cv2.threshold(imGray, 250, 255, cv2.THRESH_BINARY)
  11.  
  12.  
  13. # connected components
  14. invBwMesh = cv2.bitwise_not(imBw)
  15.  
  16. colorMask = np.ones(img.shape, dtype="uint8") * 255
  17. # Mask = np.ones(imBw.shape, dtype="uint8") * 255
  18. # print(Mask.shape)
  19. connectivity = 4
  20.  
  21. output = cv2.connectedComponentsWithStats(imBw, connectivity, cv2.CV_32S)
  22. num_labels = output[0]
  23. labels = output[1]
  24. stats = output[2]
  25.  
  26. centroids = output[3]
  27.  
  28. labels = labels + 1
  29.  
  30.  
  31. # if I want for each label
  32. for label in np.unique(labels):
  33.     mask = np.zeros(imGray.shape, dtype="uint8")
  34.     # however it will only visible the last component, because I just have one mask variable
  35.     mask[labels == label] = 255
  36.  
  37.  
  38. cv2.imwrite('tst.jpg', mask)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement