Advertisement
nicholas_b_crews

open_by_reconstruction()

Aug 7th, 2016
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. def open_by_reconstruction(src, iterations = 1, ksize = 3):
  2.         # first erode the source image
  3.         eroded = cv2.erode(src, np.ones((ksize,ksize), np.uint8), iterations=iterations)
  4.  
  5.         # Now we are going to iteratively regrow the eroded mask.
  6.         # The key difference between just a simple opening is that we
  7.         # mask the regrown everytime with the original src.
  8.         # Thus, the dilated mask never extends beyond where it does in the original.
  9.         this_iteration = eroded
  10.         last_iteration = eroded
  11.         while (True):
  12.             this_iteration = cv2.dilate(last_iteration, np.ones((ksize,ksize), np.uint8), iterations = 1)
  13.             this_iteration = this_iteration & src
  14.             if np.array_equal(last_iteration, this_iteration):
  15.                 # convergence!
  16.                 break
  17.             last_iteration = this_iteration.copy()
  18.        
  19.         return this_iteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement