element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Need Python OpenCV Code Help
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 2 replies
  • Answers 1 answer
  • Subscribers 47 subscribers
  • Views 1560 views
  • Users 0 members are here
  • coding
  • troubleshooting
  • python
  • help
  • python 3
  • Code Exchange
  • opencv
  • code
  • error
Related

Need Python OpenCV Code Help

braedan
braedan over 8 years ago

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.

Attachments:
image
  • Sign in to reply
  • Cancel
  • tutin
    0 tutin over 7 years ago

    Hi Braedan,

    I'm fairly new to OpenCV and found this post of yours. I wanted to know the same, how do I find the coordinates of the markers to augment the marker with an image for starters. Please let me know if you did find a way to do that?

    Thanks in advance!



    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • joian11
    0 joian11 over 6 years ago

    try this bro.. I'm still  working in the pose estimation but at least i took out the multiple marker problem.

     

    if np.all(ids is not None):

            zipped = zip(ids,corners)

            ids, corners = zip(*(sorted(zipped)))

            print(len(ids[0]))

            ret = cv2.aruco.estimatePoseSingleMarkers(corners,10,camera_matrix,camera_distortion)

            rvec, tvec = ret[0][0,0,:], ret[1][0,0,:]

           

            frame = cv2.aruco.drawDetectedMarkers(frame, corners)

       

            cv2.aruco.drawAxis(frame, camera_matrix, camera_distortion,rvec, tvec,10)

              

        else:

            cv2.putText(frame,"no id", (0,20),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube