0 Replies Latest reply on Jan 1, 2017 3:51 PM by braedan

    Need Python OpenCV Code Help

    braedan

      Hello Element14 Community!

           This past week I have been revisiting python 3(.5) with OpenCV 3(.2.0 + contrib) on my Windows 10 PC. However I am working on an basic augmented reality application and i have been trying to use OpenCV's ArUco markers module. So far the documentation of this module with OpenCV has been very challenging to understand and is only explained in C/C++. So after a couple days of research and several educated guesses I figured out some of the basic usage for the module and got it to where it is able to identify the marker, highlight it and then calculate the Pose of the marker and draw its axes in a three dimensional space. However that is as far as i could get on my own with my very limited knowledge of the terminology of this module and OpenCV. I am currently having a few issues with my Python code. For one, my code is only able to draw one markers axis with out giving me a error message:

      Traceback (most recent call last):
        File "C:\Users\bdogk\Desktop\OpenCV\Augmented Reality - v2.py", line 34, in <module>
          aruco.drawAxis(frame, mtx, dist, rvec, tvec, 0.1) #Draw Axis
      cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\calib3d\src\calibration.cpp:599: error: (-5) Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 3x3 rotation matrix in function cvProjectPoints2
      

       

      The program works fine with one marker. How can I have my program work for multiple markers? I hashed out the drawAxis function in my code and it worked and highlighted both markers. Here is my full code:

       

      import numpy as np
      import cv2
      import cv2.aruco as aruco
      cap = cv2.VideoCapture(0)
      
      
      with np.load('webcam_calibration_output.npz') as X:
          mtx, dist, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
          
      while(True):
          ret, frame = cap.read()
          # operations on the frame come here
          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
          aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
          parameters =  aruco.DetectorParameters_create()
      
          '''    detectMarkers(...)
              detectMarkers(image, dictionary[, corners[, ids[, parameters[, rejectedI
              mgPoints]]]]) -> corners, ids, rejectedImgPoints
          '''
          
          #lists of ids and the corners beloning to each id
          corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
      
      
          font = cv2.FONT_HERSHEY_SIMPLEX #font for displaying text (below)
      
      
          if ids != None:
              
              rvec, tvec = aruco.estimatePoseSingleMarkers(corners, 0.05, mtx, dist) #Estimate pose of each marker and return the values rvet and tvec---different from camera coeficcients
              (rvec-tvec).any() # get rid of that nasty numpy value array error
              
              aruco.drawAxis(frame, mtx, dist, rvec, tvec, 0.1) #Draw Axis
              aruco.drawDetectedMarkers(frame, corners) #Draw A square around the markers
      
      
              ###### DRAW ID #####
              cv2.putText(frame, "Id: " + str(ids), (0,64), font, 1, (0,255,0),2,cv2.LINE_AA)
      
      
          else:
              ##### DRAW "NO IDS" #####
              cv2.putText(frame, "No Ids", (0,64), font, 1, (0,255,0),2,cv2.LINE_AA)
      
          # Display the resulting frame
          cv2.imshow('frame',frame)
          if cv2.waitKey(1) & 0xFF == ord('q'):
              break
      
      # When everything done, release the capture
      cap.release()
      cv2.destroyAllWindows()
      

      I also plan on displaying images over the markers but i dont know how to get the coordinates of the markers center, help? Any links or information would be greatly appreciated! Thank you!

       

      P.S. The marker i have been using is attached to this post.