botpeddler
Posts: 11
Joined: Thu May 25, 2017 8:13 pm

OpenCV / Aruco / RaspiCam errors

Mon Jul 02, 2018 6:35 am

Hello,
I am trying to detect Aruco markers using OpenCV. At first I tried a logitech USB webcam and it worked ok, but the resolution was small and the framerate was slow. I couldn't set the resolution higher even though the camera natively supports it. Anyway, I tried a Raspberry Pi Camera 2 with the RaspiCam C++ library. I'm having two issues:
One is I'm having these errors on compile time:

Code: Select all

/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4558 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4543 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4531 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4569 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4577 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4524 >= 4040 for section `.strtab'
/usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4557 >= 4040 for section `.strtab'
It still runs for a bit, but then crashes with this error:

Code: Select all

OpenCV Error: Assertion failed ((((objectPoints) != __null && (((const CvMat*)(objectPoints))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(objectPoints))->cols > 0 && ((const CvMat*)(objectPoints))->rows > 0) && ((const CvMat*)(objectPoints))->data.ptr != __null) && (((imagePoints) != __null && (((const CvMat*)(imagePoints))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(imagePoints))->cols > 0 && ((const CvMat*)(imagePoints))->rows > 0) && ((const CvMat*)(imagePoints))->data.ptr != __null) && (((A) != __null && (((const CvMat*)(A))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(A))->cols > 0 && ((const CvMat*)(A))->rows > 0) && ((const CvMat*)(A))->data.ptr != __null) && (((rvec) != __null && (((const CvMat*)(rvec))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(rvec))->cols > 0 && ((const CvMat*)(rvec))->rows > 0) && ((const CvMat*)(rvec))->data.ptr != __null) && (((tvec) != __null && (((const CvMat*)(tvec))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(tvec))->cols > 0 && ((const CvMat*)(tvec))->rows > 0) && ((const CvMat*)(tvec))->data.ptr != __null)) in cvFindExtrinsicCameraParams2, file /home/pi/opencv/modules/calib3d/src/calibration.cpp, line 996
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/pi/opencv/modules/calib3d/src/calibration.cpp:996: error: (-215) (((objectPoints) != __null && (((const CvMat*)(objectPoints))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(objectPoints))->cols > 0 && ((const CvMat*)(objectPoints))->rows > 0) && ((const CvMat*)(objectPoints))->data.ptr != __null) && (((imagePoints) != __null && (((const CvMat*)(imagePoints))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(imagePoints))->cols > 0 && ((const CvMat*)(imagePoints))->rows > 0) && ((const CvMat*)(imagePoints))->data.ptr != __null) && (((A) != __null && (((const CvMat*)(A))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(A))->cols > 0 && ((const CvMat*)(A))->rows > 0) && ((const CvMat*)(A))->data.ptr != __null) && (((rvec) != __null && (((const CvMat*)(rvec))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(rvec))->cols > 0 && ((const CvMat*)(rvec))->rows > 0) && ((const CvMat*)(rvec))->data.ptr != __null) && (((tvec) != __null && (((const CvMat*)(tvec))->type & 0xFFFF0000) == 0x42420000 && ((const CvMat*)(tvec))->cols > 0 && ((const CvMat*)(tvec))->rows > 0) && ((const CvMat*)(tvec))->data.ptr != __null) in function cvFindExtrinsicCameraParams2
I'm on the Raspberry Pi 3 B+, has anyone else worked with the RaspiCam C++ API?

Here is the code I tried to convert from the Aruco sample to using RaspiCam:

Code: Select all

int main(int argc, char *argv[]) {
    raspicam::RaspiCam_Cv Camera;
    CommandLineParser parser(argc, argv, keys);
    parser.about(about);

    if(argc < 2) {
        parser.printMessage();
        return 0;
    }

    int i=0;
    foundCounter = 0;

    int dictionaryId = parser.get<int>("d");
    bool showRejected = parser.has("r");
    bool estimatePose = parser.has("c");
    float markerLength = parser.get<float>("l");

    Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
    if(parser.has("dp")) {
        bool readOk = readDetectorParameters(parser.get<string>("dp"), detectorParams);
        if(!readOk) {
            cerr << "Invalid detector parameters file" << endl;
            return 0;
        }
    }
    detectorParams->cornerRefinementMethod = aruco::CORNER_REFINE_SUBPIX; // do corner refinement in markers

    int camId = parser.get<int>("ci");

    String video;
    if(parser.has("v")) {
        video = parser.get<String>("v");
    }

    if(!parser.check()) {
        parser.printErrors();
        return 0;
    }

    Ptr<aruco::Dictionary> dictionary =
        aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));

    Mat camMatrix, distCoeffs;
    if(estimatePose) {
        /*
	bool readOk = readCameraParameters(parser.get<string>("c"), camMatrix, distCoeffs);
        if(!readOk) {
            cerr << "Invalid camera file" << endl;
            return 0;
        }
	*/
	//Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
	Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 );
	if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}

    }

    VideoCapture inputVideo;
    inputVideo.set(CV_CAP_PROP_FRAME_WIDTH,1920);
    inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT,1080);
    int waitTime;
    if(!video.empty()) {
        inputVideo.open(video);
        waitTime = 0;
    } else {
        inputVideo.open(camId);
        waitTime = 10;
    }

    double totalTime = 0;
    int totalIterations = 0;
    //while(inputVideo.grab()) {
    while(Camera.grab()){
        Mat image, imageCopy;
        Camera.retrieve ( image);
        //inputVideo.retrieve(image);

        double tick = (double)getTickCount();

        vector< int > ids;
        vector< vector< Point2f > > corners, rejected;
        vector< Vec3d > rvecs, tvecs;

        // detect markers and estimate pose
        aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected);
        if(estimatePose && ids.size() > 0)

            aruco::estimatePoseSingleMarkers(corners, markerLength, camMatrix, distCoeffs, rvecs,
                                             tvecs);
        double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
        totalTime += currentTime;
        totalIterations++;
        
        // draw results
        image.copyTo(imageCopy);
        if(ids.size() > 0) {
            aruco::drawDetectedMarkers(imageCopy, corners, ids);
        double x = tvecs[0][0];
        double y = tvecs[0][1];
        double z = tvecs[0][2];

            if(estimatePose) {
                for(unsigned int i = 0; i < ids.size(); i++)
                    aruco::drawAxis(imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i],
                                    markerLength * 0.5f);
            }
            
        }

        
        if(showRejected && rejected.size() > 0)
            aruco::drawDetectedMarkers(imageCopy, rejected, noArray(), Scalar(100, 0, 255));
        
        imshow("out", imageCopy);
        char key = (char)waitKey(waitTime);
        if(key == 27) break;
    }

    return 0;
}

Nanaki
Posts: 1
Joined: Thu Jul 12, 2018 3:22 pm

Re: OpenCV / Aruco / RaspiCam errors

Thu Jul 12, 2018 3:28 pm

Howdy,

I can have a guess. Without knowing the API inside out it looks to me like you are not checking the images that are being returned from the stream. I'd add something like

if(!image.empty())
{
}

Just where you think you have an image from your stream as its possible that there might be no image when you check for one.(assuming threads and queues etc)

I wanna tell you to assume nothing while I'm making assumptions. That's where I'd start!

Return to “C/C++”