Search code examples
c++opencvopencv-contrib

OpenCV C++ Error with sinusoidal pattern generation - structured_light library


I am trying to use the sinusoidal pattern tool in C++ with Visual Studio. I have placed the code that I am testing this with below. In visual studio everything looks fine bar the red squiggle under params in the following line:

Ptr<structured_light::SinusoidalPattern> sinus = structured_light::SinusoidalPattern::create(params);

When I try to build I get the following error message:

Severity    Code    Description Project File    Line    Suppression State Error (active)        
no suitable user-defined conversion from 
"cv::structured_light::SinusoidalPattern::Params" to 
"cv::Ptr<cv::structured_light::SinusoidalPattern::Params>" exists   Structured_Light_Test   
c:\Users\ianco\Desktop\CPlusPlus_Programming\Structured_Light_Test\Structured_Light_Test\Main.cpp   70

I would be very grateful if anyone could offer some advice on how I could get round this issue or suggest another method.

CODE:

#include <opencv2/highgui.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/structured_light.hpp>
#include <opencv2/phase_unwrapping.hpp>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    structured_light::SinusoidalPattern::Params params;
    params.width = 1080;
    params.height = 700;
    params.nbrOfPeriods = 5;
    params.setMarkers = true;
    params.horizontal = false;
    params.methodId = 2;
    params.shiftValue = static_cast<float>(2 * CV_PI / 3);
    params.nbrOfPixelsBetweenMarkers = 70;
    String outputPatternPath = "C:/Users/ianco/Desktop/CPlusPlus_Programming";
    String outputWrappedPhasePath = "C:/Users/ianco/Desktop/CPlusPlus_Programming";
    String outputUnwrappedPhasePath = "C:/Users/ianco/Desktop/CPlusPlus_Programming";

    Ptr<structured_light::SinusoidalPattern> sinus = structured_light::SinusoidalPattern::create(params);
    // Storage for patterns
    vector<Mat> patterns;
    //Generate sinusoidal patterns
    sinus->generate(patterns);


    cv::Mat blue, green, red;
    std::vector<cv::Mat> images(3);

    // OpenCV works natively with BGR ordering
    images.at(0) = patterns[0];
    images.at(1) = patterns[1];
    images.at(2) = patterns[2];

    cv::Mat color;
    cv::merge(images, color);

    namedWindow("pattern", WINDOW_NORMAL);
    setWindowProperty("pattern", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
    imshow("pattern", color);
    waitKey(3000);
}

Solution

  • The documentation tells you that params should also be a Ptr but you passed the object...

    try using makePtr

    Change this line:

    structured_light::SinusoidalPattern::Params params;
    

    with this:

    Ptr<cv::structured_light::SinusoidalPattern::Params> params = makePtr< SinusoidalPattern::Params >();
    

    you will have to change . to -> for each use of params like params.width = 1080; would be params->width = 1080;, since it will be a pointer now.

    The rest of the code should be ok.