Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- def show(img):
- plt.figure(figsize=(10,10))
- plt.imshow(img,cmap='gray')
- plt.show()
- image = cv2.imread('receipt-1.jpg', cv2.IMREAD_GRAYSCALE)
- cv2.fastNlMeansDenoising(image, image, 10, 5, 11)
- thresh = cv2.adaptiveThreshold(image, 255, 1, 1, 11, 2)
- kernel = np.ones((1,5), np.uint8)
- img_dilation = cv2.dilate(thresh, kernel, iterations=1)
- # find contours
- _,ctrs,_ = cv2.findContours(img_dilation,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- # Sort contours
- sorted_ctrs = sorted(ctrs,key=cv2.contourArea)
- result = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
- for i, ctr in enumerate(sorted_ctrs):
- x, y, w, h = cv2.boundingRect(ctr)
- if h > 50: continue
- roi = thresh[y:y+h, x:x+w]
- result[y:y+h, x:x+w, 0] = 0
- result[y:y+h, x:x+w, 1] = 255
- show(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement