Opencv4nodejs: Wrong typings for imdecode and imencode

Created on 29 Mar 2018  路  8Comments  路  Source: justadudewhohacks/opencv4nodejs

I think the typings for the imdecode and imencode functions are incorrect or I am not sure how to do the example with the base64 decoding as a cv.Mat correct in typescript.

In your example in opencv-express (https://github.com/justadudewhohacks/opencv-express/blob/master/src/services/imgcodecs.js) you did following:

const buffer = Buffer.from(base64Data, 'base64');
const img = cv.imdecode(buffer);

But when I do that in Typescript I am getting compile error.
You have following typings for the functions:

export function imdecode(buffer: number[], flags?: number): Mat;
export function imdecodeAsync(buffer: number[], flags?: number): Promise<Mat>;
export function imencode(fileExt: string, img: Mat, flags?: number[]): number[];
export function imencodeAsync(fileExt: string, img: Mat, flags?: number[]): Promise<number[]>;

So the compiler gives the error that the type Buffer is not of type number[].

I also tried to use cv.imdecode([...buffer]) and cv.imdecode(Array.from(buffer)), that would give a number[] but than I am getting a runtime error:
Error: Io::Imencode - expected arg 0 to be a Buffer of Uint8 Values

Would be happy regarding any help.

(If you want I could also create a PR just for the typings for this functions, but I am not sure if the Buffer type would be the correct solution).

All 8 comments

Hi,

You are right, it is a node buffer, thus should be Buffer? probably.

Yeah I tried it out with Buffer, that is working.
Should I create a Pull Request with this small change?

But I think in general it would be good to check all functions that work with buffers / data arrays and either use there the Buffer as type too or to check what you really need on the C++ side as a data type and maybe write an own subset of the Buffer type.

Sure, if you have fixed it a PR would be highly appreciated! Actually it's only imdecode/imencode and mat.getData + the mat constructor that work with buffers, I can fix the Mat stuff later on :)

I can fix the stuff for Mat too, I saw that too but was just not sure if that was all.

Should I just use Buffer as type or create an own type definition?

That would be awesome, since you are already on it.

Actually it's only this constructor:
constructor(data: number[], rows: number, cols: number, type?: number); which should accept a buffer instead of number[].

Same with mat.getData. number[] is actually the wrong type and can be removed.

Ok, I will make a PR and set Buffer as type for the stuff ;-)

I am just no sure how to do it with the Buffer type, in my project it works because I have the @type/node typings in it, so I can just write Buffer as a type.
But in your project this should be at least mentioned as a dependency, maybe as an optionalDependencies?

Fixed with PR https://github.com/justadudewhohacks/opencv4nodejs/pull/242 that was merged in and released with version 4.1.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Paulito-7 picture Paulito-7  路  5Comments

SatoshiKawabata picture SatoshiKawabata  路  5Comments

karlbernard2 picture karlbernard2  路  4Comments

developer239 picture developer239  路  5Comments

YaronHershkovitz picture YaronHershkovitz  路  6Comments