Browse Source

Dateien hochladen nach „“

master
Jakob Haber 3 years ago
parent
commit
af02743558
5 changed files with 144 additions and 0 deletions
  1. BIN
      1.jpg
  2. BIN
      DOCUMENT SCANNER-1.jpg
  3. 58
    0
      DetectChessCorners.py
  4. 86
    0
      DocumentScannerMain.py
  5. BIN
      Documnet Scanner.jpg

BIN
1.jpg View File


BIN
DOCUMENT SCANNER-1.jpg View File


+ 58
- 0
DetectChessCorners.py View File

import numpy as np
import cv2 as cv
import glob
import os
heightImg = 800
widthImg = 1000
FieldSize = 100
BASE = os.path.dirname(__file__)
pathImage = "SchachBrett2.jpg"
# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((7*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
img = cv.imread(BASE+ "/"+ pathImage) #glob.glob('*.jpg')
img = cv.resize(img, (widthImg, heightImg))
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv.findChessboardCorners(gray, (7,7), None)
# If found, add object points, image points (after refining them)
if ret == True:
#get Corner Position of the 1. Rank
#print(corners)
leftupper = corners[6][0]
leftlower = corners[48][0]
rightupper = corners[42][0]
rightlower = corners[0][0]
#Transform Image with the founded Corner Position
pts1 = np.float32([rightupper, rightlower, leftupper, leftlower]) # PREPARE POINTS FOR WARP
print(pts1)
pts2 = np.float32([[FieldSize, FieldSize*7], [FieldSize*7, FieldSize*7], [FieldSize*7, FieldSize], [FieldSize, FieldSize]]) # PREPARE POINTS FOR WARP
matrix = cv.getPerspectiveTransform(pts1, pts2)
imgWarpColored = cv.warpPerspective(img, matrix, (FieldSize*8, FieldSize*8))
#Draw founded Chess Corners
imgContours = img.copy()
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv.drawChessboardCorners(img, (7,7), corners2, ret)
else:
ret, corners = cv.findChessboardCorners(gray, (5, 5), None)
#cv.imshow('img', img)
cv.imshow('img', imgWarpColored)
cv.waitKey(500000)

+ 86
- 0
DocumentScannerMain.py View File

import cv2
import numpy as np
import utlis
import os

########################################################################
webCamFeed = True
pathImage = "SchachBrett2.jpg"
cap = cv2.VideoCapture()
#cap = cv2.VideoCapture(1)
cap.set(10,160)
heightImg = 640
widthImg = 480

BASE = os.path.dirname(__file__)
########################################################################

utlis.initializeTrackbars()
count=0

while True:
img = cv2.imread(BASE+ "/"+ 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, (5, 5), 1) # ADD GAUSSIAN BLUR
thres=utlis.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=2) # 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 =cv2.findChessboardCorners(img, (8,8),corners, cv2.CALIB_CB_ADAPTIVE_THRESH )
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 = utlis.biggestContour(contours) # FIND THE BIGGEST CONTOUR
if biggest.size != 0:
biggest=utlis.reorder(biggest)
cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20) # DRAW THE BIGGEST CONTOUR
imgBigContour = utlis.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)

# 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 = utlis.stackImages(imageArray,0.75,lables)
cv2.imshow("Result",stackedImage)

# SAVE IMAGE WHEN 's' key is pressed
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

BIN
Documnet Scanner.jpg View File


Loading…
Cancel
Save