Advertisement
dan-masek

Untitled

Jan 1st, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. def show(img):
  6.     plt.figure(figsize=(10,10))
  7.     plt.imshow(img,cmap='gray')
  8.     plt.show()
  9.  
  10.  
  11. image = cv2.imread('receipt-1.jpg', cv2.IMREAD_GRAYSCALE)
  12.  
  13. cv2.fastNlMeansDenoising(image, image, 10, 5, 11)
  14.  
  15. thresh = cv2.adaptiveThreshold(image, 255, 1, 1, 11, 2)
  16.  
  17. kernel = np.ones((1,5), np.uint8)
  18. img_dilation = cv2.dilate(thresh, kernel, iterations=1)
  19.  
  20. # find contours
  21. _,ctrs,_ = cv2.findContours(img_dilation,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  22.  
  23. # Sort contours
  24. sorted_ctrs = sorted(ctrs,key=cv2.contourArea)
  25.  
  26.  
  27. result = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
  28.  
  29. for i, ctr in enumerate(sorted_ctrs):
  30.     x, y, w, h = cv2.boundingRect(ctr)
  31.     if h > 50: continue
  32.     roi = thresh[y:y+h, x:x+w]
  33.     result[y:y+h, x:x+w, 0] = 0
  34.     result[y:y+h, x:x+w, 1] = 255
  35.  
  36. show(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement