Skip to content

Commit

Permalink
undistort first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
piercus committed May 6, 2019
1 parent 1652718 commit 4241a8c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
19 changes: 19 additions & 0 deletions cc/core/MatImgproc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ void MatImgproc::Init(v8::Local<v8::FunctionTemplate> ctor) {
Nan::SetPrototypeMethod(ctor, "cornerEigenValsAndVecsAsync", CornerEigenValsAndVecsAsync);
Nan::SetPrototypeMethod(ctor, "integral", Integral);
Nan::SetPrototypeMethod(ctor, "integralAsync", IntegralAsync);
Nan::SetPrototypeMethod(ctor, "undistort", Undistort);
Nan::SetPrototypeMethod(ctor, "undistortAsync", UndistortAsync);
};

NAN_METHOD(MatImgproc::DrawContours) {
Expand Down Expand Up @@ -993,3 +995,20 @@ NAN_METHOD(MatImgproc::IntegralAsync) {
info
);
}

NAN_METHOD(MatImgproc::Undistort) {
FF::SyncBinding(
std::make_shared<MatImgprocBindings::UndistortWorker>(Mat::Converter::unwrap(info.This())),
"Mat::Undistort",
info
);
}

NAN_METHOD(MatImgproc::UndistortAsync) {
FF::AsyncBinding(
std::make_shared<MatImgprocBindings::UndistortWorker>(Mat::Converter::unwrap(info.This())),
"Mat::UndistortAsync",
info
);
}

3 changes: 2 additions & 1 deletion cc/core/MatImgproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ class MatImgproc {
static NAN_METHOD(CornerEigenValsAndVecsAsync);
static NAN_METHOD(Integral);
static NAN_METHOD(IntegralAsync);

static NAN_METHOD(Undistort);
static NAN_METHOD(UndistortAsync);
};

#endif
48 changes: 48 additions & 0 deletions cc/core/MatImgprocBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,54 @@ namespace MatImgprocBindings {
}
};

struct UndistortWorker: public CatchCvExceptionWorker {
public:
cv::Mat mat;
cv::Size size;

UndistortWorker(cv::Mat mat) {
this->mat = mat;
this->size = cv::Size2d(mat.cols, mat.rows);
}

cv::Mat cameraMatrix;
cv::Mat distCoeffs;
cv::Mat newCameraMatrix;
cv::Mat undistortedMat;

FF_VAL getReturnValue() {
return Mat::Converter::wrap(undistortedMat);
}

bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return (
Mat::Converter::arg(0, &cameraMatrix, info) ||
Mat::Converter::arg(1, &distCoeffs, info)
);
}

bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return (
Mat::Converter::arg(2, &newCameraMatrix, info)
);
}

bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) {
return FF_ARG_IS_OBJECT(2);
}

bool unwrapOptionalArgsFromOpts(Nan::NAN_METHOD_ARGS_TYPE info) {
FF_OBJ opts = info[2]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
return (
Mat::Converter::optProp(&newCameraMatrix, "newCameraMatrix", opts)
);
}
std::string executeCatchCvExceptionWorker() {
cv::undistort(mat, undistortedMat, cameraMatrix, distCoeffs, newCameraMatrix);
return "";
}

};

}

Expand Down
15 changes: 14 additions & 1 deletion test/tests/core/Mat/Mat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,5 +1054,18 @@ describe('Mat', () => {
describe('Solve y = x equation on Id = X Id', makeTest([[1, 0, 0],[0, 1, 0],[0, 0, 1]], [[1, 0, 0],[0, 1, 0],[0, 0, 1]], cv.DECOMP_LU, [[1, 0, 0],[0, 1, 0],[0, 0, 1]]));
describe('Solve y = x equation on Id = X Id', makeTest([[1, 2],[3, 4]], [[5, 6],[7, 8]], cv.DECOMP_LU, [[-3, -4],[4, 5]]));
});

describe('undistort', () => {
const cameraMatrix = new cv.Mat([[1, 0, 10],[0, 1, 10],[0, 0, 1]], cv.CV_32F);
const newCameraMatrix = new cv.Mat([[0.5, 0, 10],[0, 0.5, 10],[0, 0, 1]], cv.CV_32F);
const distCoeffs = new cv.Mat([[0.1, 0.1, 1, 1]], cv.CV_32F);
generateAPITests({
getDut: () => new cv.Mat(20, 20, cv.CV_8U, 0.5),
methodName: 'undistort',
methodNameSpace: 'Mat',
getRequiredArgs: () => ([cameraMatrix, distCoeffs]),
getOptionalArgs: () => ([
newCameraMatrix
])
});
});
});
32 changes: 32 additions & 0 deletions test/tests/modules/imgproc/imgproc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,37 @@ describe('imgproc', () => {
expectOutput: res => expect(res).to.be.instanceOf(cv.Mat)
});
});

describe('undistortPoints', () => {
const cameraMatrix = new cv.Mat([[1, 0, 10],[0, 1, 10],[0, 0, 1]], cv.CV_32F);
const newCameraMatrix = new cv.Mat([[0.5, 0, 10],[0, 0.5, 10],[0, 0, 1]], cv.CV_32F);
const distCoeffs = new cv.Mat([[0.1, 0.1, 1, 1]], cv.CV_32F);
const srcPoints = [
[5,5] [5, 10], [5, 15],
[10,5], [10,10], 10, 15,
[15,5], [15,10], [15, 15]
].map(([x,y]) => new cv.Point(x,y))
const destPoints = [
[5,5] [5, 10], [5, 15],
[10,5], [10,10], 10, 15,
[15,5], [15,10], [15, 15]
].map(([x,y]) => new cv.Point(x,y))

generateAPITests({
getDut: () => cv,
methodName: 'undistortPoints',
getRequiredArgs: () => ([
srcPoints,
cameraMatrix,
distCoeffs
]),
getOptionalArgs: () => ([
newCameraMatrix
])
hasAsync: false,
usesMacroInferno: true,
expectOutput: destPoints => expect(destPoints).to.deepEqual(expectedDestPoints)
});
});
});
});

0 comments on commit 4241a8c

Please sign in to comment.