Ask Your Question

how to make a new instance of Mat with TypedArray data?

asked 2018-11-19 00:45:04 -0600

swon gravatar image

updated 2018-11-19 21:59:51 -0600


I'm using goodFeaturesToTrack function in web worker for balancing workload.

after finishing goodFeaturesToTrack function, worker posts corners data.

I tried making new Mat with cv.matFromArray and its data(Float32Array), then it showed error like below:

RangeError: Source is too large “too large”

Is there any way to make a new Mat with TypedArray from worker? ( I found cv.matFromImageData(), but it use the cv.CV_8UC4 type. ) I want to make a new mat for detected corners and send it as argument for optical flow function

const runGoodFeaturesToTrack = async (imgData) => {
try {
   // get imgData from main thread
   const dst = await new cv.matFromArray(480, 640, cv.CV_8UC1, imgData)
   const p0 = await new cv.Mat()
   const none = await new cv.Mat()
   const [ blockSize, maxCorners, minDistance, qualityLevel ] = [ 7, 10, 7.01, 0.01 ]

   await cv.goodFeaturesToTrack(dst, p0, maxCorners, qualityLevel, minDistance, none,
   blockSize, false, 0.01);
   // p0.data32F : for point coordinates
   return self.postMessage({ result : p0.data32F, cols : p0.cols, rows : p0.rows }, [ p0.data32F.buffer ])
 }  catch (error) { return console.log('runGoodFeaturesToTrack error', error)}

// PlayingScreenCam.js( main thread )
this.dst = new;

 worker.addEventListener('message', ev => {
 if( {
    const { result, cols, rows } =;
    const p0 =, cols,, result.data32F);


somehow i can make Mat with Float32Array from worker but value is different like below. can i get a same data32F value in main.js?

image description

edit retag flag offensive close merge delete


const { cols, result, rows } -- that's in a different order, than you sent it, right ?

berak gravatar imageberak ( 2018-11-19 04:13:53 -0600 )edit

Thank you for replying berak, I've changed the order const { result, cols, row }. but it shows same result. Is it wrong idea to get the data of Mat in worker (Float32Array) and make a same mat in PlayingScreenCam.js ?

swon gravatar imageswon ( 2018-11-19 22:07:40 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2018-11-19 22:34:32 -0600

swon gravatar image

updated 2018-11-19 22:35:11 -0600

I think i solved the problem. the mat which has Float32Array in the worker has 2 channel. so modified like below :, cols,, result.data32F);

thank you again for spending time for my question!

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-11-19 00:45:04 -0600

Seen: 843 times

Last updated: Nov 19 '18