c++opencvopencv-mat

C++ openCV mat initialization


I am completely new to OpenCV and I'm trying to initialize two cv::Mat matrices from two different type of data. One is simple one-channel array of uint16_t gray values, second should hold RGB values from array of uint8_t values holding RGB 565 (each pixel occupy 2 uint8_t, so it's basically array of uint16_t). I'd like to have one single-chanell matrix, and one 3-chanell matrix (which will probably be converted to one chanelly sometime... but I'm not quite there yet.

function(uint16_t *oneChanell, int oneChanellWidth, int oneChanellHeight, uint8_t *rgb, int rgbWidth, int rgbHeight){
...
    cv::Mat M1 = cv::Mat(oneChanellHeight, oneChanellWidth, CV_16UC1, oneChanell);
    cv::Mat M2 = cv::Mat(rgbHeight, rgbWidth, CV_16UC3, rgb);
...
}

Now, I am aware that the second initialization is wrong. So that is one part of my question, how to best convert array of rgb565values to cv::Mat. In my understanding however, the first initialization should work. When tested with cv::imwrite(), the first yields just blank white picture of correct size, the second three partially overlapping silhouettes of the same (correct) picture, but not in RGB. Any advice would be appreciated.


Solution

  • use cv::cvtColor to convert the mat M2 from RGB565 to RGB format.

    ...
    
    cv::Mat M2 = cv::Mat(rgbHeight, rgbWidth, CV_16UC1, rgb);
    // M2.depth()    = 2 # CV_16U
    // M2.channels() = 1 # single channel with each element having 16 bit
    
    
    
    //convert mat format from bgr565 to rgb
    cv::Mat M2_rgb;
    cv::cvtColor(M2, M2_rgb, COLOR_BGR5652RGB);
    
    ...