0

I just built aruco 1.3.0 and the aruco_test works perfect.

So far I tried to fetch the marker with Opencv + kinect v2. The marker is detected but doesn't have rotation and translation. Anyone has met similar questions before?

#include <iostream>

// OpenCV Header
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>  
#include "opencv2/imgproc/imgproc.hpp"
#include <aruco.h>

// Kinect for Windows SDK Header
#include <Kinect.h>

using namespace std; 
using namespace aruco;
using namespace cv;

float TheMarkerSize = -1;
int ThePyrDownLevel;
MarkerDetector MDetector; 
vector< Marker > TheMarkers; 
CameraParameters TheCameraParameters;   


int main(int argc, char** argv)
{
    TheCameraParameters.readFromXMLFile("camera.yml"); 
    if (ThePyrDownLevel > 0)
        MDetector.pyrDown(ThePyrDownLevel);


    MDetector.setThresholdParams(7, 7);
    MDetector.setThresholdParamRange(2, 0); 


    // 1a. Get default Sensor
    cout << "Try to get default sensor" << endl;
    IKinectSensor* pSensor = nullptr;
    if (GetDefaultKinectSensor(&pSensor) != S_OK)
    {
        cerr << "Get Sensor failed" << endl;
        return -1;
    } 

    // 1b. Open sensor
    cout << "Try to open sensor" << endl;
    if (pSensor->Open() != S_OK)
    {
        cerr << "Can't open sensor" << endl;
        return -1;
    }

    // 2a. Get frame source
    cout << "Try to get color source" << endl;
    IColorFrameSource* pFrameSource = nullptr;
    if (pSensor->get_ColorFrameSource(&pFrameSource) != S_OK)
    {
        cerr << "Can't get color frame source" << endl;
        return -1;
    }

    // 2b. Get frame description
    cout << "get color frame description" << endl;
    int     iWidth = 0;
    int     iHeight = 0;
    IFrameDescription* pFrameDescription = nullptr;
    if (pFrameSource->get_FrameDescription(&pFrameDescription) == S_OK)
    {
        pFrameDescription->get_Width(&iWidth);
        pFrameDescription->get_Height(&iHeight); 
    }
    pFrameDescription->Release();
    pFrameDescription = nullptr;

    // 3a. get frame reader
    cout << "Try to get color frame reader" << endl;
    IColorFrameReader* pFrameReader = nullptr;
    if (pFrameSource->OpenReader(&pFrameReader) != S_OK)
    { 
        cerr << "Can't get color frame reader" << endl;
        return -1;
    }

    // 2c. release Frame source
    cout << "Release frame source" << endl;
    pFrameSource->Release();
    pFrameSource = nullptr;

    // Prepare OpenCV data
    cv::Mat mImg(iHeight, iWidth, CV_8UC4);
    cv::Mat mImg2(iHeight, iWidth, CV_8UC1);
    UINT uBufferSize = iHeight * iWidth * 4 * sizeof(BYTE);
    cv::namedWindow("Color Map");

    // Enter main loop
    while (true)
    {
        // 4a. Get last frame
        IColorFrame* pFrame = nullptr;
        if (pFrameReader->AcquireLatestFrame(&pFrame) == S_OK)
        {
            cout << GetTickCount() << endl;
            // 4c. Copy to OpenCV image
            if (pFrame->CopyConvertedFrameDataToArray(uBufferSize, mImg.data, ColorImageFormat_Bgra) == S_OK)
            {
                cvtColor(mImg, mImg2, CV_BGR2GRAY);
                //camParam.resize(mImg2.size());
                MDetector.detect(mImg2, TheMarkers, TheCameraParameters, TheMarkerSize);

                if (TheMarkers.size() > 0) {
                    //for each marker, draw info and its boundaries in the image
                    for (unsigned int i = 0;i<TheMarkers.size();i++) { 
                        cout << TheMarkers[i] << endl;
                        TheMarkers[i].draw(mImg2, Scalar(0, 0, 255), 1);

                    } 
                }
                if (TheCameraParameters.isValid())
                    for (unsigned int i = 0; i < TheMarkers.size(); i++) {
                        CvDrawingUtils::draw3dCube(mImg2, TheMarkers[i], TheCameraParameters);
                        CvDrawingUtils::draw3dAxis(mImg2, TheMarkers[i], TheCameraParameters);
                    }

                cv::imshow("Color Map", mImg2);
            }
            else
            {
                cerr << "Data copy error" << endl;
            }

            // 4e. release frame
            pFrame->Release();
        }

        // 4f. check keyboard input
        if (cv::waitKey(30) == VK_ESCAPE) {
            break;
        }
    }

    // 3b. release frame reader
    cout << "Release frame reader" << endl;
    pFrameReader->Release();
    pFrameReader = nullptr;

    // 1c. Close Sensor
    cout << "close sensor" << endl;
    pSensor->Close();

    // 1d. Release Sensor
    cout << "Release sensor" << endl;
    pSensor->Release();
    pSensor = nullptr;

    return 0;
}

The result looks like

637=(934.56,481.291) (763.227,482.706) (737.609,222.863) (907.926,266.524) Txyz=
-999999 -999999 -999999 Rxyz=-999999 -999999 -999999
87746890
637=(934.503,481.388) (763.178,482.765) (737.533,223.02) (907.796,266.664) Txyz=
-999999 -999999 -999999 Rxyz=-999999 -999999 -999999
87747390
637=(934.145,481.07) (762.71,482.473) (737.016,222.63) (907.335,266.239) Txyz=-9
99999 -999999 -999999 Rxyz=-999999 -999999 -999999
Tony Luo
  • 110
  • 1
  • 9

3 Answers3

0

Sorry for interruption.

I just set MarkerSize to positive value ( 0.05f in my case, -1 by default ). Any rotation and translation appear!

Tony Luo
  • 110
  • 1
  • 9
0

Rvec and Tvec first have to be initialised. You can do this by calling the calculateExtrinsics() function in marker. If you set your markers size in the detect() function of MarkerDetector, the calculateExtrinsics() function will automatically be called.

0

You need to calibrate the camera using the ChArUco Boards or just ArUco Boards, Once you get the output file camera.yml. You can use that with your current app. Without the calibration file Txyz and Rxyz will not be computed.