how do I Change the opacity level of mat?

907 views
Skip to first unread message

Khawar khan

unread,
Nov 7, 2012, 12:48:52 AM11/7/12
to android...@googlegroups.com
Hi, I'm newbee and I want to scale the image and then change the transparency level as well, I've successfully scaled the mat doing this

     Size size = new Size(width, height);

      // Scales image
      Imgproc.resize(cvImage, scaledImage, size);

Now I don't know how to change the tranparency level, I've searched for this but people are doing in opencv c++, while I can't find a way to do the same in opencv4android.

Thanks in advance.

Alexander Smorkalov

unread,
Nov 8, 2012, 2:53:06 AM11/8/12
to android...@googlegroups.com
Hi,

For RGBA mat you can change transparency by multiplying the alpha channel of each mat element on scalar. You can do something like this:

Core.multiply(inputFrame, new Scalar(1,1,1,0.1), mIntermediateMat);

The last element of RGBA point is alpha channel.

Khawar khan

unread,
Nov 14, 2012, 5:04:03 AM11/14/12
to android...@googlegroups.com
Sorry for late reply, Sir, I tried provided method but actually its not changing image's opacity, btw I've replaced inputFrame and mIntermediateMat 
with one mat which actually needs its opacity, am I doing wrong?

Alexander Smorkalov

unread,
Nov 15, 2012, 4:24:12 AM11/15/12
to android...@googlegroups.com
Do you render your image as RGBA8888? If you use other pixel format alpha channel may be truncated.

Khawar khan

unread,
Nov 15, 2012, 4:34:08 AM11/15/12
to android...@googlegroups.com
Well I'm rendering just by file name, not using any color type i.e. Highgui.imread("mnt/sdcard/image.png");, I'll give a try when I get free and will let you know, thanks any way.. I'm glad you're helping me out. :)

Alexander Smorkalov

unread,
Nov 15, 2012, 5:51:17 AM11/15/12
to android...@googlegroups.com
The problem is in the imread/imwrite parameters. By default the functions read and write only RGB data cropping alpha channel. You need to set falgs parameter in the proper value to solve the issue. See docs here.
BTW, jpeg format does not support alpha channel by design. Use png only.
Reply all
Reply to author
Forward
0 new messages