mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Merge pull request #491 from woolite64/addReadImageMulti
Add readImageMulti that returns array of matrices via imreadmulti
This commit is contained in:
commit
c867fb32a8
BIN
examples/files/distanceTransform.png
Normal file
BIN
examples/files/distanceTransform.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 166 B |
BIN
examples/files/multipage.tif
Normal file
BIN
examples/files/multipage.tif
Normal file
Binary file not shown.
@ -4,6 +4,9 @@
|
||||
#define CONST(C) \
|
||||
obj->Set(Nan::New<String>(#C).ToLocalChecked(), Nan::New<Integer>(C));
|
||||
|
||||
#define CONST_INT(C) \
|
||||
obj->Set(Nan::New<String>(#C).ToLocalChecked(), Nan::New<Integer>((int)C));
|
||||
|
||||
#define CONST_DOUBLE(C) \
|
||||
obj->Set(Nan::New<String>(#C).ToLocalChecked(), Nan::New<Number>(C));
|
||||
|
||||
@ -90,6 +93,14 @@ void Constants::Init(Local<Object> target) {
|
||||
CONST_ENUM(RETR_CCOMP);
|
||||
CONST_ENUM(RETR_TREE);
|
||||
|
||||
CONST_INT(CV_DIST_C);
|
||||
CONST_INT(CV_DIST_L1);
|
||||
CONST_INT(CV_DIST_L2);
|
||||
|
||||
CONST_INT(CV_DIST_MASK_3);
|
||||
CONST_INT(CV_DIST_MASK_5);
|
||||
CONST_INT(CV_DIST_MASK_PRECISE);
|
||||
|
||||
target->Set(Nan::New("Constants").ToLocalChecked(), obj);
|
||||
}
|
||||
|
||||
|
||||
@ -9,11 +9,44 @@ void ImgProc::Init(Local<Object> target) {
|
||||
Nan::SetMethod(obj, "undistort", Undistort);
|
||||
Nan::SetMethod(obj, "initUndistortRectifyMap", InitUndistortRectifyMap);
|
||||
Nan::SetMethod(obj, "remap", Remap);
|
||||
Nan::SetMethod(obj, "distanceTransform", DistanceTransform);
|
||||
Nan::SetMethod(obj, "getStructuringElement", GetStructuringElement);
|
||||
|
||||
target->Set(Nan::New("imgproc").ToLocalChecked(), obj);
|
||||
}
|
||||
|
||||
// cv::distanceTransform
|
||||
NAN_METHOD(ImgProc::DistanceTransform) {
|
||||
Nan::EscapableHandleScope scope;
|
||||
|
||||
try {
|
||||
// Arg 0 is the image
|
||||
Matrix* m0 = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());
|
||||
cv::Mat inputImage = m0->mat;
|
||||
|
||||
// Arg 1 is the distance type (CV_DIST_L1, CV_DIST_L2, etc.)
|
||||
int distType = info[1]->IntegerValue();;
|
||||
|
||||
// Make a mat to hold the result image
|
||||
cv::Mat outputImage;
|
||||
|
||||
// Perform distance transform
|
||||
cv::distanceTransform(inputImage, outputImage, distType, 0);
|
||||
|
||||
// Wrap the output image
|
||||
Local<Object> outMatrixWrap = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
|
||||
Matrix *outMatrix = Nan::ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
|
||||
outMatrix->mat = outputImage;
|
||||
|
||||
// Return the output image
|
||||
info.GetReturnValue().Set(outMatrixWrap);
|
||||
} catch (cv::Exception &e) {
|
||||
const char *err_msg = e.what();
|
||||
Nan::ThrowError(err_msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// cv::undistort
|
||||
NAN_METHOD(ImgProc::Undistort) {
|
||||
Nan::EscapableHandleScope scope;
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
class ImgProc: public Nan::ObjectWrap {
|
||||
public:
|
||||
static void Init(Local<Object> target);
|
||||
static NAN_METHOD(DistanceTransform);
|
||||
static NAN_METHOD(Undistort);
|
||||
static NAN_METHOD(InitUndistortRectifyMap);
|
||||
static NAN_METHOD(Remap);
|
||||
|
||||
@ -11,6 +11,7 @@ void OpenCV::Init(Local<Object> target) {
|
||||
target->Set(Nan::New<String>("version").ToLocalChecked(), Nan::New<String>(out, n).ToLocalChecked());
|
||||
|
||||
Nan::SetMethod(target, "readImage", ReadImage);
|
||||
Nan::SetMethod(target, "readImageMulti", ReadImageMulti);
|
||||
}
|
||||
|
||||
NAN_METHOD(OpenCV::ReadImage) {
|
||||
@ -66,3 +67,54 @@ NAN_METHOD(OpenCV::ReadImage) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#if CV_MAJOR_VERSION >= 3
|
||||
NAN_METHOD(OpenCV::ReadImageMulti) {
|
||||
Nan::EscapableHandleScope scope;
|
||||
|
||||
REQ_FUN_ARG(1, cb);
|
||||
|
||||
Local<Value> argv[2];
|
||||
argv[0] = Nan::Null();
|
||||
|
||||
std::vector<cv::Mat> mats;
|
||||
try {
|
||||
if (info[0]->IsString()) {
|
||||
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
|
||||
cv::imreadmulti(filename, mats);
|
||||
|
||||
if (mats.empty()) {
|
||||
argv[0] = Nan::Error("Error loading file");
|
||||
}
|
||||
}
|
||||
} catch (cv::Exception& e) {
|
||||
argv[0] = Nan::Error(e.what());
|
||||
argv[1] = Nan::Null();
|
||||
}
|
||||
|
||||
Local <Array> output = Nan::New<Array>(mats.size());
|
||||
argv[1] = output;
|
||||
|
||||
for (std::vector<cv::Mat>::size_type i = 0; i < mats.size(); i ++) {
|
||||
Local<Object> im_h = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
|
||||
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_h);
|
||||
img->mat = mats[i];
|
||||
|
||||
output->Set(i, im_h);
|
||||
}
|
||||
|
||||
Nan::TryCatch try_catch;
|
||||
cb->Call(Nan::GetCurrentContext()->Global(), 2, argv);
|
||||
|
||||
if (try_catch.HasCaught()) {
|
||||
Nan::FatalException(try_catch);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#else
|
||||
NAN_METHOD(OpenCV::ReadImageMulti) {
|
||||
info.GetReturnValue().Set(Nan::New<Boolean>(false));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -67,6 +67,7 @@ public:
|
||||
static void Init(Local<Object> target);
|
||||
|
||||
static NAN_METHOD(ReadImage);
|
||||
static NAN_METHOD(ReadImageMulti);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
30
test/unit.js
30
test/unit.js
@ -216,6 +216,36 @@ test("Image read from file", function(assert){
|
||||
})
|
||||
})
|
||||
|
||||
test("Multi-page image read from file", function(assert){
|
||||
if (parseInt(cv.version) >= 3) {
|
||||
cv.readImageMulti("./examples/files/multipage.tif", function(err, imgs){
|
||||
assert.ok(imgs);
|
||||
assert.equal(imgs.length, 10);
|
||||
for (var i = 0; i < imgs.length; i++) {
|
||||
assert.ok(imgs[i]);
|
||||
assert.equal(imgs[i].width(), 800);
|
||||
assert.equal(imgs[i].height(), 600);
|
||||
assert.equal(imgs[i].channels(), 3);
|
||||
assert.equal(imgs[i].empty(), false);
|
||||
}
|
||||
assert.end();
|
||||
})
|
||||
} else {
|
||||
assert.equal(cv.readImageMulti("./examples/files/multipage.tif"), false);
|
||||
assert.end();
|
||||
}
|
||||
})
|
||||
|
||||
test("Distance transform", function(assert){
|
||||
cv.readImage("./examples/files/distanceTransform.png", function(err, img){
|
||||
assert.ok(img);
|
||||
|
||||
// 50px image with single black pixel on right side
|
||||
var result = cv.imgproc.distanceTransform(img, cv.Constants.CV_DIST_L2, cv.Constants.CV_DIST_MASK_PRECISE);
|
||||
assert.equal(result.get(0,0), 49);
|
||||
assert.end();
|
||||
})
|
||||
})
|
||||
|
||||
test("read Image from buffer", function(assert){
|
||||
cv.readImage(fs.readFileSync('./examples/files/opencv.png'), function(err, im){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user