c++node.jsopencvnode-addon-api

Opencv4nodejs addon for cv::createLineSegmentDetector() function crashed. Exception can only be caught by catch(...). How to debug type of exception?


I am trying to make an integration of cv::LineSegmentDetector to opencv4nodejs which is an node addon of opencv (link: https://github.com/justadudewhohacks/opencv4nodejs). I have successfully complied an opencv4nodejs module which get loaded by require. Initially was facing problems of opencv_core.dll , and opencv_imgproc.dll while loading the opencv4nodejs.node module to js. But cv::createLineSegmentDetector() crashes catch( std::exception &e) does not work but catch(...) exception catches it. But its still does not work.

Here is the code.

#include <nan.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "macros.h"

class LineSegmentDetector : public Nan::ObjectWrap
{
public:
    cv::Ptr<cv::LineSegmentDetector> m_plsd;

    static NAN_MODULE_INIT(Init);
    static NAN_METHOD(New);
    static NAN_METHOD(Detect);
    static Nan::Persistent<v8::FunctionTemplate> constructor;
};

Here is the CC file.

#include "LineSegmentDetector.h"
#include "Mat.h"
#include "Vec4.h"

Nan::Persistent<v8::FunctionTemplate> LineSegmentDetector::constructor;
// Nan::Persistent<v8::FunctionTemplate> Mat::constructor;
// Nan::Persistent<v8::FunctionTemplate> Vec4::constructor;

NAN_MODULE_INIT(LineSegmentDetector::Init)
{
    v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(LineSegmentDetector::New);
    constructor.Reset(ctor);
    ctor->InstanceTemplate()->SetInternalFieldCount(1);
    ctor->SetClassName(Nan::New("LineSegmentDetector").ToLocalChecked());

    Nan::SetPrototypeMethod(ctor, "detect", LineSegmentDetector::Detect);

    target->Set(Nan::New("LineSegmentDetector").ToLocalChecked(), ctor->GetFunction());
}

std::string handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try
    {
        if (eptr)
        {
            std::rethrow_exception(eptr);
        }
    }
    catch (const std::exception &e)
    {
        return std::string("Caught exception \"") + e.what() + std::string("\"\n");
    }
}

NAN_METHOD(LineSegmentDetector::New)
{
    // throw an error if constructor is called without new keyword
    if (!info.IsConstructCall())
    {
        return Nan::ThrowError(Nan::New("LineSegmentDetector::New - called without new keyword").ToLocalChecked());
    }
    LineSegmentDetector *self = new LineSegmentDetector();
    if (self == nullptr)
    {
        return Nan::ThrowError(Nan::New("LineSegmentDetector::New - self was null").ToLocalChecked());
    }
    try
    {
        self->m_plsd = cv::createLineSegmentDetector();
    }
    catch (...)
    {
        // std::exception_ptr eptr = std::current_exception();
        return Nan::ThrowError(Nan::New("LineSegmentDetector::New - cv::createLineSegmentDetector failed").ToLocalChecked());
    }
    self->Wrap(info.Holder());

    // return the wrapped javascript instance
    info.GetReturnValue().Set(info.Holder());
}

NAN_METHOD(LineSegmentDetector::Detect)
{
    FF::TryCatch tryCatch("LineSegmentDetector::Detect");
    cv::Mat gray;
    if (Mat::Converter::arg(0, &gray, info))
    {
        return tryCatch.reThrow();
    }

    LineSegmentDetector *self = Nan::ObjectWrap::Unwrap<LineSegmentDetector>(info.This());
    std::vector<cv::Vec4f> lines;
    self->m_plsd->detect(gray, lines);
    v8::Local<v8::Value> val = Vec4::ArrayWithCastConverter<cv::Vec4f>::wrap(lines);
    info.GetReturnValue().Set(val);
}

Solution

  • As I understood there is no problem with my code the implementation is not present in version 3.4.

       LineSegmentDetectorImpl::LineSegmentDetectorImpl(int _refine, double _scale, double _sigma_scale, double _quant,
    double _ang_th, double _log_eps, double _density_th, int _n_bins)
    {
    CV_Assert(_scale > 0 && _sigma_scale > 0 && _quant >= 0 &&
    _ang_th > 0 && _ang_th < 180 && _density_th >= 0 && _density_th < 1 &&
    _n_bins > 0);
    CV_UNUSED(_refine); CV_UNUSED(_log_eps);
    CV_Error(Error::StsNotImplemented, "Implementation has been removed due original code license issues");
    }