增加Mat数据获取

This commit is contained in:
2025-03-17 11:51:48 +08:00
parent 84f388f294
commit 27a24d0073
6 changed files with 36 additions and 5 deletions

13
.npmignore Normal file
View File

@ -0,0 +1,13 @@
/.git
/.vscode
/build
/cxx
/node_modules
/src
/test_data
/thirdpart
/.clang-format
/.gitignore
/CMakeLists.txt
/package-lock.json
/tsconfig.json

View File

@ -50,6 +50,7 @@ class CVMat : public ObjectWrap<CVMat> {
MAT_INSTANCE_METHOD(GetTotal), MAT_INSTANCE_METHOD(GetTotal),
MAT_INSTANCE_METHOD(GetTotalWithDim), MAT_INSTANCE_METHOD(GetTotalWithDim),
MAT_INSTANCE_METHOD(GetSize), MAT_INSTANCE_METHOD(GetSize),
MAT_INSTANCE_METHOD(GetData),
MAT_INSTANCE_METHOD(Col), MAT_INSTANCE_METHOD(Col),
MAT_INSTANCE_METHOD(ColRange), MAT_INSTANCE_METHOD(ColRange),
@ -58,6 +59,7 @@ class CVMat : public ObjectWrap<CVMat> {
MAT_INSTANCE_METHOD(Diag), MAT_INSTANCE_METHOD(Diag),
MAT_INSTANCE_METHOD(Clone), MAT_INSTANCE_METHOD(Clone),
MAT_INSTANCE_METHOD(CopyTo),
}); });
constructor = new FunctionReference(); constructor = new FunctionReference();
@ -159,7 +161,12 @@ class CVMat : public ObjectWrap<CVMat> {
for (int i = 0; i < mat_.dims; ++i) ret.Set(i, size[i]); for (int i = 0; i < mat_.dims; ++i) ret.Set(i, size[i]);
return ret; return ret;
} }
Napi::Value GetData(const Napi::CallbackInfo &info)
{
auto ptr = mat_.ptr();
auto bytes = mat_.elemSize() * mat_.total();
return ArrayBuffer::New(info.Env(), ptr, bytes);
}
Napi::Value Col(const Napi::CallbackInfo &info) Napi::Value Col(const Napi::CallbackInfo &info)
{ {
@ -194,7 +201,11 @@ class CVMat : public ObjectWrap<CVMat> {
{ {
return CreateMat(info.Env(), [&](CVMat &mat) { mat.mat_ = mat_.clone(); }); return CreateMat(info.Env(), [&](CVMat &mat) { mat.mat_ = mat_.clone(); });
} }
Napi::Value CopyTo(const Napi::CallbackInfo &info) {
auto &target = GetMat(info[0].As<Object>());
mat_.copyTo(target.mat_);
return info.Env().Undefined();
}
static Napi::Value Crop(const Napi::CallbackInfo &info) static Napi::Value Crop(const Napi::CallbackInfo &info)
{ {

View File

@ -1,10 +1,11 @@
{ {
"name": "@yizhi/cv", "name": "@yizhi/cv",
"version": "1.0.1", "version": "1.0.1",
"releaseVersion": "1.0.2",
"main": "dist/index.js", "main": "dist/index.js",
"types": "typing/index.d.ts", "types": "typing/index.d.ts",
"scripts": { "scripts": {
"build": "tsc", "build": "rm -rf dist typing && tsc",
"watch": "tsc -w --inlineSourceMap" "watch": "tsc -w --inlineSourceMap"
}, },
"keywords": [], "keywords": [],

View File

@ -9,7 +9,7 @@ const URLS = {
} }
function getURL(template: string) { function getURL(template: string) {
const version = require("../../package.json").version; const version = require("../../package.json").releaseVersion;
let platform = ""; let platform = "";
let arch = ""; let arch = "";
switch (os.platform()) { switch (os.platform()) {
@ -49,7 +49,7 @@ export async function downloadAddon(savename?: string) {
const defaultAddon = path.resolve(process.cwd(), C("ADDON_PATH")); const defaultAddon = path.resolve(process.cwd(), C("ADDON_PATH"));
const saveName = savename ? path.resolve(path.dirname(defaultAddon), savename) : defaultAddon; const saveName = savename ? path.resolve(path.dirname(defaultAddon), savename) : defaultAddon;
if (fs.existsSync(saveName)) return saveName; if (fs.existsSync(saveName)) return saveName;
await fs.promises.mkdir(path.dirname(saveName), { recursive: true }); await fs.promises.mkdir(path.dirname(saveName), { recursive: true });
const stream = await getStream(); const stream = await getStream();

View File

@ -35,8 +35,13 @@ export class Mat {
else return this.#mat.GetTotalWithDim(startDim, endDim ?? 2147483647); else return this.#mat.GetTotalWithDim(startDim, endDim ?? 2147483647);
} }
public get size(): number[] { return this.#mat.GetSize(); } public get size(): number[] { return this.#mat.GetSize(); }
public get data(): Uint8Array {
if (this.isContinuous) return new Uint8Array(this.#mat.GetData());
else return this.copyTo(this.clone()).data;
}
public clone() { return FromCV(this.#mat.Clone()); } public clone() { return FromCV(this.#mat.Clone()); }
public copyTo(mat: Mat) { return this.#mat.CopyTo(M(mat)), mat; }
public row(y: number) { return FromCV(this.#mat.Row(y)); } public row(y: number) { return FromCV(this.#mat.Row(y)); }
public col(x: number) { return FromCV(this.#mat.Col(x)); } public col(x: number) { return FromCV(this.#mat.Col(x)); }

View File

@ -8,6 +8,7 @@ async function test() {
const res = await cv.imread("test_data/im1.jpeg"); const res = await cv.imread("test_data/im1.jpeg");
// const res = await cv.imdecode(buffer); // const res = await cv.imdecode(buffer);
const cropIm = cv.crop(res, { x: 10, y: 10, width: 300, height: 200 }); const cropIm = cv.crop(res, { x: 10, y: 10, width: 300, height: 200 });
console.log(cropIm.data)
console.log(cv.imwrite("test_data/cropIm.jpg", cropIm)); console.log(cv.imwrite("test_data/cropIm.jpg", cropIm));
fs.writeFileSync("test_data/base.jpg", cv.imencode(".jpg", res)!); fs.writeFileSync("test_data/base.jpg", cv.imencode(".jpg", res)!);