How to merge multi RGB channel to a new Mat

asked 2016-04-25 01:30:10 -0500

updated 2016-04-25 01:50:04 -0500

I am trying to grab a frame from my camera which provides raw image in Bayer 8bit format, I am converting it to RGB 8bit channel using cvtColor and make red, green, blue Mat using CV_8UC3 as splitting Bayer 8 bit gives me grayscale image, which I don't require. I then loop through every pixel and add RGB info to provide it to unity3d texture2d RGB24. Here is the code:

Bayer2RGB(byte* rawImage, byte encodedImage[])
{
    cv::Mat bayer8BitMat(3288, 4608, CV_8UC1, rawImage);
    cv::Mat rgb8BitMat(3288, 4608, CV_8UC3);

    cv::cvtColor(bayer8BitMat, rgb8BitMat, CV_BayerGR2RGB);

    cv::Mat red = cv::Mat::zeros(rgb8BitMat.rows, rgb8BitMat.cols, CV_8UC3 );
    cv::Mat green = cv::Mat::zeros(rgb8BitMat.rows, rgb8BitMat.cols, CV_8UC3 );
    cv::Mat blue = cv::Mat::zeros(rgb8BitMat.rows, rgb8BitMat.cols, CV_8UC3 );

    cv::Mat channels[] = { red, green, blue };
    int from_to[] = {0,2, 1,4, 2,6 };

    cv::mixChannels( &rgb8BitMat, 1, channels, 3, from_to, 3);

    int i,j;
    uchar* p0;
    uchar* p1;
    uchar* p2;
    std::vector<byte> v;

    for( i = 0; i < rgb8BitMat.rows; ++i)
    {
        p0 = channels[2].ptr<byte>(i);
        p1 = channels[1].ptr<byte>(i);
        p2 = channels[0].ptr<byte>(i);

        for ( j = 0; j < rgb8BitMat.cols; ++j)
        {
            v.push_back(p0[j]);
            v.push_back(p1[j]);
            v.push_back(p2[j]);
        }
    }
        std::copy(v.begin(), v.end(), encodedImage);
}

As you can see I am making encodedImage to resolution*3, this is what TextureFormat.RGB24 of unity3d expects. But the image I am getting in unity has separate RGB channels (Please view attached 2nd image), do I need any interpolation ? or am I mistaking somewhere in conversions ? C:\fakepath\1.png C:\fakepath\2.png

edit retag flag offensive close merge delete