Garmaine Staff asked 1 year ago

Writing a webapp for browser(proof of concept of wasm), want to return the mat captured by the webcam back to the c++ site in order to process the image and display it(ui is written by Qt5).

How could I return the Mat? Solution I found are

  1. iterate pixels by img.ucharPtr, copy the pixels value to string and return it to c++. But iterate pixel by js is slow, not an ideal solution.
  2. Use imencode to encode the Mat to jpg and return it, problem is when I call imencode, it give me error message "cv.imencode is not a function". I download the opencv.js from this link, it is official site.

    function captureFrame()
    {
        console.log("capture frame start");
        console.log("cols = ", global_frame.cols, ", rows = ",  global_frame.rows, ", type = ", global_frame.type(), ", steps = ", global_frame.step[0]);
        global_cap.read(global_frame);  // Read a frame from camera
        console.log("convert from rgba 2 rgb");
        cv.cvtColor(global_frame, global_rgb_frame, cv.COLOR_RGBA2RGB);
    
        console.log("convert to byte64 string");
        var base64_frame = cv.imencode(".jpg", global_rgb_frame).toString('base64');
        var length_bytes = lengthBytesUTF8(base64_frame);
        var string_on_wasm_heap = _malloc(length_bytes);
        stringToUTF8(base64_frame, string_on_wasm_heap, length_bytes);
    
        return string_on_wasm_heap;
    }
    

It would be perfect if I could open the webcam and access the frame directly from c++, but haven't found a way to do that yet.