justadudewhohacks/opencv4nodejs

Return Different Buffer

Closed this issue · 2 comments

when I convert the image into the buffer in two ways return totally different buffer from each way.
why this happens.

const fs = require('fs');
const path = require('path');
const dataDir = 'Data';

const centerImageBuffer = fs.promises.readFile(
    path.join(dataDir, '1.jpg')

centerImageBuffer.then(function(result) {
    console.log('fs', result)
});

);

this way I got output like this

fs <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 
07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... 14157 more bytes>

when using openv4nodejs

const cv = require('opencv4nodejs');
const path = require('path');
const dataDir = 'Data';

const img = cv.imread(path.join(dataDir, '1.jpg'));
const str = cv.imencode('.jpg', img).toString("base64");
console.log("cv", Buffer.from(str, "base64"));

I got output like this

cv <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 02 01 01 01 01 01 02 
01 01 01 02 02 02 02 02 04 03 02 02 02 02 05 04 04 03 ... 26489 more bytes>

each bytes lengths are not equal.

In my code 1st method working properly (Both are working, But the expected outcome more closer when using the 1st method)
But I want to implement it using opwncv4nodejs.

Can you guys give me a solution for that..

thank you.

Hi, I am not sure with your first method, but I am using the second method exactly: cv.imencode('.jpg', img).toString("base64"); and then getting Mat from buffer const newMat = cv.imdecode(buffer);. It works on my side without making any buffer as the final result. Besides, opencv has a better performance when reading and writing image files based on my experience.
Hope it will be helpful.

yea thanks for your comment.