Ask Your Question
1

Image Background Transparency - OpenCV

asked 2014-05-21 00:06:40 -0600

Farrakh Javed gravatar image

updated 2020-11-17 17:31:00 -0600

Hi, I'm using this code to make image background transparent, but i'm not getting the background transparent.

Imgproc.warpAffine(targetMat, resultMat, rotImage, targetSize, Imgproc.INTER_CUBIC,        
Imgproc.BORDER_TRANSPARENT,new Scalar(255,255,255,0));

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-05-21 00:45:26 -0600

warpAffine sets the destination image to have the same type as the source. You need targetMat to be CV_8UC4

edit flag offensive delete link more

Comments

Thanks for the reply, Can you tell me how to pass this parameter to target mat?

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-21 00:49:29 -0600 )edit

In C++ this would be done like this when you declare targetMat:

cv::Mat targetMat(rows, cols, CV_8UC4);
Martin Peris gravatar imageMartin Peris ( 2014-05-21 01:30:38 -0600 )edit

targetSize = new Size(newWidthHeight[0], newWidthHeight[1]);

I'm doing this, but not getting transparency. targetSize = new Size(newWidthHeight[0], newWidthHeight[1]); Scalar colorScalar = new Scalar(125,125,200,0.6); targetMat = new Mat(targetSize, scaledImage.type(),colorScalar);

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-21 01:59:05 -0600 )edit

If i omit the colorScalar from Mat function, default black background is applied. I can see different colors in background if i change Scalar values.

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-21 02:01:07 -0600 )edit

You are setting targetMat with the same type as scaledImage which is probably not CV_8UC4. Try:

targetMat = new Mat(targetSize, CV_8UC4, colorScalar);
Martin Peris gravatar imageMartin Peris ( 2014-05-21 02:10:19 -0600 )edit

After making change as you suggested, i lost my watermark image(above rotated image).

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-21 02:21:13 -0600 )edit

For CV_8UC3 it is working. I think i am missing alpha value, but don't know how to solve this.

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-21 02:29:43 -0600 )edit

You are losing the image because you are defining colorScalar as:

Scalar colorScalar = new Scalar(125,125,200,0.6)

The alpha channel on a CV_8UC4 image is represented by a uchar ([0-255]) and you are giving it a value of 0.6 which will truncate to 0 (which means fully transparent, and hence making the whole image fully trasparent)

Try this instead:

Scalar colorScalar = new Scalar(125, 125,200, 154);

This should make your watermark appear about 60% transparent

Martin Peris gravatar imageMartin Peris ( 2014-05-21 04:02:03 -0600 )edit

Question Tools

Stats

Asked: 2014-05-21 00:06:40 -0600

Seen: 7,448 times

Last updated: May 21 '14