Document Scanner - opencv cv2 tutorial project2
project2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import cv2
import numpy as np
from cv2tools import stackImages
Width = 832
Height = 640
def preprocessing(img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(gray_img, (5, 5), 0)
kernel = np.ones((5, 5), np.uint8)
canny_img = cv2.Canny(blur_img, 200, 200)
dilation_img = cv2.dilate(canny_img, kernel, iterations=2)
erode_img = cv2.erode(dilation_img, kernel, iterations=1)
return erode_img
def getContours(img):
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
maxArea = 0
biggest = None
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 500:
# cv2.drawContours(imgCopy, cnt, -1, (255, 0, 0), 3)
arclen = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.02 * arclen, True)
if area > maxArea and len(approx) == 4:
maxArea = area
biggest = approx
cv2.drawContours(imgCopy, biggest, -1, (255,0,0), 30)
return biggest
def wrap(img, contours):
def reorder(contours):
contours = contours.reshape(4, 2)
generate = np.zeros((4,2), np.float32)
sumArr = np.sum(contours, axis=1)
generate[0] = contours[np.argmin(sumArr)]
generate[3] = contours[np.argmax(sumArr)]
diffArr = np.diff(contours, axis=1)
generate[1] = contours[np.argmin(diffArr)]
generate[2] = contours[np.argmax(diffArr)]
return generate
contours = reorder(contours)
pst1 = np.float32(contours)
pst2 = np.float32([[0, 0], [Width, 0], [0, Height], [Width, Height]])
matrix = cv2.getPerspectiveTransform(pst1, pst2)
imgWarp = cv2.warpPerspective(img, matrix, (Width, Height))
return imgWarp
img = cv2.imread('Resources/paper.jpg')
img = cv2.resize(img, (Width, Height))
imgCopy = img.copy()
imgPreprocess = preprocessing(img)
biggest = getContours(imgPreprocess)
imgWrap = wrap(img, biggest)
imgRst = stackImages(0.5, ([img, imgPreprocess], [imgCopy, imgWrap]))
cv2.imshow('paper', imgRst)
cv2.waitKey(0)
This post is licensed under CC BY 4.0 by the author.