Skip to content

Commit

Permalink
Merge pull request #339 from jspdown/master
Browse files Browse the repository at this point in the history
Add GetStructuringElement, kernel argument for Erode and Dilate
  • Loading branch information
peterbraden committed Nov 25, 2015
2 parents 794e1ad + 9b6c9a9 commit 9a77841
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
Binary file added examples/files/note.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/remove-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var cv = require('../lib/opencv');

// Load the image
cv.readImage('./files/note.png', function(err, im) {
if (err) {
throw err;
}
if (im.width() < 1 || im.height() < 1) {
throw new Error('Image has no size');
}

im.cvtColor('CV_BGR2GRAY');
var bw = im.adaptiveThreshold(255, 0, 0, 15, 2);
bw.bitwiseNot(bw);

var vertical = bw.clone();

var verticalsize = vertical.size()[0] / 30;
var verticalStructure = cv.imgproc.getStructuringElement(1, [1, verticalsize]);

// Apply morphology operations
vertical.erode(1, verticalStructure);
vertical.dilate(1, verticalStructure);

vertical.bitwiseNot(vertical);
vertical.gaussianBlur([3, 3]);

// Save output image
vertical.save('./tmp/note.png');
});
43 changes: 43 additions & 0 deletions src/ImgProc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void ImgProc::Init(Local<Object> target) {
Nan::SetMethod(obj, "undistort", Undistort);
Nan::SetMethod(obj, "initUndistortRectifyMap", InitUndistortRectifyMap);
Nan::SetMethod(obj, "remap", Remap);
Nan::SetMethod(obj, "getStructuringElement", GetStructuringElement);

target->Set(Nan::New("imgproc").ToLocalChecked(), obj);
}
Expand Down Expand Up @@ -157,3 +158,45 @@ NAN_METHOD(ImgProc::Remap) {
return;
}
}

// cv::getStructuringElement
NAN_METHOD(ImgProc::GetStructuringElement) {
Nan::EscapableHandleScope scope;

try {
// Get the arguments

if (info.Length() != 2) {
Nan::ThrowTypeError("Invalid number of arguments");
}

// Arg 0 is the element shape
if (!info[0]->IsNumber()) {
JSTHROW_TYPE("'shape' argument must be a number");
}
int shape = info[0]->NumberValue();

// Arg 1 is the size of the structuring element
cv::Size ksize;
if (!info[1]->IsArray()) {
JSTHROW_TYPE("'ksize' argument must be a 2 double array");
}
Local<Object> v8sz = info[1]->ToObject();
ksize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue());

// GetStructuringElement
cv::Mat mat = cv::getStructuringElement(shape, ksize);

// Wrap the output image
Local<Object> outMatrixWrap = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *outMatrix = ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = mat;

// Return the image
info.GetReturnValue().Set(outMatrixWrap);
} catch (cv::Exception &e) {
const char *err_msg = e.what();
JSTHROW(err_msg);
return;
}
}
1 change: 1 addition & 0 deletions src/ImgProc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ImgProc: public Nan::ObjectWrap {
static NAN_METHOD(Undistort);
static NAN_METHOD(InitUndistortRectifyMap);
static NAN_METHOD(Remap);
static NAN_METHOD(GetStructuringElement);
};

#endif
15 changes: 13 additions & 2 deletions src/Matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,13 @@ NAN_METHOD(Matrix::Dilate) {
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This());
int niters = info[0]->NumberValue();

cv::dilate(self->mat, self->mat, cv::Mat(), cv::Point(-1, -1), niters);
cv::Mat kernel = cv::Mat();
if (info.Length() == 2) {
Matrix *kernelMatrix = Nan::ObjectWrap::Unwrap<Matrix>(info[1]->ToObject());
kernel = kernelMatrix->mat;
}

cv::dilate(self->mat, self->mat, kernel, cv::Point(-1, -1), niters);

info.GetReturnValue().Set(Nan::Null());
}
Expand All @@ -1352,7 +1358,12 @@ NAN_METHOD(Matrix::Erode) {
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This());
int niters = info[0]->NumberValue();

cv::erode(self->mat, self->mat, cv::Mat(), cv::Point(-1, -1), niters);
cv::Mat kernel = cv::Mat();
if (info.Length() == 2) {
Matrix *kernelMatrix = Nan::ObjectWrap::Unwrap<Matrix>(info[1]->ToObject());
kernel = kernelMatrix->mat;
}
cv::erode(self->mat, self->mat, kernel, cv::Point(-1, -1), niters);

info.GetReturnValue().Set(Nan::Null());
}
Expand Down

0 comments on commit 9a77841

Please sign in to comment.