Skip to content

Commit 4528aa3

Browse files
committed
Add cv.seamlessClone, bindings and tests
1 parent 4425af5 commit 4528aa3

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed

cc/photo/photo.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ NAN_MODULE_INIT(Photo::Init) {
1212
Nan::SetMethod(target, "fastNlMeansDenoisingColoredAsync", FastNlMeansDenoisingColoredAsync);
1313
Nan::SetMethod(target, "inpaint", Inpaint);
1414
Nan::SetMethod(target, "inpaintAsync", InpaintAsync);
15+
Nan::SetMethod(target, "seamlessClone", SeamlessClone);
16+
Nan::SetMethod(target, "seamlessCloneAsync", SeamlessCloneAsync);
1517
};
1618

1719
NAN_METHOD(Photo::FastNlMeansDenoisingColored) {
@@ -46,4 +48,20 @@ NAN_METHOD(Photo::InpaintAsync) {
4648
);
4749
}
4850

51+
NAN_METHOD(Photo::SeamlessClone) {
52+
FF::executeSyncBinding(
53+
std::make_shared<PhotoBindings::SeamlessCloningWorker>(),
54+
"Photo::SeamlessClone",
55+
info
56+
);
57+
}
58+
59+
NAN_METHOD(Photo::SeamlessCloneAsync) {
60+
FF::executeAsyncBinding(
61+
std::make_shared<PhotoBindings::SeamlessCloningWorker>(),
62+
"Photo::SeamlessCloneAsync",
63+
info
64+
);
65+
}
66+
4967
#endif

cc/photo/photo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Photo {
1414
static NAN_METHOD(FastNlMeansDenoisingColoredAsync);
1515
static NAN_METHOD(Inpaint);
1616
static NAN_METHOD(InpaintAsync);
17+
static NAN_METHOD(SeamlessClone);
18+
static NAN_METHOD(SeamlessCloneAsync);
1719
};
1820

1921
#endif

cc/photo/photoBindings.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,40 @@ namespace PhotoBindings {
8686
return Mat::Converter::wrap(dst);
8787
}
8888
};
89-
89+
90+
struct SeamlessCloningWorker : public CatchCvExceptionWorker {
91+
public:
92+
// required function arguments
93+
cv::Mat src;
94+
cv::Mat dst;
95+
cv::Mat mask;
96+
cv::Point2d p;
97+
int flags;
98+
99+
// return value
100+
cv::Mat blend;
101+
102+
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
103+
return (
104+
Mat::Converter::arg(0, &src, info) ||
105+
Mat::Converter::arg(1, &dst, info) ||
106+
Mat::Converter::arg(2, &mask, info) ||
107+
Point2::Converter::arg(3, &p, info) ||
108+
FF::IntConverter::arg(4, &flags, info)
109+
);
110+
}
111+
112+
std::string executeCatchCvExceptionWorker() {
113+
cv::seamlessClone(
114+
src, dst, mask, p, blend, flags
115+
);
116+
return "";
117+
}
118+
119+
v8::Local<v8::Value> getReturnValue() {
120+
return Mat::Converter::wrap(blend);
121+
}
122+
};
90123

91124
}
92125

cc/photo/photoConstants.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ using namespace cv;
99
void PhotoConstants::Init(v8::Local<v8::Object> target) {
1010
FF_SET_CV_CONSTANT(target, INPAINT_NS);
1111
FF_SET_CV_CONSTANT(target, INPAINT_TELEA);
12+
FF_SET_CV_CONSTANT(target, NORMAL_CLONE);
13+
FF_SET_CV_CONSTANT(target, MIXED_CLONE);
14+
FF_SET_CV_CONSTANT(target, MONOCHROME_TRANSFER);
1215
}
1316

1417
#endif

lib/typings/constants.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ export const LINE_4: number;
435435
export const LINE_8: number;
436436
export const LINE_AA: number;
437437
export const LMEDS: number;
438+
export const MIXED_CLONE: number;
439+
export const MONOCHROME_TRANSFER: number;
438440
export const MORPH_BLACKHAT: number;
439441
export const MORPH_CLOSE: number;
440442
export const MORPH_CROSS: number;
@@ -455,6 +457,7 @@ export const NORM_L2SQR: number;
455457
export const NORM_MINMAX: number;
456458
export const NORM_RELATIVE: number;
457459
export const NORM_const_MASK: number;
460+
export const NORMAL_CLONE: number;
458461
export const RANSAC: number;
459462
export const REGULAR: number;
460463
export const RETR_CCOMP: number;

lib/typings/cv.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export function reduce(mat: Mat, dim: number, rtype: number, dtype?: number): Ma
163163
export function reduceAsync(mat: Mat, dim: number, rtype: number, dtype?: number): Promise<Mat>;
164164
export function sampsonDistance(pt1: Vec2, pt2: Vec2, F: Mat): number;
165165
export function sampsonDistanceAsync(pt1: Vec2, pt2: Vec2, F: Mat): Promise<number>;
166+
export function seamlessClone(src: Mat, dst: Mat, mask: Mat, p: Point2, flags: number): Mat;
167+
export function seamlessCloneAsync(src: Mat, dst: Mat, mask: Mat, p: Point2, flags: number): Promise<Mat>;
166168
export function solve(mat: Mat, mat2: Mat, flags?: number): Mat;
167169
export function solveAsync(mat: Mat, mat2: Mat, flags?: number): Promise<Mat>;
168170
export function solveP3P(objectPoints: Point3[], imagePoints: Point2[], cameraMatrix: Mat, distCoeffs: number[], flags?: number): { returnValue: boolean, rvecs: Mat[], tvecs: Mat[] };

test/tests/photo/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,38 @@ module.exports = function ({ cv, utils }) {
5959
done();
6060
}).catch(done);
6161
});
62-
})
62+
});
63+
64+
describe('seamlessClone', () => {
65+
66+
it('should have constants', () => {
67+
expect(isNaN(cv.NORMAL_CLONE)).to.be.equal(false);
68+
expect(isNaN(cv.MIXED_CLONE)).to.be.equal(false);
69+
expect(isNaN(cv.MONOCHROME_TRANSFER)).to.be.equal(false);
70+
});
71+
72+
const src = new cv.Mat(5, 5, cv.CV_8UC3, [128, 128, 128]);
73+
const dest = new cv.Mat(10, 10, cv.CV_8UC3, [32, 32, 32]);
74+
const mask = new cv.Mat(10, 10, cv.CV8U, 255);
75+
const center = new cv.Point(5, 5);
76+
const cloneType = cv.NORMAL_CLONE;
77+
78+
const expectOutput = (res) => {
79+
assertMetaData(res)(dest.rows, dest.cols, cv.CV_8UC3);
80+
};
81+
82+
generateAPITests({
83+
getDut: () => cv,
84+
methodName: 'seamlessClone',
85+
getRequiredArgs: () => ([
86+
src,
87+
dest,
88+
mask,
89+
center,
90+
cloneType
91+
]),
92+
expectOutput
93+
});
94+
});
6395

6496
};

0 commit comments

Comments
 (0)