我正在使用opencv创建水印应用程序,我无法将图像背景设置为透明。 我使用此代码Scalar colorScalar = new Scalar(255,255,255,0); 任何身体都可以帮助我如何使背景透明。我使用PNG格式图片。
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();
答案 0 :(得分:1)
如果您要使用Alpha通道加载PNG图像并因此使用透明加载图像,则必须使用以下代码:
imread("image.png",-1)
您可以在opencv文档中找到更多信息: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread
答案 1 :(得分:1)
正如您在Maximus提供的文档中看到的那样。你需要创建一个4通道垫:
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;
}
然后添加参数并写入。 (此代码来自文档)