This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilla.py
More file actions
43 lines (31 loc) · 1.52 KB
/
billa.py
File metadata and controls
43 lines (31 loc) · 1.52 KB
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
import cv2
import numpy as np
import mapper
import io
image = cv2.imread("hey.jpg") # read in the image
_, compressedimage=cv2.imencode(".jpg",image, [1,90])
file_bytes=io.BytesIO(compressedimage)
#image = cv2.resize(image, (1300, 800)) # resizing because opencv does not work well with bigger images
orig = compressedimage.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # RGB To Gray Scale
cv2.imshow("Title", gray)
blurred = cv2.GaussianBlur(gray, (5, 5),
0) # (5,5) is the kernel size and 0 is sigma that determines the amount of blur
cv2.imshow("Blur", blurred)
edged = cv2.Canny(blurred, 30, 50) # 30 MinThreshold and 50 is the MaxThreshold
cv2.imshow("Canny", edged)
contours,hierarchy = cv2.findContours(edged, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE) # retrieve the contours as a list, with simple apprximation model
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# the loop extracts the boundary contours of the page
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4:
target = approx
break
approx = mapper.mapp(target) # find endpoints of the sheet
pts = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) # map to 800*800 target window
op = cv2.getPerspectiveTransform(approx, pts) # get the top or bird eye view effect
dst = cv2.warpPerspective(orig, op, (800, 800))
cv2.imshow("Scanned", dst)