Skip to content

Commit 4f3e6f4

Browse files
committed
(#68) Moved scaling of images into separate file
1 parent aeac4e6 commit 4f3e6f4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as path from "path";
2+
import { ImageProcessor } from "./image-processor.class";
3+
import { ImageReader } from "./image-reader.class";
4+
import { scaleImage } from "./scale-image.function";
5+
6+
describe("scaleImage", () => {
7+
it.each([[0.5], [1.5]])("should scale an image correctly by factor %f", async (scaleFactor) => {
8+
// GIVEN
9+
const imageLoader = new ImageReader();
10+
const pathToinput = path.resolve(__dirname, "./__mocks__/mouse.png");
11+
const inputImage = await imageLoader.load(pathToinput);
12+
const inputMat = await ImageProcessor.fromImageWithoutAlphaChannel(inputImage);
13+
const expectedWidth = Math.floor(inputMat.cols * scaleFactor);
14+
const expectedHeight = Math.floor(inputMat.rows * scaleFactor);
15+
16+
// WHEN
17+
const result = await scaleImage(inputMat, scaleFactor);
18+
19+
// THEN
20+
expect(result.rows).toBe(expectedHeight);
21+
expect(result.cols).toBe(expectedWidth);
22+
});
23+
24+
it.each([[0], [-0.25]])("should keep scale if factor <= 0: Scale %f", async (scaleFactor) => {
25+
// GIVEN
26+
const imageLoader = new ImageReader();
27+
const pathToinput = path.resolve(__dirname, "./__mocks__/mouse.png");
28+
const inputImage = await imageLoader.load(pathToinput);
29+
const inputMat = await ImageProcessor.fromImageWithoutAlphaChannel(inputImage);
30+
const expectedWidth = inputMat.cols;
31+
const expectedHeight = inputMat.rows;
32+
33+
// WHEN
34+
const result = await scaleImage(inputMat, scaleFactor);
35+
36+
// THEN
37+
expect(result.rows).toBe(expectedHeight);
38+
expect(result.cols).toBe(expectedWidth);
39+
});
40+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as cv from "opencv4nodejs-prebuilt";
2+
3+
export const scaleImage = async (image: cv.Mat, scaleFactor: number): Promise<cv.Mat> => {
4+
const minScaleFactor = (scaleFactor <= 0.0) ? 1.0 : scaleFactor;
5+
const scaledRows = Math.floor(image.rows * minScaleFactor);
6+
const scaledCols = Math.floor(image.cols * minScaleFactor);
7+
return image.resizeAsync(scaledRows, scaledCols, 0, 0, cv.INTER_AREA);
8+
};

0 commit comments

Comments
 (0)