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.

DetectChessCorners.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import numpy as np
  2. import cv2 as cv
  3. import glob
  4. import os
  5. heightImg = 800
  6. widthImg = 1000
  7. FieldSize = 100
  8. BASE = os.path.dirname(__file__)
  9. pathImage = "SchachBrett2.jpg"
  10. # termination criteria
  11. criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  12. # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  13. objp = np.zeros((7*7,3), np.float32)
  14. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  15. # Arrays to store object points and image points from all the images.
  16. objpoints = [] # 3d point in real world space
  17. imgpoints = [] # 2d points in image plane.
  18. img = cv.imread(BASE+ "/"+ pathImage) #glob.glob('*.jpg')
  19. img = cv.resize(img, (widthImg, heightImg))
  20. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  21. # Find the chess board corners
  22. ret, corners = cv.findChessboardCorners(gray, (7,7), None)
  23. # If found, add object points, image points (after refining them)
  24. if ret == True:
  25. #get Corner Position of the 1. Rank
  26. #print(corners)
  27. leftupper = corners[6][0]
  28. leftlower = corners[48][0]
  29. rightupper = corners[42][0]
  30. rightlower = corners[0][0]
  31. #Transform Image with the founded Corner Position
  32. pts1 = np.float32([rightupper, rightlower, leftupper, leftlower]) # PREPARE POINTS FOR WARP
  33. print(pts1)
  34. pts2 = np.float32([[FieldSize, FieldSize*7], [FieldSize*7, FieldSize*7], [FieldSize*7, FieldSize], [FieldSize, FieldSize]]) # PREPARE POINTS FOR WARP
  35. matrix = cv.getPerspectiveTransform(pts1, pts2)
  36. imgWarpColored = cv.warpPerspective(img, matrix, (FieldSize*8, FieldSize*8))
  37. #Draw founded Chess Corners
  38. imgContours = img.copy()
  39. objpoints.append(objp)
  40. corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
  41. imgpoints.append(corners)
  42. # Draw and display the corners
  43. cv.drawChessboardCorners(img, (7,7), corners2, ret)
  44. else:
  45. ret, corners = cv.findChessboardCorners(gray, (5, 5), None)
  46. #cv.imshow('img', img)
  47. cv.imshow('img', imgWarpColored)
  48. cv.waitKey(500000)