Farrakh Javed
Farrakh Javed

Reputation: 153

Alpha Value issue in transparency

I'm creating an application of watermarking using opencv, I'm not able to set background of image as transparent. I'm using this code Scalar colorScalar = new Scalar(255,255,255,0); Can any body help me how to make background transparent. I'm using PNG format image.

targetMat = new Mat(targetSize, scaledImage.type(), colorScalar);
Mat waterSubmat = targetMat.submat((int)offsetY,scaledImage.height(), (int)offsetX,    scaledImage.width());
scaledImage.copyTo(waterSubmat);
center = new org.opencv.core.Point(pivotX, pivotY);
Mat rotImage = Imgproc.getRotationMatrix2D(center, degreevaluechange, 1); 
Mat resultMat = new Mat(2,3, CvType.CV_32FC1);
colorScalar = new Scalar(255,255,255,0);
Imgproc.warpAffine(targetMat, resultMat,   rotImage, targetSize, Imgproc.INTER_AREA, Imgproc.BORDER_CONSTANT,   colorScalar);
scaledImage = resultMat.clone(); 

Upvotes: 0

Views: 978

Answers (2)

Øystein W.
Øystein W.

Reputation: 517

As you see in the documentation provided by Maximus. You need to create a 4 channel Mat:

Mat* targetMat = new Mat(targetSize, CV_8UC4, colorScalar);


vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);

try {
    imwrite("alpha.png", targetMat, compression_params);
}
catch (runtime_error& ex) {
    fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
    return 1;
}

Then add the parameters and write. (This code is from the documentation)

Upvotes: 1

Maximus
Maximus

Reputation: 165

If you want to load your PNG image with the alpha channel and therefore load your image with transparenty, you have to use this code:

imread("image.png",-1)

You can find more informations in the opencv documentation here: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread

Upvotes: 1

Related Questions