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

Commit 069a26d

Browse files
Merge pull request justadudewhohacks#425 from oyyd/colormap
imgproc: implement applyColorMap
2 parents 0e5da61 + e54e859 commit 069a26d

File tree

9 files changed

+172
-11
lines changed

9 files changed

+172
-11
lines changed

cc/cvTypes/cvTypes.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,18 @@ void CvTypes::Init(v8::Local<v8::Object> target) {
209209
FF_SET_CV_CONSTANT(target, ROTATE_180);
210210
FF_SET_CV_CONSTANT(target, ROTATE_90_COUNTERCLOCKWISE);
211211
#endif
212+
213+
FF_SET_CV_CONSTANT(target, COLORMAP_AUTUMN);
214+
FF_SET_CV_CONSTANT(target, COLORMAP_BONE);
215+
FF_SET_CV_CONSTANT(target, COLORMAP_JET);
216+
FF_SET_CV_CONSTANT(target, COLORMAP_WINTER);
217+
FF_SET_CV_CONSTANT(target, COLORMAP_RAINBOW);
218+
FF_SET_CV_CONSTANT(target, COLORMAP_OCEAN);
219+
FF_SET_CV_CONSTANT(target, COLORMAP_SUMMER);
220+
FF_SET_CV_CONSTANT(target, COLORMAP_SPRING);
221+
FF_SET_CV_CONSTANT(target, COLORMAP_COOL);
222+
FF_SET_CV_CONSTANT(target, COLORMAP_HSV);
223+
FF_SET_CV_CONSTANT(target, COLORMAP_PINK);
224+
FF_SET_CV_CONSTANT(target, COLORMAP_HOT);
225+
FF_SET_CV_CONSTANT(target, COLORMAP_PARULA);
212226
}

