Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit d69cd6a

Browse files
committed
test(getPatchIntensityCentroid): tests are passing
1 parent d0424f1 commit d69cd6a

File tree

6 files changed

+84
-34
lines changed

6 files changed

+84
-34
lines changed

src/Image.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ export class Image {
333333
const start = (row * this.width + column) * this.channels;
334334
for (let i = 0; i < this.channels; i++) {
335335
this.data[start + i] = value[i];
336-
console.log(this.data[start + i], value[i]);
337336
}
338337
}
339338

@@ -346,7 +345,6 @@ export class Image {
346345
*/
347346
public setVisiblePixel(column: number, row: number, value: number[]): void {
348347
if (column >= 0 && column < this.width && row >= 0 && row < this.height) {
349-
console.log(value);
350348
this.setPixel(column, row, value);
351349
}
352350
}
Loading

src/featureMatching/keypoints/__tests__/getPatchIntensityCentroid.test.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,68 @@ test('3x3 empty image', () => {
88
expect(result).toStrictEqual([{ column: 0, row: 0 }]);
99
});
1010

11-
test.only('patch, default options', () => {
11+
test('3x3 image', () => {
12+
const image = testUtils.createGreyImage([
13+
[0, 0, 0],
14+
[1, 0, 0],
15+
[0, 0, 0],
16+
]);
17+
const result = getPatchIntensityCentroid(image, { radius: 1 });
18+
expect(result).toStrictEqual([{ column: -1, row: 0 }]);
19+
});
20+
21+
test('5x5 image', () => {
22+
const image = testUtils.createGreyImage([
23+
[0, 0, 0, 0, 0],
24+
[0, 0, 0, 0, 0],
25+
[0, 0, 0, 1, 9],
26+
[0, 0, 0, 0, 0],
27+
[0, 0, 0, 0, 0],
28+
]);
29+
const result = getPatchIntensityCentroid(image, { radius: 2 });
30+
expect(result).toStrictEqual([{ column: 1.9, row: 0 }]);
31+
});
32+
33+
test('5x5 image, diagonal line', () => {
34+
const image = testUtils.createGreyImage([
35+
[0, 0, 0, 0, 0],
36+
[0, 1, 0, 0, 0],
37+
[0, 0, 1, 0, 0],
38+
[0, 0, 0, 0, 0],
39+
[0, 0, 0, 0, 0],
40+
]);
41+
const result = getPatchIntensityCentroid(image, { radius: 2 });
42+
expect(result).toStrictEqual([{ column: -0.5, row: -0.5 }]);
43+
});
44+
45+
test('check window is circular', () => {
46+
const image = testUtils.createGreyImage([
47+
[0, 0, 0, 0, 0],
48+
[0, 0, 0, 1, 0],
49+
[0, 0, 0, 1, 0],
50+
[0, 0, 0, 1, 0],
51+
[0, 0, 0, 0, 100],
52+
]);
53+
const result = getPatchIntensityCentroid(image, { radius: 2 });
54+
expect(result).toStrictEqual([{ column: 1, row: 0 }]);
55+
});
56+
57+
test('patch, default options', () => {
1258
const image = testUtils.load('featureMatching/patch.png');
13-
console.log(image.colorModel);
59+
image.invert({ out: image });
1460

15-
const centroid = round(getPatchIntensityCentroid(image)[0]);
16-
console.log(centroid);
61+
const radius = 3;
62+
63+
const centroid = getPatchIntensityCentroid(image)[0];
1764

1865
const center = image.getCoordinates(ImageCoordinates.CENTER);
1966

20-
const point = sum(center, centroid);
21-
console.log(point);
67+
const point = round(sum(center, centroid));
2268

2369
const result = image.convertColor(ImageColorModel.RGB);
24-
for (let i = 14; i < 15; i++) {
25-
result.drawPoints([{ row: i, column: i }], {
26-
color: [0, 255, 0],
27-
out: result,
28-
});
29-
}
70+
result.drawCircle(center, radius + 1, { color: [255, 0, 0], out: result });
71+
72+
result.drawPoints([point], { color: [0, 255, 0], out: result });
3073

3174
expect(result).toMatchImageSnapshot();
3275
});

src/featureMatching/keypoints/__tests__/getPatchIntensityMoment.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ test('5x5 image, 01, radius = 2', () => {
2424
expect(result).toStrictEqual([-3]);
2525
});
2626

27+
test('5x5 image, 10, radius = 2', () => {
28+
const image = testUtils.createGreyImage([
29+
[0, 0, 1, 0, 42],
30+
[0, 0, 1, 0, 0],
31+
[0, 0, 0, 4, 0],
32+
[0, 0, 0, 0, 0],
33+
[0, 0, 0, 0, 0],
34+
]);
35+
const result = getPatchIntensityMoment(image, 1, 0, { radius: 2 });
36+
expect(result).toStrictEqual([4]);
37+
});
38+
2739
test('too close to border error', () => {
2840
const image = testUtils.createGreyImage([
2941
[0, 0, 1, 0, 42],

src/featureMatching/keypoints/getOrientedFastKeypoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getOrientedFastKeypoints(
5959
let orientedFastKeypoints: OrientedFastKeypoint[] = [];
6060
for (let keypoint of keypoints) {
6161
const centroid = getPatchIntensityCentroid(image, {
62-
origin: keypoint.origin,
62+
center: keypoint.origin,
6363
radius,
6464
})[0];
6565
const angle = toDegrees(getAngle({ column: 0, row: 0 }, centroid));

src/featureMatching/keypoints/getPatchIntensityMoment.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Image, ImageCoordinates } from '../../Image';
22
import { Point } from '../../geometry';
3+
import { getFilledCirclePoints } from '../../utils/geometry/getCirclePoints';
34
import { checkBorderDistance } from '../utils/checkBorderDistance';
45

56
export interface GetPatchIntensityMomentOptions {
@@ -8,7 +9,7 @@ export interface GetPatchIntensityMomentOptions {
89
*
910
* @default The center of the image.
1011
*/
11-
origin?: Point;
12+
center?: Point;
1213
/**
1314
* Radius of the circular window.
1415
*
@@ -27,37 +28,33 @@ export interface GetPatchIntensityMomentOptions {
2728
* @param p - Order along x.
2829
* @param q - Order along y.
2930
* @param options - Get intensity moment options.
30-
* @returns The intensity moment of order pq or the circular window.
31+
* @returns The intensity moment of order pq of the circular window relative to the center.
3132
*/
3233
export function getPatchIntensityMoment(
3334
image: Image,
3435
p: number,
3536
q: number,
3637
options: GetPatchIntensityMomentOptions = {},
3738
): number[] {
38-
const { origin = image.getCoordinates(ImageCoordinates.CENTER), radius = 3 } =
39-
options;
39+
const {
40+
center: origin = image.getCoordinates(ImageCoordinates.CENTER),
41+
radius = 3,
42+
} = options;
4043

4144
if (!checkBorderDistance(image, origin, radius)) {
4245
throw new Error(`desired patch is too close to image border`);
4346
}
44-
4547
let moment = new Array(image.channels).fill(0);
46-
for (let row = origin.row - radius; row < origin.row + radius; row++) {
47-
for (
48-
let column = origin.column - radius;
49-
column < origin.column + radius;
50-
column++
51-
) {
52-
const xDistance = column - origin.column;
53-
const yDistance = row - origin.row;
5448

55-
if (xDistance ** 2 + yDistance ** 2 <= radius ** 2) {
56-
for (let channel = 0; channel < image.channels; channel++) {
57-
const intensity = image.getValue(column, row, channel);
58-
moment[channel] += xDistance ** p * yDistance ** q * intensity;
59-
}
60-
}
49+
let relativeCirclePoints = getFilledCirclePoints(radius);
50+
for (let point of relativeCirclePoints) {
51+
for (let channel = 0; channel < image.channels; channel++) {
52+
const intensity = image.getValue(
53+
point.column + origin.column,
54+
point.row + origin.row,
55+
channel,
56+
);
57+
moment[channel] += point.column ** p * point.row ** q * intensity;
6158
}
6259
}
6360
return moment;

0 commit comments

Comments
 (0)