-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathdewapper.py
57 lines (48 loc) · 1.75 KB
/
dewapper.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -----------------------------------------
# author : Ahmet Ozlu
# mail : [email protected]
# date : 05.05.2019
# -----------------------------------------
from utils.transform import four_point_transform
from skimage.filters import threshold_local
import numpy as np
import argparse
import cv2
import imutils
def dewarp_book(image):
# get input image ration to keep best output resolution quality
ratio = image.shape[0] / 500.0
# copy source image for filter operations
orig = image.copy()
# resize the input image
image = imutils.resize(image, height = 500)
# convert rgb input image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# sigma parameter is used for automatic Canny edge detection
sigma=0.33
# compute the median of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)
# perform dilate morphological filter to connect teh image pixel points
'''kernel = np.ones((5,5),np.uint8)
edged = cv2.dilate(edged,kernel,iterations = 1)'''
# find contours
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
# apply the four point transform for book dewarping
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
return warped