我正在尝试将cv::LineSegmentDetector集成到opencv4nodejs,这是opencv的节点附加组件(链接:https://github.com/justadudewhohacks/opencv4nodejs)。我已经成功编译了一个由require加载的opencv4nodejs模块。最初将opencv4nodejs.node模块加载到js时遇到了opencv_core.dll和opencv_imgproc.dll的问题。但是cv::createLineSegmentDetector()崩溃catch(std::exception&e)不起作用,但是catch(...)异常捕获它。但是它仍然不起作用。

这是代码。

#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;
};

这是CC文件。
#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);
}

最佳答案

据我了解,我的代码没有问题,该实现在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");
}

关于c++ - cv::createLineSegmentDetector()函数的Opencv4nodejs插件崩溃。异常只能由catch(…)捕获。如何调试异常类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61579780/

10-10 00:45