Hello Stack Overflow community.
I am attempting to extract the pixel values from a cropped section of an opencv matrix to a javascript array in order to feed that data into a tensor for OCR. I cannot use cv.imshow()
because it interacts with the DOM and my process is on a web worker.
This is what I have so far to convert from Opencv to js array:
JavaScript
x
22
22
1
let src = cv.imread('canvasInput');
2
let dst = new cv.Mat();
3
let rect = new cv.Rect(100, 100, 300, 300);
4
dst = src.roi(rect);
5
6
// equivalent(ish) to cv.imshow();
7
let canvas = document.getElementById('canvasOutput');
8
let context = canvas.getContext('2d');
9
10
let imageData = context.createImageData(dst.cols, dst.rows);
11
imageData.data.set(new Uint8ClampedArray(dst.data, dst.cols, dst.rows));
12
13
canvas.height = dst.rows;
14
canvas.width = dst.cols;
15
16
context.putImageData(imageData, 0, 0);
17
// end cv.imshow()
18
19
src.delete();
20
dst.delete();
21
22
If you paste this code into Opencv IDE and click try it the output seems to be offsetting weirdly. What am I doing wrong here?
Thanks in advance for the help!!
Advertisement
Answer
I believe this should help you:
JavaScript
1
9
1
let mat = new cv.Mat();
2
// Initialise a MatVector
3
let matVec = new cv.MatVector();
4
// Push a Mat back into MatVector
5
matVec.push_back(mat);
6
// Get a Mat fom MatVector
7
let cnt = matVec.get(0);
8
mat.delete(); matVec.delete(); cnt.delete();
9
Refer the opencv.js documentation link for more details.