detectmarkers segmentation fault error

asked 2018-08-14 19:36:25 -0600

vannem95 gravatar image

updated 2018-08-14 19:37:37 -0600

Opencv version - 3.3.1

Ubuntu 16.04

All i'm trying to do is get the pose of the marker in the test image. I'm getting segmentation fault and i have no idea what the issue might be. Here is my code.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/aruco.hpp"
#include <iostream>
#include <unistd.h>


using namespace std;
using namespace cv;

namespace {
const char* about = "Basic marker detection";
const char* keys  =
        "{d        |       | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
        "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
        "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
        "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16,"
        "DICT_APRILTAG_16h5=17, DICT_APRILTAG_25h9=18, DICT_APRILTAG_36h10=19, DICT_APRILTAG_36h11=20}"
        "{v        |       | Input from video file, if ommited, input comes from camera }"
        "{ci       | 0     | Camera id if input doesnt come from video (-v) }"
        "{c        |       | Camera intrinsic parameters. Needed for camera pose }"
        "{l        | 0.1   | Marker side lenght (in meters). Needed for correct scale in camera pose }"
        "{dp       |       | File of marker detector parameters }"
        "{r        |       | show rejected candidates too }"
        "{refine   |       | Corner refinement: CORNER_REFINE_NONE=0, CORNER_REFINE_SUBPIX=1,"
        "CORNER_REFINE_CONTOUR=2, CORNER_REFINE_APRILTAG=3}";
}

/**
 */
static bool readCameraParameters(string filename, Mat &camMatrix, Mat &distCoeffs) {
    FileStorage fs(filename, FileStorage::READ);
    if(!fs.isOpened())
        return false;
    fs["camera_matrix"] >> camMatrix;
    fs["distortion_coefficients"] >> distCoeffs;
    return true;
}

double fx = 1122.3605940949296;
double cx = 720.000000;
double fy = 1122.3605940949296;
double cy = 540.000000;

double k1 = -0.40065045745851968;
double k2 = 0.33348148287772167;
double p1 = 0.000000;
double p2 = 0.000000;
double k3 = -0.31525722067617584;

Mat camMatrix = (Mat1d(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
Mat distCoeffs = (Mat1d(1, 4) << k1, k2, p1, p2);

/**
 */
static bool readDetectorParameters(string filename, Ptr<aruco::DetectorParameters> &params) {
    FileStorage fs(filename, FileStorage::READ);
    if(!fs.isOpened())
        return false;
    fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
    fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
    fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
    fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
    fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
    fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
    fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
    fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
    fs["minDistanceToBorder"] >> params->minDistanceToBorder;
    fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
    fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
    fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
    fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
    fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
    fs["markerBorderBits"] >> params->markerBorderBits;
    fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
    fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
    fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
    fs["minOtsuStdDev"] >> params->minOtsuStdDev;
    fs["errorCorrectionRate"] >> params->errorCorrectionRate;
    return true;
}



/**
 */
int main() {
    try{


        bool estimatePose = true;
        float markerLength = 0.42;

        Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
        detectorParams->errorCorrectionRate = 0.6;
        detectorParams->adaptiveThreshWinSizeMin = 3;
        detectorParams->adaptiveThreshWinSizeMax = 23;
        detectorParams->adaptiveThreshWinSizeStep = 10;
        detectorParams->adaptiveThreshConstant = 7;
        detectorParams->minMarkerPerimeterRate = 0.03;
        detectorParams->maxMarkerPerimeterRate = 4;
        detectorParams->minCornerDistanceRate = 0.05;
        detectorParams->minDistanceToBorder = 3;
        detectorParams->minMarkerDistanceRate = 0.05;
        detectorParams->cornerRefinementWinSize = 5;
        detectorParams->cornerRefinementMaxIterations = 30;
        detectorParams->cornerRefinementMinAccuracy = 0.1;
        detectorParams->markerBorderBits = 1;
        detectorParams->perspectiveRemovePixelPerCell = 8;
        detectorParams->perspectiveRemoveIgnoredMarginPerCell = 0.13;
        detectorParams->maxErroneousBitsInBorderRate = 0.35;
        detectorParams->minOtsuStdDev = 5;
        detectorParams->errorCorrectionRate = 0.6;



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


        double totalTime = 0;
        int totalIterations = 0;
        unsigned int microseconds = 2e6 ;


        while(1) {
            Mat image, imageCopy;
            // inputVideo.retrieve(image);
            image = imread("/home/mm/Documents/testlib/test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
            double tick = (double)getTickCount();

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

            // detect markers and estimate pose
            std::cout << ...
(more)
edit retag flag offensive close merge delete

Comments

1

can't reproduce the segfault.

why did you swap waitKey() for usleep() ? you won't see anything, this way.

berak gravatar imageberak ( 2018-08-15 00:02:25 -0600 )edit

I had that when i was trying to check if imread was working fine. Later realized that it wasn't the issue. The segfault is happening at "aruco::detectmarkers(..." Were you able to detect the marker in my test image with this code?

vannem95 gravatar imagevannem95 ( 2018-08-15 00:09:16 -0600 )edit

no detection at all (due to the params, and the bad lighting, i guess)

(but i'm using 3.4.2, so who knows..)

berak gravatar imageberak ( 2018-08-15 00:21:32 -0600 )edit

oh well. thanks anyway.

vannem95 gravatar imagevannem95 ( 2018-08-15 00:26:04 -0600 )edit

you should try to run it from gdb now (with a debug build).

berak gravatar imageberak ( 2018-08-15 01:47:10 -0600 )edit