Opencv version - 3.3.1
Ubuntu 16.04
All i'm trying to do is detect 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> ¶ms) {
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 << "here" << std::endl;
imshow("out", image.clone());
usleep(microseconds);
aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected);
std::cout << "here" << std::endl;
if(estimatePose && ids.size() > 0)
aruco::estimatePoseSingleMarkers(corners, markerLength, camMatrix, distCoeffs, rvecs,
tvecs);
double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
totalTime += currentTime;
totalIterations++;
if(totalIterations % 30 == 0) {
cout << "Detection Time = " << currentTime * 1000 << " ms "
<< "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl;
}
// draw results
image.copyTo(imageCopy);
if(ids.size() > 0) {
aruco::drawDetectedMarkers(imageCopy, corners, ids);
if(estimatePose) {
for(unsigned int i = 0; i < ids.size(); i++)
aruco::drawAxis(imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i],
markerLength * 0.5f);
}
}
imshow("out", imageCopy);
}
}catch(std::exception &ex)
{
cout<<"Exception :"<<ex.what()<<endl;
}
return 0;
}
Please help.