Play the next OpenCV Aruco (Python version)

Source: Internet
Author: User
Tags glob

Simply play the next OpenCV inside the Aruco, with the mobile phone camera, mobile phone installed an IP camera , so that the video can be uploaded to the computer.

First is the calibration, I did not print chessboard, directly on the computer screen display, took 17, probably as follows:

And put a ruler and the like on the mobile phone app, the volume of the next, each lattice is probably 18.1 mm, the chessboard is the board of X 7.

To pip install Opencv-contrib-python, there is an extension module that contains the Aruco

Then calibrate it:

1 ImportCv22 ImportNumPy as NP3 ImportGlob4 ImportMatplotlib.pyplot as Plt5 Importmatplotlib.patches as Patches6 7 8 #find a Checkerboard corner point9 TenCriteria = (CV2. Term_criteria_eps + CV2. Term_criteria_max_iter, 30, 0.001)#threshold Value One #Checker Template Specifications AW = 9#10-1 -H = 6#7-1 - #a checkerboard point in the world coordinate system, for example (0,0,0), (1,0,0), (2,0,0) ..., (8,5,0), minus the z-coordinate, as a two-dimensional matrix theOBJP = Np.zeros ((w*h,3), Np.float32) -Objp[:,:2] = np.mgrid[0:w,0:h]. T.reshape ( -1,2) -OBJP = objp*18.1#18.1 mm -  + #storing world coordinates and image coordinate pairs for checkerboard corner points -Objpoints = []#three-dimensional points in the world coordinate system +Imgpoints = []#two-dimensional points in the image plane A  atImages = Glob.glob ('./chessboard/*.jpg')#List of more than 10 board images taken -  -i = 1 -  forFNameinchImages: -  -IMG =Cv2.imread (fname) inGray =Cv2.cvtcolor (Img,cv2. Color_bgr2gray) -     #find the Checkerboard corner point toRET, corners =Cv2.findchessboardcorners (Gray, (w,h), None) +     #If you find enough pairs, store them . -     ifRET = =True: the         Print("I:", i) *i = i+1 $     Panax NotoginsengCv2.cornersubpix (Gray,corners, (11,11), ( -1,-1), criteria) - objpoints.append (OBJP) the imgpoints.append (Corners) +         #Display corner points on an image A cv2.drawchessboardcorners (IMG, (w,h), corners, ret) theCv2.namedwindow ('findcorners', Cv2. Window_normal) +Cv2.resizewindow ('findcorners', 810, 405) -Cv2.imshow ('findcorners', IMG) $Cv2.waitkey (1) $ cv2.destroyallwindows () - #Percent Calibration -RET, MTX, Dist, rvecs, tvecs =  theCv2.calibratecamera (objpoints, imgpoints, gray.shape[::-1], none, none) - Wuyi  the Print("ret:", ret) - Print("mtx:\n", MTX)#Internal parameter matrix Wu Print("dist:\n", Dist)#distortion coefficient distortion cofficients = (k_1,k_2,p_1,p_2,k_3) - Print("rvecs:\n", Rvecs)#rotation vector # external parameters About Print("tvecs:\n", Tvecs)#translation Vector # external parameters
View Code

In the calibration results, MTX and Dist are useful for Aruco.

Then print the paper containing the Aruco marker, and run the following code to play it:

1 ImportNumPy as NP2 Import Time3 ImportCv24 ImportCv2.aruco as Aruco5 6 #With np.load (' Webcam_calibration_output.npz ') as X:7 #MTX, Dist, _, _ = [X[i] for I in (' MTX ', ' dist ', ' rvecs ', ' tvecs ')]8 9 #MTX =Ten #2946.48 0 1980.53 One #0 2945.41 1129.25 A #0 0 1 -  -MTX =Np.array ([ the[2946.48, 0, 1980.53], -[0, 2945.41, 1129.25], -[0, 0, 1], -         ]) + #when my phone was making a chessboard, the picture size was 4000 x 2250 - #when the IP camera took the video, it was set to x 1080 and the aspect ratio was the same, + #ip Camera When setting the resolution, pay attention. A  at  -dist = Np.array ([0.226317,-1.21478, 0.00170689,-0.000334551, 1.9892] ) -  -Video ="Http://admin:[email protected]:8081/"   #Mobile IP Camera - #The upper right corner modifies the image resolution based on IP address changes generated by IP camera on your phone -  inCap =Cv2. Videocapture (VIDEO) -  to  +Font = Cv2. Font_hershey_simplex#font for displaying text (below) -  the #num = 0 *  whileTrue: $RET, frame =Cap.read ()Panax Notoginseng     #operations on the frame come here -      theGray =Cv2.cvtcolor (frame, cv2. Color_bgr2gray) +Aruco_dict =Aruco. Dictionary_get (Aruco. DICT_6X6_250) AParameters =Aruco. Detectorparameters_create () the    +     " " - detectmarkers (...) $ detectmarkers (image, dictionary[, corners[, ids[, parameters[, Rejectedi $ Mgpoints]]) Corners, IDs, Rejectedimgpoints -     " "   -        the     #lists of IDs and the corners beloning to each ID -Corners, IDs, rejectedimgpoints =aruco.detectmarkers (Gray,Wuyi Aruco_dict, theparameters=parameters) -    Wu #if ids! = None: -     ifIds is  notNone: About            $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 A          + #Aruco.drawaxis (frame, MTX, Dist, Rvec, Tvec, 0.1) #Draw Axis the #aruco.drawdetectedmarkers (frame, corners) #Draw A Square around the markers -          $          forIinchRange (Rvec.shape[0]): theAruco.drawaxis (frame, MTX, Dist, rvec[i,:,:], Tvec[i,:,:], 0.03) the aruco.drawdetectedmarkers (frame, corners) the         ###### DRAW ID ##### the #Cv2.puttext (Frame, "Id:" + str (IDS), (0,64), font, 1, (0,255,0), 2,cv2. LINE_AA) -    in    the     Else:   the         ## # # # DRAW "NO IDS" ##### AboutCv2.puttext (Frame,"No Ids", (0,64), font, 1, (0,255,0), 2, Cv2. LINE_AA) the    the     #Display the resulting frame theCv2.imshow ("Frame", frame) +      -Key = Cv2.waitkey (1) the     Bayi     ifKey = = 27:#Press the ESC key to exit the         Print('ESC Break ...')   the cap.release () - cv2.destroyallwindows () -          Break the      the     ifKey = = Ord (' '):#Press SPACEBAR to save the #num = num + 1 the #filename = "frames_%s.jpg"% num # saves an image -filename = str (time.time ()) [: 10] +". jpg"   theCv2.imwrite (filename, frame)
View Code

The final effect is as follows:

Play the next OpenCV Aruco (Python version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.