Skip to content

Commit 967872c

Browse files
Merge pull request #236 from findie/feature/inpaint
photo inpaint
2 parents c328205 + 375fc2a commit 967872c

File tree

9 files changed

+152
-0
lines changed

9 files changed

+152
-0
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"cc/ExternalMemTracking.cc",
2323
"cc/cvTypes/cvTypes.cc",
2424
"cc/cvTypes/imgprocConstants.cc",
25+
"cc/cvTypes/photoConstants.cc",
2526
"cc/cvTypes/videoCaptureProps.cc",
2627
"cc/core/core.cc",
2728
"cc/core/Mat.cc",

cc/cvTypes/cvTypes.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include "cvTypes.h"
22
#include "matTypes.h"
33
#include "imgprocConstants.h"
4+
#include "photoConstants.h"
45
#include "videoCaptureProps.h"
56

67
using namespace cv;
78

89
void CvTypes::Init(v8::Local<v8::Object> target) {
910
initMatTypes(target);
1011
ImgprocConstants::Init(target);
12+
PhotoConstants::Init(target);
1113
VideoCaptureProps::Init(target);
1214

1315
FF_SET_CV_CONSTANT(target, NORM_INF);

cc/cvTypes/photoConstants.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "photoConstants.h"
2+
3+
using namespace cv;
4+
5+
void PhotoConstants::Init(v8::Local<v8::Object> target) {
6+
FF_SET_CV_CONSTANT(target, INPAINT_NS);
7+
FF_SET_CV_CONSTANT(target, INPAINT_TELEA);
8+
}

cc/cvTypes/photoConstants.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "macros.h"
2+
#include <opencv2/core.hpp>
3+
#include <opencv2/imgproc.hpp>
4+
#include <opencv2/photo.hpp>
5+
6+
#ifndef __FF_PHOTOCONSTANTS_H__
7+
#define __FF_PHOTOCONSTANTS_H__
8+
9+
class PhotoConstants {
10+
public:
11+
static void Init(v8::Local<v8::Object> target);
12+
};
13+
14+
#endif //__FF_PHOTOCONSTANTS_H__

cc/modules/photo.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
NAN_MODULE_INIT(Photo::Init) {
77
Nan::SetMethod(target, "fastNlMeansDenoisingColored", FastNlMeansDenoisingColored);
8+
Nan::SetMethod(target, "inpaint", Inpaint);
9+
Nan::SetMethod(target, "inpaintAsync", InpaintAsync);
810
};
911

1012
NAN_METHOD(Photo::FastNlMeansDenoisingColored) {
@@ -30,4 +32,59 @@ NAN_METHOD(Photo::FastNlMeansDenoisingColored) {
3032
FF_OBJ jsDst = FF_NEW_INSTANCE(Mat::constructor);
3133
cv::fastNlMeansDenoisingColored(src, FF_UNWRAP_MAT_AND_GET(jsDst), h, hColor, templateWindowSize, searchWindowSize);
3234
FF_RETURN(jsDst);
35+
}
36+
37+
struct Photo::InpaintWorker : public SimpleWorker {
38+
public:
39+
cv::Mat src;
40+
InpaintWorker(cv::Mat self) {
41+
this->src = self;
42+
}
43+
44+
// required function arguments
45+
cv::Mat inpaintMask;
46+
double inpaintRadius;
47+
int flags;
48+
49+
// function return value
50+
cv::Mat dst;
51+
52+
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
53+
return (
54+
Mat::Converter::arg(1, &inpaintMask, info) ||
55+
DoubleConverter::arg(2, &inpaintRadius, info) ||
56+
IntConverter::arg(3, &flags, info)
57+
);
58+
}
59+
60+
const char* execute() {
61+
cv::inpaint(
62+
src, inpaintMask, dst,
63+
inpaintRadius, flags
64+
);
65+
return "";
66+
}
67+
68+
FF_VAL getReturnValue() {
69+
return Mat::Converter::wrap(dst);
70+
}
71+
};
72+
73+
NAN_METHOD(Photo::Inpaint) {
74+
FF_METHOD_CONTEXT("Photo::Inpaint");
75+
76+
FF_ARG_INSTANCE(0, cv::Mat src, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
77+
78+
InpaintWorker worker(src);
79+
FF_WORKER_SYNC("Photo::Inpaint", worker);
80+
info.GetReturnValue().Set(worker.getReturnValue());
81+
}
82+
83+
NAN_METHOD(Photo::InpaintAsync) {
84+
FF_METHOD_CONTEXT("Photo::InpaintAsync");
85+
86+
FF_ARG_INSTANCE(0, cv::Mat src, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
87+
88+
InpaintWorker worker(src);
89+
FF_WORKER_ASYNC("Photo::InpaintAsync", InpaintWorker, worker);
3390
}

cc/modules/photo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Photo {
1010
static NAN_MODULE_INIT(Init);
1111
static NAN_METHOD(FastNlMeansDenoisingColored);
1212

13+
struct InpaintWorker;
14+
static NAN_METHOD(Inpaint);
15+
static NAN_METHOD(InpaintAsync);
1316
};
1417

1518
#endif

lib/typings/constants.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ export const VIDEOWRITER_PROP_NSTRIPES: number;
479479
export const VIDEOWRITER_PROP_QUALITY: number;
480480
export const WARP_FILL_OUTLIERS: number;
481481
export const WARP_INVERSE_MAP: number;
482+
export const INPAINT_NS: number;
483+
export const INPAINT_TELEA: number;
482484

483485
export const HAAR_EYE: string;
484486
export const HAAR_EYE_TREE_EYEGLASSES: string;

lib/typings/cv.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function estimateAffine3DAsync(from: Point2[], to: Point2[], method?: num
4949
export function estimateAffinePartial2D(from: Point2[], to: Point2[], method?: number, ransacReprojThreshold?: number, maxIters?: number, confidence?: number, refineIters?: number): { out: Mat, inliers: Mat };
5050
export function estimateAffinePartial2DAsync(from: Point2[], to: Point2[], method?: number, ransacReprojThreshold?: number, maxIters?: number, confidence?: number, refineIters?: number): Promise<{ out: Mat, inliers: Mat }>;
5151
export function fastNlMeansDenoisingColored(src: Mat, h?: number, hColor?: number, templateWindowSize?: number, searchWindowSize?: number): Mat;
52+
export function inpaint(src: Mat, mask: Mat, inpaintRadius: number, flags: number): Mat;
53+
export function inpaintAsync(src: Mat, mask: Mat, inpaintRadius: number, flags: number): Promise<Mat>;
5254
export function findEssentialMat(points1: Point2[], points2: Point2[], focal?: number, pp?: Point2, method?: number, prob?: number, threshold?: number): { E: Mat, mask: Mat };
5355
export function findEssentialMatAsync(points1: Point2[], points2: Point2[], focal?: number, pp?: Point2, method?: number, prob?: number, threshold?: number): Promise<{ E: Mat, mask: Mat }>;
5456
export function findFundamentalMat(points1: Point2[], points2: Point2[], method?: number, param1?: number, param2?: number): { F: Mat, mask: Mat };
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const cv = global.dut;
2+
const {
3+
assertError,
4+
assertPropsWithValue,
5+
assertMetaData,
6+
funcShouldRequireArgs,
7+
readTestImage,
8+
generateAPITests
9+
} = global.utils;
10+
const { expect } = require('chai');
11+
12+
describe('photo', () => {
13+
14+
describe('inpaint', () => {
15+
16+
it('should have constants', () => {
17+
expect(isNaN(cv.INPAINT_TELEA)).to.be.equal(false);
18+
expect(isNaN(cv.INPAINT_NS)).to.be.equal(false);
19+
});
20+
21+
22+
it('should perform inpainting', () => {
23+
24+
// construct a black image with a white dot in the middle
25+
const imgData = new Array(7*7).fill(0);
26+
imgData[7 * 3 + 3] = 255;
27+
const image = new cv.Mat(new Buffer(imgData), 7,7,cv.CV_8U);
28+
// construct the mask from the same image (since we want to inpaint the white dot)
29+
const mask = image.copy();
30+
31+
// perform inpainting
32+
const inpainted = cv.inpaint(image, mask, 3, cv.INPAINT_TELEA);
33+
34+
// now the result should be all black
35+
const sum = inpainted.sum();
36+
37+
// so sum should be 0
38+
expect(sum).to.be.equal(0);
39+
});
40+
41+
it('should perform inpainting async', (done) => {
42+
43+
// construct a black image with a white dot in the middle
44+
const imgData = new Array(7*7).fill(0);
45+
imgData[7 * 3 + 3] = 255;
46+
const image = new cv.Mat(new Buffer(imgData), 7,7,cv.CV_8U);
47+
// construct the mask from the same image (since we want to inpaint the white dot)
48+
const mask = image.copy();
49+
50+
// perform inpainting
51+
cv.inpaintAsync(image, mask, 3, cv.INPAINT_TELEA)
52+
.then(inpainted => {
53+
// now the result should be all black
54+
const sum = inpainted.sum();
55+
56+
// so sum should be 0
57+
expect(sum).to.be.equal(0);
58+
59+
done();
60+
}).catch(done);
61+
});
62+
})
63+
});

0 commit comments

Comments
 (0)