Ask Your Question
0

How can I convert an OpenCV Mat of type CV_8UC3 pixel data to an unsigned int* in ARGB format?

asked 2013-10-17 08:57:57 -0600

prerna1 gravatar image

updated 2013-10-17 11:57:34 -0600

berak gravatar image

I need a pointer unsigned int* data, such that,

an unsigned int (32 bits) holds a pixel in ARGB format, which from left to right is as follows:

the first 8 bits are for the alpha channel (and are ignored) the next 8 bits are for the red channel the next 8 bits are for the green channel the last 8 bits are for the blue channl

edit retag flag offensive close merge delete

Comments

could you please remove the other question ? one should be enough.

berak gravatar imageberak ( 2013-10-17 12:03:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-10-17 11:56:19 -0600

berak gravatar image

unfortunately there's no builtin support for ARGB with opencv , so there's no easy, straightforward solution.

you could try a double-for loop like this:

unsigned int * pixels = new unsigned int [ 4 * img.total() ];
int k=0;
for ( int i=0; i<img.rows; i++ ) {
    for ( int j=0; j<img.rows; j++ ) {
        Vec3b p = img.at<Vec3b>(i,j);
                                 // a ( skipped )
        pixels[k]  = p[2] << 16; // r
        pixels[k] |= p[1] << 8;  // g
        pixels[k] |= p[0];       // b
        k++;
    }
}
// now process pixels

// ofc, later, you'll have to cleanup
delete [] pixels;
edit flag offensive delete link more

Comments

Later, if I want to retrieve these rgb values, how can I do that?

prerna1 gravatar imageprerna1 ( 2013-10-17 14:00:21 -0600 )edit
2

from the unsigned int * pointer ?

unsigned int p = pixels[i];

uchar r =(p>>16) & 0xff;

uchar g =(p>>8) & 0xff;

uchar b =(p) & 0xff;

it's just the reverse..

berak gravatar imageberak ( 2013-10-17 14:17:08 -0600 )edit
2

it should basically work like that:

int r = (pixel & 0x00FF0000) >> 16;

int g = (pixel & 0x0000FF00) >> 8;

int b = (pixel & 0x000000FF);

int a = (pixel & 0xFF000000) >> 24;

Those numbers are basically bitmasks extracting only the part that is responsible for the channel.

Btw, you can also add the alpha channel (imread also supports this). You just need Vec4b p = img.at<Vec4b> and then pixels[k] = p[3] << 24;

Moster gravatar imageMoster ( 2013-10-17 14:20:35 -0600 )edit

lol, Moster, i got : first shift, then mask, you got the opposite. is it actually the same ? i think so.

actually i think, your version is more clear on what ends up where

berak gravatar imageberak ( 2013-10-17 14:25:10 -0600 )edit

Yes, its doing the same. It should also have exactly the same performance.

Moster gravatar imageMoster ( 2013-10-17 14:27:22 -0600 )edit

Question Tools

Stats

Asked: 2013-10-17 08:57:57 -0600

Seen: 3,306 times

Last updated: Oct 17 '13