我正在尝试将 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);
}