How to convert Image RAW10 format to cv::Mat

I’m trying to convert Image RAW10.

void ConvertToMat(unsigned char * buffer, unsigned int width, unsigned int height) {
        cv::Mat rgb;
        cv::Mat raw10Img(image.height, image.width, CV_16SC1, image.buffer);

        cv::cvtColor(raw10Img, rgb, cv::COLOR_BayerRG2RGB);
        cv::imwrite("myimage.png", rgb);
}

I get error:

Streaming ERROR : OpenCV(4.5.1) /home/opencv_build/opencv/modules/imgproc/src/demosaicing.cpp:1691: error: (-215:Assertion failed) depth == CV_8U || depth == CV_16U in function 'demosaicing'

wild guess:

cv::Mat raw10Img(image.height, image.width, CV_16UC1, image.buffer);

(unsigned short)

Ok, thank you. I stopped get error, but in output image it is gray square.

        cv::Mat rgb;
        cv::Mat raw10Img(image.height, image.width, CV_16UC1, image.buffer);

        cv::cvtColor(raw10Img, rgb, cv::COLOR_BayerRG2RGB);
        cv::imwrite("myimage.png", raw10Img);

From web-camera API:

Callback will be used to provide RGB preview image

I’ll take that as “API return raw data in RGB format”, but code bellow don’t work:
cv::Mat raw10Img(image.height, image.width, CV_8UC3, image.buffer);

typo, you wanted:

cv::imwrite("myimage.png", rgb); // not raw10Img

please also note, that opencv uses BGR pixel order, so your saved image will have R and B swapped

Oh, yes, of course, you right, sorry. But after correct I get gray square:
cv::imwrite("myimage.png", rgb);

> please also note, that opencv uses BGR pixel order, so your saved image will have R and B swapped
What is means?

This code:

        cv::Mat rgb;
        cv::Mat raw10Img(image.height, image.width, CV_8UC3, image.buffer);

        cv::cvtColor(raw10Img, rgb, cv::COLOR_BGR2RGB);
        cv::imwrite("myimage.png", rgb);

also don’t work

^^ that’#s all broken.

can you save a buffer as 16bit png, and upload it here, please ?

cv::Mat raw10Img(image.height, image.width, CV_16U, image.buffer);
cv::imwrite("myimage.png", raw10Img);