Image
Image
image.Image
Bitmap wrapper backed by OpenCV Mat. Instances are produced from readImage, decodeImage, loadImage, Image.ofBitmap, or by wrapping an existing cv.Mat.
Call recycle() when you are finished to release native memory promptly.
Table of contents
Constructors
Accessors
Methods
- clip
- clipSync
- copy
- copySync
- detectAndComputeFeatures
- grayscale
- grayscaleSync
- pixel
- recycle
- rotate
- rotateSync
- scale
- scaleSync
- resize
- resizeSync
- toBitmap
- ofBitmap
Constructors
constructor
• new Image(mat)
Wrap an existing OpenCV matrix.
Parameters
| Name | Type | Description |
|---|---|---|
mat | cv.Mat | Source image. |
Accessors
mat
• get mat(): cv.Mat
Underlying cv.Mat in the image’s native channel layout (often BGR or BGRA).
Returns
cv.Mat
c4mat
• get c4mat(): cv.Mat
4-channel BGRA view. Converts automatically when the source has fewer channels.
Returns
cv.Mat
width
• get width(): number
Image width in pixels.
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
console.log("width:", img.width);
}
main();Returns
number
height
• get height(): number
Image height in pixels.
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
console.log("height:", img.height);
}
main();Returns
number
Methods
pixel
▸ pixel(x, y): Color
Sample (x, y) as a Color. Interpretation:
- 3 channels — assumed BGR
- 4 channels — assumed BGRA
- 1 channel — grayscale
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const color = img.pixel(0, 0);
console.log("top-left:", color);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
x | number | Column index, 0 = left. |
y | number | Row index, 0 = top. |
Returns
clip
▸ clip(rect): Promise<Image>
Async ROI crop.
Example
"nodejs";
const cv = require("@autojs/opencv");
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const clipped = await img.clip(new cv.Rect(0, 0, 100, 100));
console.log("clipped:", clipped);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
rect | cv.Rect | x, y, width, height. |
Returns
Promise<Image>
clipSync
▸ clipSync(rect): Image
Synchronous clip.
Example
"nodejs";
const cv = require("@autojs/opencv");
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const clipped = img.clipSync(new cv.Rect(0, 0, 100, 100));
console.log("clipped:", clipped);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
rect | cv.Rect | ROI. |
Returns
resize
▸ resize(width, height, interpolation?): Promise<Image>
Resize to an explicit width × height.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
width | number | undefined | Target width (px). |
height | number | undefined | Target height (px). |
interpolation | number | cv.INTER_LINEAR | OpenCV interpolation flag (INTER_AREA, INTER_CUBIC, INTER_LANCZOS4, INTER_LINEAR, INTER_NEAREST, …). |
Returns
Promise<Image>
resizeSync
▸ resizeSync(width, height, interpolation?): Image
Synchronous resize.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
width | number | undefined | Target width. |
height | number | undefined | Target height. |
interpolation | number | cv.INTER_LINEAR | Interpolation flag. |
Returns
scale
▸ scale(fx, fy?, interpolation?): Promise<Image>
Uniform or independent scale factors relative to the current size (not absolute pixels).
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const scaled = await img.scale(0.5, 0.5);
console.log("scaled:", scaled);
}
main();Parameters
| Name | Type | Default | Description |
|---|---|---|---|
fx | number | undefined | Horizontal scale (1 = original, 0.5 = half width, 2 = double). |
fy | number | fx | Vertical scale; defaults to fx. |
interpolation | number | cv.INTER_LINEAR | Interpolation flag (same set as resize). |
Returns
Promise<Image>
scaleSync
▸ scaleSync(fx, fy?, interpolation?): Image
Synchronous scale.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
fx | number | undefined | Horizontal factor. |
fy | number | fx | Vertical factor. |
interpolation | number | cv.INTER_LINEAR | Interpolation flag. |
Returns
rotate
▸ rotate(degree, center?): Promise<Image>
Rotate by degree around center (defaults to image center). Positive angles follow the implementation’s OpenCV convention (see upstream docs for your build).
Example
"nodejs";
const cv = require("@autojs/opencv");
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const rotated = await img.rotate(90);
console.log("rotated:", rotated);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
degree | number | Angle in degrees. |
center | cv.Point2 | Optional pivot; image center if omitted. |
Returns
Promise<Image>
rotateSync
▸ rotateSync(degree, center?): Image
Synchronous rotate.
Parameters
| Name | Type | Description |
|---|---|---|
degree | number | Degrees. |
center | cv.Point2 | Pivot. |
Returns
grayscale
▸ grayscale(): Promise<Image>
Async single-channel conversion.
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const gray = await img.grayscale();
console.log("grayscale:", gray);
}
main();Returns
Promise<Image>
grayscaleSync
▸ grayscaleSync(): Image
Synchronous grayscale.
Returns
copy
▸ copy(): Promise<Image>
Deep copy into a new Image.
Example
"nodejs";
const { loadImage } = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
const copied = await img.copy();
console.log("copy:", copied);
}
main();Returns
Promise<Image>
copySync
▸ copySync(): Image
Synchronous copy.
Returns
toBitmap
▸ toBitmap(): android.graphics.Bitmap
Export to an Android Bitmap.
See also
Returns
android.graphics.Bitmap
detectAndComputeFeatures
▸ detectAndComputeFeatures(options?): Promise<ImageFeatures>
Run feature detection + descriptor extraction for template / feature matching workflows.
Example
"nodejs";
const { readImage, FeatureDetectMethod } = require("image");
async function main() {
const img = await readImage("./image.png");
const features = await img.detectAndComputeFeatures({
method: FeatureDetectMethod.SIFT,
scale: 0.5,
});
features.recycle();
}
main();Parameters
| Name | Type | Description |
|---|---|---|
options | DetectAndComputeFeaturesOptions | Detector tuning (method, scale, ROI, …). |
Returns
Promise<ImageFeatures>
recycle
▸ recycle(): void
Release native buffers. Do not touch the wrapper afterwards.
Important
Image instances can be large—recycle() eagerly to avoid OOM pressure.
Example
"nodejs";
const { readImage } = require("image");
async function main() {
const img = await readImage("./image.png");
const gray = await img.grayscale();
img.recycle();
gray.recycle();
}
main();Returns
void
ofBitmap
▸ Static ofBitmap(bitmap): Image
Wrap an android.graphics.Bitmap.
Example
"nodejs";
const { Image } = require("image");
const bitmap = /* ... */;
const img = Image.ofBitmap(bitmap);
console.log("from bitmap:", img);Parameters
| Name | Type | Description |
|---|---|---|
bitmap | android.graphics.Bitmap | Valid bitmap. |
Returns
Throws
If bitmap is null or not a bitmap instance.
