Ask Your Question

Revision history [back]

how to convert cv::Mat::data into a std::vector ?

Hello

I've been struggling with this problem for a few days. I have a jpeg image stored in a cv::Mat object

it was created thusly:

jpg = cv::Mat(h, w, CV_8UC3, (uint8_t *) image.GetBuffer());

I can write to a file using cv::imwrite so I know the jpg object contains valid data.

I am now attempting to extract/convert/massage the cv::Mat::data into a form that will be compatible with the Node.js Nan::NewBuffer function:

Nan::MaybeLocal<v8::Object> Nan::NewBuffer(char* data, uint32_t size)

My strategy has been to make an intermediate std:vector object but am having no luck. Something along the lines of:

std::vector<unsigned char>  * pImg;

if(jpg.isContinuous())
{
    pImg->assign((uchar*)jpg.datastart, (uchar*)jpg.dataend);
    pImg->assign(jpg.begin<uchar>(),jpg.end<uchar>());
    // and lots of variation on the above
}

then use it to contstruct the NewBuffer:

Local<Object> jpegData =
            NewBuffer((char*)pImg->data(),
            pImg->size(),
            buffer_delete_callback,
            pImg).ToLocalChecked();

Any advice would be greatly appreciated.

In general I can get the code to compile, but it appears to crash silently during the vector->assign operation.

Thanks