增加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(GetTotalWithDim),
MAT_INSTANCE_METHOD(GetSize),
MAT_INSTANCE_METHOD(GetData),
MAT_INSTANCE_METHOD(Col),
MAT_INSTANCE_METHOD(ColRange),
@ -58,6 +59,7 @@ class CVMat : public ObjectWrap<CVMat> {
MAT_INSTANCE_METHOD(Diag),
MAT_INSTANCE_METHOD(Clone),
MAT_INSTANCE_METHOD(CopyTo),
});
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]);
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)
{
@ -194,7 +201,11 @@ class CVMat : public ObjectWrap<CVMat> {
{
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)
{

View File

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

View File

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

View File

@ -35,8 +35,13 @@ export class Mat {
else return this.#mat.GetTotalWithDim(startDim, endDim ?? 2147483647);
}
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 copyTo(mat: Mat) { return this.#mat.CopyTo(M(mat)), mat; }
public row(y: number) { return FromCV(this.#mat.Row(y)); }
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.imdecode(buffer);
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));
fs.writeFileSync("test_data/base.jpg", cv.imencode(".jpg", res)!);