eine Schach Stellung soll mit eine Kamera erkannt werden
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentScannerMain.py 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import cv2
  2. import numpy as np
  3. import utlis
  4. import os
  5. ########################################################################
  6. webCamFeed = True
  7. pathImage = "SchachBrett2.jpg"
  8. cap = cv2.VideoCapture()
  9. #cap = cv2.VideoCapture(1)
  10. cap.set(10,160)
  11. heightImg = 640
  12. widthImg = 480
  13. BASE = os.path.dirname(__file__)
  14. ########################################################################
  15. utlis.initializeTrackbars()
  16. count=0
  17. while True:
  18. img = cv2.imread(BASE+ "/"+ pathImage)
  19. img = cv2.resize(img, (widthImg, heightImg)) # RESIZE IMAGE
  20. imgBlank = np.zeros((heightImg,widthImg, 3), np.uint8) # CREATE A BLANK IMAGE FOR TESTING DEBUGING IF REQUIRED
  21. imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # CONVERT IMAGE TO GRAY SCALE
  22. imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # ADD GAUSSIAN BLUR
  23. thres=utlis.valTrackbars() # GET TRACK BAR VALUES FOR THRESHOLDS
  24. imgThreshold = cv2.Canny(imgBlur,thres[0],thres[1]) # APPLY CANNY BLUR
  25. kernel = np.ones((5, 5))
  26. imgDial = cv2.dilate(imgThreshold, kernel, iterations=2) # APPLY DILATION
  27. imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # APPLY EROSION
  28. ## FIND ALL COUNTOURS
  29. imgContours = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES
  30. imgBigContour = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES
  31. #contours =cv2.findChessboardCorners(img, (8,8),corners, cv2.CALIB_CB_ADAPTIVE_THRESH )
  32. contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS
  33. cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS
  34. # FIND THE BIGGEST COUNTOUR
  35. biggest, maxArea = utlis.biggestContour(contours) # FIND THE BIGGEST CONTOUR
  36. if biggest.size != 0:
  37. biggest=utlis.reorder(biggest)
  38. cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20) # DRAW THE BIGGEST CONTOUR
  39. imgBigContour = utlis.drawRectangle(imgBigContour,biggest,2)
  40. pts1 = np.float32(biggest) # PREPARE POINTS FOR WARP
  41. pts2 = np.float32([[0, 0],[widthImg, 0], [0, heightImg],[widthImg, heightImg]]) # PREPARE POINTS FOR WARP
  42. matrix = cv2.getPerspectiveTransform(pts1, pts2)
  43. imgWarpColored = cv2.warpPerspective(img, matrix, (widthImg, heightImg))
  44. #REMOVE 20 PIXELS FORM EACH SIDE
  45. imgWarpColored=imgWarpColored[20:imgWarpColored.shape[0] - 20, 20:imgWarpColored.shape[1] - 20]
  46. imgWarpColored = cv2.resize(imgWarpColored,(widthImg,heightImg))
  47. # APPLY ADAPTIVE THRESHOLD
  48. imgWarpGray = cv2.cvtColor(imgWarpColored,cv2.COLOR_BGR2GRAY)
  49. imgAdaptiveThre= cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 7, 2)
  50. imgAdaptiveThre = cv2.bitwise_not(imgAdaptiveThre)
  51. imgAdaptiveThre=cv2.medianBlur(imgAdaptiveThre,3)
  52. # Image Array for Display
  53. imageArray = ([img,imgGray,imgThreshold,imgContours],
  54. [imgBigContour,imgWarpColored, imgWarpGray,imgAdaptiveThre])
  55. else:
  56. imageArray = ([img,imgGray,imgThreshold,imgContours],
  57. [imgBlank, imgBlank, imgBlank, imgBlank])
  58. # LABELS FOR DISPLAY
  59. lables = [["Original","Gray","Threshold","Contours"],
  60. ["Biggest Contour","Warp Prespective","Warp Gray","Adaptive Threshold"]]
  61. stackedImage = utlis.stackImages(imageArray,0.75,lables)
  62. cv2.imshow("Result",stackedImage)
  63. # SAVE IMAGE WHEN 's' key is pressed
  64. if cv2.waitKey(1) & 0xFF == ord('s'):
  65. cv2.imwrite("Scanned/myImage"+str(count)+".jpg",imgWarpColored)
  66. cv2.rectangle(stackedImage, ((int(stackedImage.shape[1] / 2) - 230), int(stackedImage.shape[0] / 2) + 50),
  67. (1100, 350), (0, 255, 0), cv2.FILLED)
  68. cv2.putText(stackedImage, "Scan Saved", (int(stackedImage.shape[1] / 2) - 200, int(stackedImage.shape[0] / 2)),
  69. cv2.FONT_HERSHEY_DUPLEX, 3, (0, 0, 255), 5, cv2.LINE_AA)
  70. cv2.imshow('Result', stackedImage)
  71. cv2.waitKey(300)
  72. count += 1