Ask Your Question
0

How to get RGBA8888 image data from 8UC4 Matrix?

asked 2013-03-08 00:41:41 -0600

asloob gravatar image

updated 2013-03-08 05:48:30 -0600

Hello, I am new to OpenCV. I need to get raw RGBA8888 image data from a Matrix in C++. The matrix is of type CvType.CV_8UC4 . Is this possible? Can anyone point me in the right direction? Or even better some sample code?

UPDATE I have created the following methods to da that with help from Mathiew. Is this correct?

void HelloWorld::convertBRGAtoRGBA(Mat mat, Mat dst)
{
    dst(mat); //create a copy
    for( int rowIndex = 0 ; rowIndex < mat.rows ; ++rowIndex )
    {
        for( int colIndex = 0 ; colIndex < mat.cols ; ++colIndex )
        {
            cv::Vec4b val = mat.at<cv::Vec4b>(rowIndex,colIndex);
        char blue = val[0];
        char green = val[1];
        char red = val[2];
        char alpha = val[3];
       //do anything you want here...
        dst.at<Vec4b>(rowIndex, colIndex)[0] = red;
        dst.at<Vec4b>(rowIndex, colIndex)[1] = green;
        dst.at<Vec4b>(rowIndex, colIndex)[2] = blue;
        dst.at<Vec4b>(rowIndex, colIndex)[3] = alpha;
    }
}
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-08 02:10:37 -0600

You could create you image in RGBA8888 with the accessors of a matrix:

Mat mat = imread("myimage.png");
for( int r = 0 ; r < mat.rows ; ++r )
{
    for( int c = 0 ; c < mat.cols ; ++c )
    {
        cv::Vec4b val = mat.at<cv::Vec4b>(r,c);
        char blue = val[0];
        char green = val[1];
        char red = val[2];
        char alpha = val[3];
       //do anything you want here...
    }
}

Image in OpenCV are in BGRA, not in RGBA! Maybe you could directly used the data pointer of cv::Mat to create your image, but I probably depend on the internal organization of RGBA8888 image you want to use.

edit flag offensive delete link more

Comments

Thanks mathieu. I have tried to create a functiont to do this. Its still not working, can you have a look?

asloob gravatar imageasloob ( 2013-03-08 05:49:26 -0600 )edit

Consider using mixChannels in your case http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=mixchannels#void%20mixChannels%28const%20Mat%20src,%20size_t%20nsrcs,%20Mat%20dst,%20size_t%20ndsts,%20const%20int*%20fromTo,%20size_t%20npairs%29

But I think you have a reference missing in your code for the dst matrix:

void HelloWorld::convertBRGAtoRGBA(Mat mat, Mat & dst)

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-03-08 09:35:40 -0600 )edit

Question Tools

Stats

Asked: 2013-03-08 00:41:41 -0600

Seen: 5,768 times

Last updated: Mar 08 '13