cc/modules/imgproc/imgproc.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ NAN_MODULE_INIT(Imgproc::Init) {
2929
Nan::SetMethod(target, "getPerspectiveTransform", GetPerspectiveTransform);
3030
Nan::SetMethod(target, "getTextSize", GetTextSize);
3131
Nan::SetMethod(target, "getTextSizeAsync", GetTextSizeAsync);
32+
Nan::SetMethod(target, "applyColorMap", ApplyColorMap);
33+
Nan::SetMethod(target, "applyColorMapAsync", ApplyColorMapAsync);
3234
#if CV_VERSION_MINOR > 1
3335
Nan::SetMethod(target, "canny", Canny);
3436
#endif
@@ -171,10 +173,10 @@ NAN_METHOD(Imgproc::Plot1DHist) {
171173

172174
FF_ARG_INSTANCE(0, cv::Mat hist, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
173175
FF_ARG_INSTANCE(1, cv::Mat plot, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
174-
FF_ARG_INSTANCE(2, cv::Vec3d color, Vec3::constructor, FF_UNWRAP_VEC3_AND_GET);
175-
if (1 != hist.cols) {
176-
return Nan::ThrowError(FF_NEW_STRING("Plot1DHist - hist rows mismatch, expected "
177-
+ std::to_string(1) + ", have " + std::to_string(hist.cols)));
176+
FF_ARG_INSTANCE(2, cv::Vec3d color, Vec3::constructor, FF_UNWRAP_VEC3_AND_GET);
177+
if (1 != hist.cols) {
178+
return Nan::ThrowError(FF_NEW_STRING("Plot1DHist - hist rows mismatch, expected "
179+
+ std::to_string(1) + ", have " + std::to_string(hist.cols)));
178180
}
179181
if (hist.channels() != 1) {
180182
FF_THROW("expected hist to be single channeled");
@@ -292,3 +294,14 @@ NAN_METHOD(Imgproc::Canny) {
292294
FF_RETURN(jsMat);
293295
}
294296
#endif
297+
298+
299+
NAN_METHOD(Imgproc::ApplyColorMap) {
300+
FF::SyncBinding(std::make_shared<ImgprocBindings::ApplyColorMapWorker>(),
301+
"Imgproc::ApplyColorMap", info);
302+
}
303+
304+
NAN_METHOD(Imgproc::ApplyColorMapAsync) {
305+
FF::AsyncBinding(std::make_shared<ImgprocBindings::ApplyColorMapWorker>(),
306+
"Imgproc::ApplyColorMapAsync", info);
307+
}

cc/modules/imgproc/imgproc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Imgproc {
2626
#if CV_VERSION_MINOR > 1
2727
static NAN_METHOD(Canny);
2828
#endif
29+
static NAN_METHOD(ApplyColorMap);
30+
static NAN_METHOD(ApplyColorMapAsync);
2931
};
3032

3133
#endif

cc/modules/imgproc/imgprocBindings.h

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "imgproc.h"
2+
#include <opencv2/imgproc.hpp>
23

34
#ifndef __FF_IMGPROCBINDINGS_H_
45
#define __FF_IMGPROCBINDINGS_H_
@@ -11,22 +12,22 @@ namespace ImgprocBindings {
1112
int fontFace;
1213
double fontScale;
1314
int thickness;
14-
15+
1516
cv::Size2d returnValue;
1617
int baseLine;
17-
18+
1819
std::string executeCatchCvExceptionWorker() {
1920
returnValue = cv::getTextSize(text, fontFace, fontScale, thickness, &baseLine);
2021
return "";
2122
}
22-
23+
2324
v8::Local<v8::Value> getReturnValue() {
2425
v8::Local<v8::Object> ret = Nan::New<v8::Object>();
2526
Nan::Set(ret, Nan::New("size").ToLocalChecked(), Size::Converter::wrap(returnValue));
2627
Nan::Set(ret, Nan::New("baseLine").ToLocalChecked(), IntConverter::wrap(baseLine));
2728
return ret;
2829
}
29-
30+
3031
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
3132
return (
3233
StringConverter::arg(0, &text, info) ||
@@ -36,8 +37,52 @@ namespace ImgprocBindings {
3637
);
3738
}
3839
};
39-
4040

41+
struct ApplyColorMapWorker : public CatchCvExceptionWorker {
42+
public:
43+
cv::Mat src;
44+
cv::Mat dst;
45+
cv::Mat userColor;
46+
int colormap;
47+
bool useUserColor = 0;
48+
49+
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
50+
#if CV_VERSION_MINOR < 3
51+
return (Mat::Converter::arg(0, &src, info) ||
52+
IntConverter::optArg(1, &colormap, info));
53+
#endif
54+
55+
#if CV_VERSION_MINOR >= 3
56+
if (info[1]->IsNumber()) {
57+
return (Mat::Converter::arg(0, &src, info) ||
58+
IntConverter::optArg(1, &colormap, info));
59+
}
60+
61+
useUserColor = 1;
62+
63+
return (Mat::Converter::arg(0, &src, info) ||
64+
Mat::Converter::arg(1, &userColor, info));
65+
#endif
66+
}
67+
68+
std::string executeCatchCvExceptionWorker() {
69+
#if CV_VERSION_MINOR < 3
70+
cv::applyColorMap(src, dst, colormap);
71+
return "";
72+
#endif
73+
74+
#if CV_VERSION_MINOR >= 3
75+
if (useUserColor) {
76+
cv::applyColorMap(src, dst, userColor);
77+
} else {
78+
cv::applyColorMap(src, dst, colormap);
79+
}
80+
return "";
81+
#endif
82+
}
83+
84+
v8::Local<v8::Value> getReturnValue() { return Mat::Converter::wrap(dst); }
85+
};
4186
}
4287

43-
#endif
88+
#endif

examples/applyColorMap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path');
2+
const cv = require('../');
3+
4+
const image = cv.imread(path.resolve(__dirname, '../data/Lenna.png'));
5+
6+
const processedImage = cv.applyColorMap(image, cv.COLORMAP_AUTUMN);
7+
8+
cv.imshowWait("applyColorMap", processedImage);

lib/typings/constants.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,19 @@ export const COLOR_YUV420sp2GRAY: number;
352352
export const COLOR_YUV420sp2RGB: number;
353353
export const COLOR_YUV420sp2RGBA: number;
354354
export const COLOR_mRGBA2RGBA: number;
355+
export const COLORMAP_AUTUMN: number;
356+
export const COLORMAP_BONE: number;
357+
export const COLORMAP_JET: number;
358+
export const COLORMAP_WINTER: number;
359+
export const COLORMAP_RAINBOW: number;
360+
export const COLORMAP_OCEAN: number;
361+
export const COLORMAP_SUMMER: number;
362+
export const COLORMAP_SPRING: number;
363+
export const COLORMAP_COOL: number;
364+
export const COLORMAP_HSV: number;
365+
export const COLORMAP_PINK: number;
366+
export const COLORMAP_HOT: number;
367+
export const COLORMAP_PARULA: number;
355368
export const CV_CONTOURS_MATCH_I1: number;
356369
export const CV_CONTOURS_MATCH_I2: number;
357370
export const CV_CONTOURS_MATCH_I3: number;

lib/typings/cv.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface HistAxes {
1818
ranges: number[];
1919
}
2020

21+
export function applyColorMap(src: Mat, colormap: number | Mat): Mat;
2122
export function blobFromImage(image: Mat, scaleFactor?: number, size?: Size, mean?: Vec3, swapRB?: boolean): Mat;
2223
export function blobFromImageAsync(image: Mat, scaleFactor?: number, size?: Size, mean?: Vec3, swapRB?: boolean): Promise<Mat>;
2324
export function blobFromImages(image: Mat[], scaleFactor?: number, size?: Size, mean?: Vec3, swapRB?: boolean): Mat;
@@ -162,4 +163,4 @@ export function drawTextBox(img: Mat, upperLeft: { x: number, y: number }, textL
162163
export function isCustomMatAllocatorEnabled(): boolean;
163164
export function dangerousEnableCustomMatAllocator(): boolean;
164165
export function dangerousDisableCustomMatAllocator(): boolean;
165-
export function getMemMetrics(): { TotalAlloc: number, TotalKnownByJS: number, NumAllocations: number, NumDeAllocations: number };
166+
export function getMemMetrics(): { TotalAlloc: number, TotalKnownByJS: number, NumAllocations: number, NumDeAllocations: number };
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 { generateAPITests } = global.utils;
3+
const { expect } = require('chai');
4+
5+
module.exports = () => {
6+
describe('applyColorMap', () => {
7+
it('should have all colormap types in the "cv" object', () => {
8+
const COLORMAP_TYPE_NAMES = [
9+
'COLORMAP_AUTUMN',
10+
'COLORMAP_BONE',
11+
'COLORMAP_JET',
12+
'COLORMAP_WINTER',
13+
'COLORMAP_RAINBOW',
14+
'COLORMAP_OCEAN',
15+
'COLORMAP_SUMMER',
16+
'COLORMAP_SPRING',
17+
'COLORMAP_COOL',
18+
'COLORMAP_HSV',
19+
'COLORMAP_PINK',
20+
'COLORMAP_HOT',
21+
'COLORMAP_PARULA',
22+
];
23+
24+
COLORMAP_TYPE_NAMES.forEach((name) => {
25+
expect(typeof cv[name]).to.be.equal('number');
26+
});
27+
});
28+
29+
it('should process an image with a type of colormap integer', () => {
30+
generateAPITests({
31+
getDut: () => cv,
32+
methodName: 'applyColorMap',
33+
getRequiredArgs: () => ([
34+
new cv.Mat([[0, 1, 100]], cv.CV_8UC1),
35+
cv.COLORMAP_HOT,
36+
]),
37+
hasAsync: true,
38+
usesMacroInferno: false,
39+
expectOutput: res => {
40+
return expect(res).to.be.instanceOf(cv.Mat)
41+
},
42+
});
43+
});
44+
45+
if (cv.version.minor >= 3) {
46+
it('should process an image with a customized colormap', () => {
47+
generateAPITests({
48+
getDut: () => cv,
49+
methodName: 'applyColorMap',
50+
getRequiredArgs: () => ([
51+
new cv.Mat([[0, 1, 100]], cv.CV_8UC1),
52+
new cv.Mat(256, 1, cv.CV_8UC3),
53+
]),
54+
hasAsync: true,
55+
usesMacroInferno: false,
56+
expectOutput: res => {
57+
return expect(res).to.be.instanceOf(cv.Mat)
58+
},
59+
});
60+
});
61+
}
62+
});
63+
};

test/tests/modules/imgproc/imgproc.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
} = global.utils;
1010
const { expect } = require('chai');
1111
const contourTests = require('./contourTests');
12+
const colormapTests = require('./colormapTests');
1213

1314
describe('imgproc', () => {
1415
let testImg;
@@ -18,6 +19,7 @@ describe('imgproc', () => {
1819
});
1920

2021
contourTests();
22+
colormapTests();
2123

2224
describe('getStructuringElement', () => {
2325
const rows = 4;

0 commit comments

Comments
 (0)