How can I get RGB three channel color image from yuv

2,199 views
Skip to first unread message

stone_liu

unread,
Oct 19, 2011, 12:51:30 AM10/19/11
to android-opencv
Hello,

I using the following code to get a gray image:
////////////////////////////////////////////////
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);
//jbyte* newyuv = env->

Mat myuv(height + height/2, width, CV_8UC1, (unsigned char
*)_yuv);
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);

cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
Mat grayimg(height,width,CV_8UC1);
cvtColor(mbgra,grayimg,CV_BGR2GRAY);
///////////////////////

Here grayimg is a gray image,and mbgra is a four channels ABGR image.
I want to use RGB or BGR three channels image. how can I get it?

Rouhollah Rahmatizadeh

unread,
Oct 19, 2011, 3:17:28 AM10/19/11
to android...@googlegroups.com
Hi,
You can replace CV_8UC4 with CV_8UC3 and
cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4); with
cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 3);
this should work.
Another way is:
Mat interm;
cvtColor(mbgra, interm, CV_RGBA2RGB, 3);

stone_liu

unread,
Oct 19, 2011, 11:30:38 PM10/19/11
to android-opencv
Thank you very much.
it's nice for me.

Stone_Liu
> > I want to use RGB or BGR three channels image. how can I get it?- Hide quoted text -
>
> - Show quoted text -

stone_liu

unread,
Oct 20, 2011, 2:49:28 AM10/20/11
to android-opencv
HI,Rouhollah,

When I try the two ways provied by you, I found the second way:
Mat interm; cvtColor(mbgra,interm,CV_RGBA2RGB,3);
works well.
The first way :cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 3);
reports error.
I am not clear about why this way will be wrong.
Can you help me again?

By the way, I found the my opencv programs runs very slow just under
such android JNI mode, it is only about 2-3 frams/s. Have you some
comments or ideas on how to improve the performance?

Thanks!
Stone_Liu

On Oct 19, 3:17 pm, Rouhollah Rahmatizadeh <rrahm...@gmail.com> wrote:

Rouhollah Rahmatizadeh

unread,
Oct 20, 2011, 3:29:58 AM10/20/11
to android...@googlegroups.com
Hi,
I am compiling the first solution without any errors. However, I should change back channels to 4 to make an ARGB8888 bitmap. 

Why are you creating two gray images?

with this you are done:

Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);

And what are these lines for?!

  Mat grayimg(height,width,CV_8UC1);
 cvtColor(mbgra,grayimg,CV_
BGR2GRAY);

My app works on galaxy S with 640*480 preview size at 15-20 FPS. What is your device and resolution? And is the code you provided all the processing you are doing on your frame?

Best Regards

stone_liu

unread,
Oct 20, 2011, 4:41:59 AM10/20/11
to android-opencv
Thanks a lot for your comments.
Yes, the compiling of the first solution:
///////////////
Mat mbgra(height, width, CV_8UC3, (unsigned char *)_bgra);
cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 3);
/////////////////
can be passed. And the apk files can be built.
But when I run it in android phone (SAMSUNG galaxy GT-P1000). It will
reports "quits with unexpect errors".

I use the second way to change my code. But the speed is still very
slow.
I list the main code of my app in the following:
///////////////////////////////////////
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);
Mat myuv(height + height/2, width, CV_8UC1, (unsigned char
*)_yuv); /// I do not know why this "height+height/2" will work while
not "height"?
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
Mat trcm;
cvtColor(mbgra, trcm, CV_RGBA2RGB, 3);
IplImage img1=IplImage(trcm);//since my opencv program use 'c'
style, I have to covert the mat data 'trcm' to IplImage type.
int direction=-1;
direction=motion_deter(&img1,10);//here, the motion_deter function
will do some processing to get the diretion of objects motion.
if (direction==0)
///show a circle in the screen
if (direction==1)
///show a cross in the screen
......
///////////////////////////////////
The above code runs very slowly. I guess the reason may be in the
function motion_deter. However, it works well in windows and linux. I
do not konw why it is so slow in such android opencv enviorment.
It works on galaxy GT-P1000.
The preview size may be 640*480 or higher (actually, I do not know
how to see it and adjust it).
And can I set the video data size to a smaller one?( it seems that
cvSetCaptureProperty can not work)

Bests,
Stone_Liu

On Oct 20, 3:29 pm, Rouhollah Rahmatizadeh <rrahm...@gmail.com> wrote:
> Hi,
> > > - Show quoted text -- Hide quoted text -

Rouhollah Rahmatizadeh

unread,
Oct 21, 2011, 2:53:59 AM10/21/11
to android...@googlegroups.com
I can't help you anymore. Hope someone in this group help you.

stone_liu

unread,
Oct 21, 2011, 6:40:29 AM10/21/11
to android-opencv
Rouhollah , thanks a lot!
Anyone else has some ideas on it?
Reply all
Reply to author
Forward
0 new messages