Use OpenCV in C++ WASM

Dear colleagues,

I am trying to port a Linux console application to WASM. It is important to mention that no JS is used. JS is only used to call functions of the WASM file. But before I continue, here is the basic information:

emscripten version 3.1.51 (c0c2ca1314672a25699846b4663701bcb6f69cca)
openCV     version 4.7.0

Compile openCV for WASM:

Change build_js.py:
-------------------

if "EMSCRIPTEN" in os.environ:
   emscripten_dir = os.environ["EMSCRIPTEN"]
else:
   log.warning("EMSCRIPTEN environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher")

->

if "EMSDK" in os.environ:
   emscripten_dir = os.environ["EMSDK"]
else:
   log.warning("EMSDK environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher")

Set Toolchain manual
--------------------
emcmake python3 /home/gundula/Workspace/opencv/platforms/js/build_js.py build_wasm --build_wasm --emscripten_dir="/usr/emsdk/upstream/emscripten" --cmake_option="-DCMAKE_TOOLCHAIN_FILE=/usr/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" --cmake_option="-DCMAKE_CROSSCOMPILING_EMULATOR=/usr/emsdk/node/16.20.0_64bit/bin/node"

This generates:
- libopencv_calib3d.a
- libopencv_core.a
- libopencv_dnn.a
- libopencv_features2d.a
- libopencv_flann.a
- libopencv_imgproc.a
- libopencv_objdetect.a
- libopencv_photo.a
- libopencv_video.a

I write a Makefile with a Variable:

STATLIBS += libopencv_calib3d.a libopencv_core.a libopencv_dnn.a libopencv_features2d.a libopencv_flann.a libopencv_imgproc.a libopencv_objdetect.a libopencv_photo.a libopencv_video.a

and build it with:

$(EXECUTABLE) : $(OBJECTS)
	$(CC) $(OBJECTS) $(STATLIBS) $(LIBS) -o $@ -s EXPORTED_FUNCTIONS=...

Then I get the following Linker error’s:

wasm-ld: error: build/convertPictures.o: undefined symbol: cv::imread(...
wasm-ld: error: build/convertPictures.o: undefined symbol: cv::imwrite(...

There is no doubt that Make works correctly. The libs are also included, because classes like cv::Mat are included. And yes, we need imread and imwrite for the local file system of emscripten. We have verified that we can read and write image data to this file system.

One thing first. I do NOT want to port to JS. I want to call OpenCV functions from the C++ code. I already had the idea that this might not be possible with the WASM code and therefore tried to compile OpenCV the “normal” way and specified the em++ compiler as native compiler in “cmake config”. Configure successful but this causes massive problems with make, because the include chain is lost. See “⚙ D131441 [libc++] Diagnose when header search paths are set up incorrectly

What am I looking for. I need to compile openCV so that I can call all openCV functions from the C++ (WASM) Code. If this is not possible with the existing makes, I need a file list for the different modules to create a makefile myself.

explicit js or not, you still need to use the js generators / toolchain,
and the imagecodecs module is not available there
(too many 3rd party code & good js alternatives in that world, i guess)

basically, you’re restricted to the whitelist here

It’s time to change the Computer Vision Library. Tomorrow I check CImg, boost::GIl and OpenImageIO. Thanks