Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在wasm(C+;+;)中将图像帧摄影机传递给函数? 我尝试用C++编写一个C++函数,并将它编译成WASM。 此函数将接收图像并对其进行处理并返回结果。 我的第一次POC成功,用户使用文件输入上传图像,我使用文件阅读器API传递图像数据: const fileReader = new FileReader(); fileReader.onload = (event) => { const uint8Arr = new Uint8Array(event.target.result); passToWasm(event.target.result); }; fileReader.readAsArrayBuffer(file); // I got this `file` from `change` event of the file input. 但是当我实现相机进给并开始将帧传递给Wasm时,我开始在C++端得到异常,这里是JS实现: let imageData = canvasCtx.getImageData(0, 0, videoWidth, videoHeight); var data=imageData.data.buffer; var uint8Arr = new Uint8Array(data); passToWasm(uint8Arr); 此在C++方面抛出异常。< /P>_Javascript_C++_Opencv_Webassembly - Fatal编程技术网

Javascript 如何在wasm(C+;+;)中将图像帧摄影机传递给函数? 我尝试用C++编写一个C++函数,并将它编译成WASM。 此函数将接收图像并对其进行处理并返回结果。 我的第一次POC成功,用户使用文件输入上传图像,我使用文件阅读器API传递图像数据: const fileReader = new FileReader(); fileReader.onload = (event) => { const uint8Arr = new Uint8Array(event.target.result); passToWasm(event.target.result); }; fileReader.readAsArrayBuffer(file); // I got this `file` from `change` event of the file input. 但是当我实现相机进给并开始将帧传递给Wasm时,我开始在C++端得到异常,这里是JS实现: let imageData = canvasCtx.getImageData(0, 0, videoWidth, videoHeight); var data=imageData.data.buffer; var uint8Arr = new Uint8Array(data); passToWasm(uint8Arr); 此在C++方面抛出异常。< /P>

Javascript 如何在wasm(C+;+;)中将图像帧摄影机传递给函数? 我尝试用C++编写一个C++函数,并将它编译成WASM。 此函数将接收图像并对其进行处理并返回结果。 我的第一次POC成功,用户使用文件输入上传图像,我使用文件阅读器API传递图像数据: const fileReader = new FileReader(); fileReader.onload = (event) => { const uint8Arr = new Uint8Array(event.target.result); passToWasm(event.target.result); }; fileReader.readAsArrayBuffer(file); // I got this `file` from `change` event of the file input. 但是当我实现相机进给并开始将帧传递给Wasm时,我开始在C++端得到异常,这里是JS实现: let imageData = canvasCtx.getImageData(0, 0, videoWidth, videoHeight); var data=imageData.data.buffer; var uint8Arr = new Uint8Array(data); passToWasm(uint8Arr); 此在C++方面抛出异常。< /P>,javascript,c++,opencv,webassembly,Javascript,C++,Opencv,Webassembly,现在passToWasm的实现是: function passToWasm(uint8ArrData) { // copying the uint8ArrData to the heap const numBytes = uint8ArrData.length * uint8ArrData.BYTES_PER_ELEMENT; const dataPtr = Module._malloc(numBytes); const dataOnHeap = new Uint8Arr

现在
passToWasm
的实现是:

function passToWasm(uint8ArrData) {
   // copying the uint8ArrData to the heap
   const numBytes = uint8ArrData.length * uint8ArrData.BYTES_PER_ELEMENT;
   const dataPtr = Module._malloc(numBytes);
   const dataOnHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, numBytes);
   dataOnHeap.set(uint8ArrData);

   // calling the Wasm function
   const res = Module._myWasmFunc(dataOnHeap.byteOffset, uint8ArrData.length);
}
虽然C++实现将是这样的:

void EMSCRIPTEN_KEEPALIVE checkImageQuality(uint8_t* buffer, size_t size) {
   // I'm using OpenCV in C++ to process the image data
   // So I read the data of the image
   cv::Mat raw_data = cv::Mat(1, size, CV_8UC1, buffer);

   // Then I convert it
   cv::Mat img_data = cv::imdecode(raw_data, cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION);

   // in one of the following steps I'm using cvtColor function which causes the issue for some reason
}
由于摄像头的实施,我得到的例外情况是:

OpenCV(4.1.0-dev)…/modules/imgproc/src/color.cpp:182:错误:(-215:断言失败)_函数“cvtColor”中的src.empty()


使用
文件
输入和获取数据以传递数据,以及从
画布
获取数据,只要两者都将其转换为
Uint8Array
我找到了解决方案(可能只适合我的情况)。
当您试图从
canvas
获取图像数据时,您可以将其作为4个通道(类似于PNG中的RGBA),并根据您的图像处理代码处理它。
我的代码考虑到图像应该是3个通道(类似于jpeg中的RGB),因此我必须使用以下代码进行转换:

canvasBuffer.toBlob(function (blob) {
  passToWASM(blob);
},'image/jpeg');

使用
FileReader
API?
.jpg
图像文件时上载的文件格式是什么?链接到的是什么?您能在C++中上传<代码> MyWasMuncC< /COD>代码吗?