mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Fixed testing for imreadmulti on OpenCV < 3, added distanceTransform function to imgproc module.
This commit is contained in:
parent
cfeb82d9fc
commit
592b70a3c2
BIN
examples/files/distanceTransform.png
Normal file
BIN
examples/files/distanceTransform.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 166 B |
@ -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);
|
||||
|
||||
@ -68,6 +68,7 @@ NAN_METHOD(OpenCV::ReadImage) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if CV_MAJOR_VERSION >= 3
|
||||
NAN_METHOD(OpenCV::ReadImageMulti) {
|
||||
Nan::EscapableHandleScope scope;
|
||||
|
||||
@ -111,3 +112,9 @@ NAN_METHOD(OpenCV::ReadImageMulti) {
|
||||
|
||||
return;
|
||||
}
|
||||
#else
|
||||
NAN_METHOD(OpenCV::ReadImageMulti) {
|
||||
info.GetReturnValue().Set(Nan::New<Boolean>(false));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
37
test/unit.js
37
test/unit.js
@ -217,16 +217,33 @@ test("Image read from file", function(assert){
|
||||
})
|
||||
|
||||
test("Multi-page image read from file", function(assert){
|
||||
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);
|
||||
}
|
||||
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 {
|
||||
console.log(cv.Constants);
|
||||
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();
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user