Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit bb12146

Browse files
committed
Add SuperpixelSLIC functionality
1 parent e681901 commit bb12146

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"cc/modules/imgproc/Moments.cc",
4343
"cc/modules/ximgproc/ximgproc.cc",
4444
"cc/modules/ximgproc/SuperpixelSEEDS.cc",
45+
"cc/modules/ximgproc/SuperpixelSLIC.cc",
4546
"cc/modules/objdetect/objdetect.cc",
4647
"cc/modules/objdetect/CascadeClassifier.cc",
4748
"cc/modules/objdetect/HOGDescriptor.cc",

cc/modules/ximgproc/SuperpixelSLIC.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifdef HAVE_XIMGPROC
2+
3+
#include "SuperpixelSLIC.h"
4+
5+
Nan::Persistent<v8::FunctionTemplate> SuperpixelSLIC::constructor;
6+
7+
NAN_MODULE_INIT(SuperpixelSLIC::Init) {
8+
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(SuperpixelSLIC::New);
9+
v8::Local<v8::ObjectTemplate> instanceTemplate = ctor->InstanceTemplate();
10+
constructor.Reset(ctor);
11+
instanceTemplate->SetInternalFieldCount(1);
12+
ctor->SetClassName(Nan::New("SuperpixelSLIC").ToLocalChecked());
13+
14+
Nan::SetAccessor(instanceTemplate, Nan::New("img").ToLocalChecked(), SuperpixelSLIC::GetImg);
15+
Nan::SetAccessor(instanceTemplate, Nan::New("labels").ToLocalChecked(), SuperpixelSLIC::GetLabels);
16+
Nan::SetAccessor(instanceTemplate, Nan::New("labelContourMask").ToLocalChecked(), SuperpixelSLIC::GetLabelContourMask);
17+
Nan::SetAccessor(instanceTemplate, Nan::New("algorithm").ToLocalChecked(), SuperpixelSLIC::GetAlgorithm);
18+
Nan::SetAccessor(instanceTemplate, Nan::New("regionSize").ToLocalChecked(), SuperpixelSLIC::GetRegionSize);
19+
Nan::SetAccessor(instanceTemplate, Nan::New("ruler").ToLocalChecked(), SuperpixelSLIC::GetRuler);
20+
Nan::SetAccessor(instanceTemplate, Nan::New("numCalculatedSuperpixels").ToLocalChecked(), SuperpixelSLIC::GetNumCalculatedSuperpixels);
21+
22+
Nan::SetPrototypeMethod(ctor, "iterate", SuperpixelSLIC::Iterate);
23+
24+
target->Set(Nan::New("SuperpixelSLIC").ToLocalChecked(), ctor->GetFunction());
25+
};
26+
27+
NAN_METHOD(SuperpixelSLIC::New) {
28+
FF_METHOD_CONTEXT("SuperpixelSLIC::New");
29+
if (!info.IsConstructCall()) {
30+
FF_THROW("expected new key word");
31+
}
32+
SuperpixelSLIC* self = new SuperpixelSLIC();
33+
34+
FF_ARG_INSTANCE(0, self->img, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
35+
FF_ARG_INT(1, self->algorithm);
36+
FF_ARG_INT_IFDEF(2, self->regionSize, 10);
37+
FF_ARG_INT_IFDEF(3, self->ruler, 10.0);
38+
39+
self->Wrap(info.Holder());
40+
self->superpixelSlic = cv::ximgproc::createSuperpixelSLIC(
41+
self->img,
42+
self->algorithm,
43+
self->regionSize,
44+
self->ruler
45+
);
46+
info.GetReturnValue().Set(info.Holder());
47+
}
48+
49+
NAN_METHOD(SuperpixelSLIC::Iterate) {
50+
FF_METHOD_CONTEXT("SuperpixelSLIC::Iterate");
51+
52+
FF_ARG_UINT_IFDEF(0, uint iterations, 10);
53+
54+
SuperpixelSLIC* self = FF_UNWRAP(info.This(), SuperpixelSLIC);
55+
self->superpixelSlic->iterate((int)iterations);
56+
self->superpixelSlic->getLabels(self->labels);
57+
self->numCalculatedSuperpixels = self->superpixelSlic->getNumberOfSuperpixels();
58+
self->superpixelSlic->getLabelContourMask(self->labelContourMask, false);
59+
}
60+
61+
#endif // HAVE_XIMGPROC

cc/modules/ximgproc/SuperpixelSLIC.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <opencv2/ximgproc.hpp>
2+
#include "Mat.h"
3+
#include "macros.h"
4+
5+
#ifndef __FF_SUPERPIXELSLIC_H__
6+
#define __FF_SUPERPIXELSLIC_H__
7+
8+
class SuperpixelSLIC : public Nan::ObjectWrap {
9+
public:
10+
cv::Ptr<cv::ximgproc::SuperpixelSLIC> superpixelSlic;
11+
cv::Mat img;
12+
cv::Mat labels;
13+
cv::Mat labelContourMask;
14+
int algorithm;
15+
int regionSize = 10;
16+
float ruler = 10.0;
17+
int numCalculatedSuperpixels = 0;
18+
19+
static NAN_MODULE_INIT(Init);
20+
static NAN_METHOD(New);
21+
static NAN_METHOD(Iterate);
22+
23+
static FF_GETTER_JSOBJ(SuperpixelSLIC, GetImg, img, FF_UNWRAP_MAT_AND_GET, Mat::constructor);
24+
static FF_GETTER_JSOBJ(SuperpixelSLIC, GetLabels, labels, FF_UNWRAP_MAT_AND_GET, Mat::constructor);
25+
static FF_GETTER_JSOBJ(SuperpixelSLIC, GetLabelContourMask, labelContourMask, FF_UNWRAP_MAT_AND_GET, Mat::constructor);
26+
static FF_GETTER(SuperpixelSLIC, GetAlgorithm, algorithm);
27+
static FF_GETTER(SuperpixelSLIC, GetRegionSize, regionSize);
28+
static FF_GETTER(SuperpixelSLIC, GetRuler, ruler);
29+
static FF_GETTER(SuperpixelSLIC, GetNumCalculatedSuperpixels, numCalculatedSuperpixels);
30+
31+
static Nan::Persistent<v8::FunctionTemplate> constructor;
32+
};
33+
34+
#endif

cc/modules/ximgproc/ximgproc.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include "ximgproc.h"
44
#include "SuperpixelSEEDS.h"
5+
#include "SuperpixelSLIC.h"
56

67
NAN_MODULE_INIT(XImgproc::Init) {
78
SuperpixelSEEDS::Init(target);
9+
SuperpixelSLIC::Init(target);
810
}
911

1012
#endif // HAVE_XIMGPROC

0 commit comments

Comments
 (0)