Skip to content

Commit 17144cf

Browse files
Copilotttt43ttt
andauthored
Add TypeScript definitions for applyColorMap function and colormap constants (#88)
* Initial plan * Add applyColorMap TypeScript definitions and tests Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> * Complete applyColorMap implementation with comprehensive tests Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com>
1 parent 8b6120a commit 17144cf

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/types/opencv/_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export * from "./fisheye";
1919
export * from "./FlannBasedMatcher";
2020
export * from "./HOGDescriptor";
2121
export * from "./imgproc_color_conversions";
22+
export * from "./imgproc_colormap";
2223
export * from "./imgproc_draw";
2324
export * from "./imgproc_feature";
2425
export * from "./imgproc_filter";
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { InputArray, int, OutputArray } from "./_types";
2+
3+
/*
4+
* # Colormap Transformations
5+
*
6+
*/
7+
8+
/**
9+
* Applies a colormap on a given image.
10+
*
11+
* @param src The source image, which should be grayscale. Should be 8-bit, 16-bit, or floating-point.
12+
* @param dst The result is the colored image.
13+
* @param colormap The colormap to apply.
14+
*/
15+
export declare function applyColorMap(
16+
src: InputArray,
17+
dst: OutputArray,
18+
colormap: int,
19+
): void;
20+
21+
/**
22+
* Applies a user colormap on a given image.
23+
*
24+
* @param src The source image, which should be grayscale. Should be 8-bit, 16-bit, or floating-point.
25+
* @param dst The result is the colored image.
26+
* @param userColor The colormap to apply of type CV_8UC1 or CV_8UC3 and size 256.
27+
*/
28+
export declare function applyColorMap(
29+
src: InputArray,
30+
dst: OutputArray,
31+
userColor: InputArray,
32+
): void;
33+
34+
/**
35+
* Colormap types used by the applyColorMap function.
36+
*/
37+
export type ColormapTypes = any;
38+
39+
export declare const COLORMAP_AUTUMN: ColormapTypes; // initializer: = 0
40+
export declare const COLORMAP_BONE: ColormapTypes; // initializer: = 1
41+
export declare const COLORMAP_JET: ColormapTypes; // initializer: = 2
42+
export declare const COLORMAP_WINTER: ColormapTypes; // initializer: = 3
43+
export declare const COLORMAP_RAINBOW: ColormapTypes; // initializer: = 4
44+
export declare const COLORMAP_OCEAN: ColormapTypes; // initializer: = 5
45+
export declare const COLORMAP_SUMMER: ColormapTypes; // initializer: = 6
46+
export declare const COLORMAP_SPRING: ColormapTypes; // initializer: = 7
47+
export declare const COLORMAP_COOL: ColormapTypes; // initializer: = 8
48+
export declare const COLORMAP_HSV: ColormapTypes; // initializer: = 9
49+
export declare const COLORMAP_PINK: ColormapTypes; // initializer: = 10
50+
export declare const COLORMAP_HOT: ColormapTypes; // initializer: = 11
51+
export declare const COLORMAP_PARULA: ColormapTypes; // initializer: = 12
52+
export declare const COLORMAP_MAGMA: ColormapTypes; // initializer: = 13
53+
export declare const COLORMAP_INFERNO: ColormapTypes; // initializer: = 14
54+
export declare const COLORMAP_PLASMA: ColormapTypes; // initializer: = 15
55+
export declare const COLORMAP_VIRIDIS: ColormapTypes; // initializer: = 16
56+
export declare const COLORMAP_CIVIDIS: ColormapTypes; // initializer: = 17
57+
export declare const COLORMAP_TWILIGHT: ColormapTypes; // initializer: = 18
58+
export declare const COLORMAP_TWILIGHT_SHIFTED: ColormapTypes; // initializer: = 19
59+
export declare const COLORMAP_TURBO: ColormapTypes; // initializer: = 20
60+
export declare const COLORMAP_DEEPGREEN: ColormapTypes; // initializer: = 21

test/applyColorMap.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { setupOpenCv } from "./cv";
2+
3+
beforeAll(setupOpenCv);
4+
5+
describe("applyColorMap", () => {
6+
it("should apply COLORMAP_JET to a grayscale image", async () => {
7+
// Create a simple grayscale image
8+
const src = new cv.Mat(100, 100, cv.CV_8UC1);
9+
10+
// Fill with gradient values
11+
for (let i = 0; i < 100; i++) {
12+
for (let j = 0; j < 100; j++) {
13+
src.ucharPtr(i, j)[0] = Math.floor((i + j) * 255 / 200);
14+
}
15+
}
16+
17+
const dst = new cv.Mat();
18+
19+
// Apply JET colormap
20+
cv.applyColorMap(src, dst, cv.COLORMAP_JET);
21+
22+
// Verify the output is a 3-channel color image
23+
expect(dst.channels()).toBe(3);
24+
expect(dst.rows).toBe(100);
25+
expect(dst.cols).toBe(100);
26+
expect(dst.type()).toBe(cv.CV_8UC3);
27+
28+
// Clean up
29+
src.delete();
30+
dst.delete();
31+
});
32+
33+
it("should have all COLORMAP constants available", () => {
34+
// Test that all colormap constants are defined
35+
expect(typeof cv.COLORMAP_JET).toBe('number');
36+
expect(typeof cv.COLORMAP_AUTUMN).toBe('number');
37+
expect(typeof cv.COLORMAP_BONE).toBe('number');
38+
expect(typeof cv.COLORMAP_WINTER).toBe('number');
39+
expect(typeof cv.COLORMAP_RAINBOW).toBe('number');
40+
expect(typeof cv.COLORMAP_OCEAN).toBe('number');
41+
expect(typeof cv.COLORMAP_SUMMER).toBe('number');
42+
expect(typeof cv.COLORMAP_SPRING).toBe('number');
43+
expect(typeof cv.COLORMAP_COOL).toBe('number');
44+
expect(typeof cv.COLORMAP_HSV).toBe('number');
45+
expect(typeof cv.COLORMAP_PINK).toBe('number');
46+
expect(typeof cv.COLORMAP_HOT).toBe('number');
47+
expect(typeof cv.COLORMAP_PARULA).toBe('number');
48+
expect(typeof cv.COLORMAP_MAGMA).toBe('number');
49+
expect(typeof cv.COLORMAP_INFERNO).toBe('number');
50+
expect(typeof cv.COLORMAP_PLASMA).toBe('number');
51+
expect(typeof cv.COLORMAP_VIRIDIS).toBe('number');
52+
expect(typeof cv.COLORMAP_CIVIDIS).toBe('number');
53+
expect(typeof cv.COLORMAP_TWILIGHT).toBe('number');
54+
expect(typeof cv.COLORMAP_TWILIGHT_SHIFTED).toBe('number');
55+
expect(typeof cv.COLORMAP_TURBO).toBe('number');
56+
expect(typeof cv.COLORMAP_DEEPGREEN).toBe('number');
57+
});
58+
59+
it("should apply different colormaps correctly", async () => {
60+
// Create a simple grayscale image
61+
const src = new cv.Mat(50, 50, cv.CV_8UC1, new cv.Scalar(128));
62+
const dst1 = new cv.Mat();
63+
const dst2 = new cv.Mat();
64+
65+
// Apply different colormaps
66+
cv.applyColorMap(src, dst1, cv.COLORMAP_JET);
67+
cv.applyColorMap(src, dst2, cv.COLORMAP_VIRIDIS);
68+
69+
// Both should be 3-channel color images
70+
expect(dst1.channels()).toBe(3);
71+
expect(dst2.channels()).toBe(3);
72+
73+
// Different colormaps should produce different results
74+
const data1 = dst1.data;
75+
const data2 = dst2.data;
76+
let different = false;
77+
for (let i = 0; i < Math.min(data1.length, data2.length); i++) {
78+
if (data1[i] !== data2[i]) {
79+
different = true;
80+
break;
81+
}
82+
}
83+
expect(different).toBe(true);
84+
85+
// Clean up
86+
src.delete();
87+
dst1.delete();
88+
dst2.delete();
89+
});
90+
});

0 commit comments

Comments
 (0)