Advertisement
dan-masek

Untitled

Feb 4th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. img = cv2.imread('box.png')
  5.  
  6. img = cv2.blur(img,(5,5))
  7.  
  8. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  9. h,s,v = cv2.split(hsv)
  10.  
  11.  
  12. cv2.imwrite('box_h.png', cv2.applyColorMap(h, cv2.COLORMAP_JET))
  13. cv2.imwrite('box_s.png', cv2.applyColorMap(s, cv2.COLORMAP_JET))
  14. cv2.imwrite('box_v.png', cv2.applyColorMap(v, cv2.COLORMAP_JET))
  15.  
  16.  
  17. thresh0 = cv2.adaptiveThreshold(s,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
  18. thresh1 = cv2.adaptiveThreshold(v,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
  19. thresh2 = cv2.adaptiveThreshold(v,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
  20. thresh = cv2.bitwise_or(thresh0, thresh1)
  21.  
  22. cv2.imwrite('box_ht.png', thresh2)
  23. cv2.imwrite('box_st.png', thresh0)
  24. cv2.imwrite('box_vt.png', thresh1)
  25.  
  26. kernel = np.ones((3, 3), np.uint8)
  27. thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
  28. thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  29.  
  30. mask = np.ones_like(v) * 255
  31.  
  32. contours,heirarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
  33. for contour in contours:
  34.   hull = cv2.convexHull(contour)
  35.   area = cv2.contourArea(contour)
  36.   if area > 10.0:
  37.     cv2.drawContours(mask,[hull],-1,0,-1)
  38.  
  39.    
  40. mask = cv2.morphologyEx(255 - mask, cv2.MORPH_ERODE, kernel, iterations=3)
  41. result = cv2.bitwise_and(img, cv2.merge([mask]*3))
  42.  
  43. cv2.imwrite('box_masked.png', result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement