forked from wangdotjason/pest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanthing.py
108 lines (91 loc) · 4.38 KB
/
scanthing.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import cv2
import numpy as np
import utils
########################################################################
webCamFeed = False
pathImage = "tomato.jpg"
# cap = cv2.VideoCapture(0)
# cap.set(10,160)
heightImg = 1000
widthImg = 700
########################################################################
#utils.initializeTrackbars()
count=0
thresh_ct = 0
x = cv2.imread(pathImage)
while True:
img = cv2.imread(pathImage)
img = cv2.resize(img, (widthImg, heightImg)) # RESIZE IMAGE
imgBlank = np.zeros((heightImg,widthImg, 3), np.uint8) # CREATE A BLANK IMAGE FOR TESTING DEBUGING IF REQUIRED
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # CONVERT IMAGE TO GRAY SCALE
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1) # ADD GAUSSIAN BLUR
thres = [240 - thresh_ct, 240 - thresh_ct] #utils.valTrackbars() # GET TRACK BAR VALUES FOR THRESHOLDS
imgThreshold = cv2.Canny(imgBlur,thres[0],thres[1]) # APPLY CANNY BLUR
kernel = np.ones((5, 5))
imgDial = cv2.dilate(imgThreshold, kernel, iterations=4) # APPLY DILATION
imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # APPLY EROSION
## FIND ALL COUNTOURS
imgContours = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES
imgBigContour = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES
contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS
cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS
# FIND THE BIGGEST COUNTOUR
biggest, maxArea = utils.biggestContour(contours) # FIND THE BIGGEST CONTOUR
if biggest.size != 0 or thresh_ct == 200:
break
thresh_ct += 10
if biggest.size != 0:
biggest=utils.reorder(biggest)
cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20) # DRAW THE BIGGEST CONTOUR
imgBigContour = utils.drawRectangle(imgBigContour,biggest,2)
pts1 = np.float32(biggest) # PREPARE POINTS FOR WARP
pts2 = np.float32([[0, 0],[widthImg, 0], [0, heightImg],[widthImg, heightImg]]) # PREPARE POINTS FOR WARP
matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgWarpColored = cv2.warpPerspective(img, matrix, (widthImg, heightImg))
#REMOVE 20 PIXELS FORM EACH SIDE
imgWarpColored=imgWarpColored[20:imgWarpColored.shape[0] - 20, 20:imgWarpColored.shape[1] - 20]
imgWarpColored = cv2.resize(imgWarpColored,(widthImg,heightImg))
# APPLY ADAPTIVE THRESHOLD
imgWarpGray = cv2.cvtColor(imgWarpColored,cv2.COLOR_BGR2GRAY)
imgAdaptiveThre = cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 7, 2)
imgAdaptiveThre = cv2.bitwise_not(imgAdaptiveThre)
imgAdaptiveThre = cv2.medianBlur(imgAdaptiveThre,3)
imgAdaptiveDial = cv2.dilate(imgAdaptiveThre, (3,3), iterations=4)
imgAdaptiveEro = cv2.erode(imgAdaptiveDial, kernel, iterations=4)
# Image Array for Display
#imageArray = ([img,imgGray,imgThreshold,imgContours],
# [imgBigContour,imgWarpColored, imgWarpGray,imgAdaptiveThre])
# else:
# imageArray = ([img,imgGray,imgThreshold,imgContours],
# [imgBlank, imgBlank, imgBlank, imgBlank])
# LABELS FOR DISPLAY
#lables = [["Original","Gray","Threshold","Contours"],
#["Biggest Contour","Warp Prespective","Warp Gray","Adaptive Threshold"]]
#stackedImage = utils.stackImages(imageArray,0.75,lables)
#cv2.imshow("Result",imgAdaptiveThre)
# SAVE IMAGE WHEN 's' key is pressed
#cv2.imshow("a",imgGray)
#cv2.imshow("as",imgBlur)
cv2.imshow("asds",imgThreshold)
cv2.imshow(":)",imgContours)
cv2.imshow("esfse.jpg",imgWarpGray)
cv2.imshow("myImage1.jpg",imgAdaptiveThre)
cv2.imshow("myImage.jpg",imgWarpColored)
cv2.imshow("mfsf",imgAdaptiveEro)
cv2.waitKey(0)
cv2.destroyAllWindows()
#
# while 1:
# cv2.imshow(":)",imgContours)
# if cv2.waitKey(1) & 0xFF == ord('s'):
# cv2.imwrite("Scanned/myImage"+str(count)+".jpg",imgWarpColored)
# cv2.rectangle(stackedImage, ((int(stackedImage.shape[1] / 2) - 230), int(stackedImage.shape[0] / 2) + 50),
# (1100, 350), (0, 255, 0), cv2.FILLED)
# cv2.putText(stackedImage, "Scan Saved", (int(stackedImage.shape[1] / 2) - 200, int(stackedImage.shape[0] / 2)),
# cv2.FONT_HERSHEY_DUPLEX, 3, (0, 0, 255), 5, cv2.LINE_AA)
# cv2.imshow('Result', stackedImage)
# cv2.waitKey(300)
# count += 1
# if 0xFF == ord('q'):
# cv2.destroyWindow(":)")
# break