java - OpenCV java alpha 混合

标签 java c++ opencv alphablending blending

我正在尝试移植以下代码(从 C++ 到 Java)以在图像之间进行良好的 alpha 混合,但它不起作用:

#include opencv2/opencv.hpp

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    // Read the images
    Mat foreground = imread("puppets.png");
    Mat background = imread("ocean.png");
    Mat alpha = imread("puppets_alpha.png");

    // Convert Mat to float data type
    foreground.convertTo(foreground, CV_32FC3);
    background.convertTo(background, CV_32FC3);

    // Normalize the alpha mask to keep intensity between 0 and 1
    alpha.convertTo(alpha, CV_32FC3, 1.0/255); // 

    // Storage for output image
    Mat ouImage = Mat::zeros(foreground.size(), foreground.type());

    // Multiply the foreground with the alpha matte
    multiply(alpha, foreground, foreground); 

    // Multiply the background with ( 1 - alpha )
    multiply(Scalar::all(1.0)-alpha, background, background); 

    // Add the masked foreground and background.
    add(foreground, background, ouImage); 

    // Display image
    imshow("alpha blended image", ouImage/255);
    waitKey(0);

    return 0;
}

代码可以在这里找到:https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/ 我的 Java 版本:

public static Mat alphaBlend(Mat background, Mat foreground) {
        Vector<Mat> rgba = new Vector<Mat>();
        // split RBGA image for separate channels
        Core.split(background, rgba);
        // get alpha channel
        Mat alpha = rgba.get(3);

        // Convert Mat to float data type
        foreground.convertTo(foreground, CvType.CV_32FC3);
        background.convertTo(background, CvType.CV_32FC3);

        // Normalize the alpha mask to keep intensity between 0 and 1
        alpha.convertTo(alpha, CvType.CV_32FC3, 1.0/255); //
        Imgproc.cvtColor(alpha,alpha, Imgproc.COLOR_GRAY2BGRA,4);

        Mat outImage = Mat.zeros(foreground.size(),foreground.type());

        // Multiply the foreground with the alpha matte
        Core.multiply(alpha, foreground, foreground);

        Mat kernel = new MatOfDouble(1.0);

        Core.subtract(kernel, alpha, alpha);

        // Multiply the background with ( 1 - alpha )
        Core.multiply(alpha, background, background);

        // Add the masked foreground and background.
        Core.add(foreground, background, outImage);

        Core.divide(new MatOfDouble(255), outImage,outImage);
        return outImage;
    }

我认为我的问题是将其移植到java中:

multiply(Scalar::all(1.0)-alpha, background, background); 

如有任何帮助,我们将不胜感激!

谢谢

编辑: 这是一个工作版本,但边框不混合透明像素,导致图像交叉点的边框处出现空白像素/线:

    private static Mat merge(Mat background, Mat foreground) {
        Vector<Mat> rgba = new Vector<Mat>();
        // split RBGA image for separate channels
        Core.split(background, rgba);
        // get alpha channel
        Mat alpha = rgba.get(3);
        // Convert Mat to float data type
        // Normalize the alpha mask to keep intensity between 0 and 1
        alpha.convertTo(alpha, CvType.CV_32FC3, 1.0/255); //
        Mat dst = new Mat(256,256,CvType.CV_8UC4);
        alpha.convertTo(alpha,CvType.CV_8UC1);
        Mat alphaInv = new Mat();
        // invert the mask
        Core.absdiff(new MatOfDouble(1.0), alpha, alphaInv);
        foreground.copyTo(dst, alphaInv);
        // case where foreground is full JPEG in BGR
        if(dst.type() != CvType.CV_8UC4) {
            Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2BGRA, 4);
        }
        background.copyTo(dst, alpha);
        return dst;
    }

最佳答案

我建议使用addWeighted Core 中用于基本 alpha 混合的方法。

看起来您想要一个基于蒙版的可变 alpha 混合,您需要按照 api 指定的顺序排列参数,以实现 addition 等基本内容。/subtraction/multiplication 。对于乘法,这意味着标量应该是第二个参数。对于减法,这意味着标量应该再次成为第二个参数(我发现自己将矩阵乘以负 1,然后添加到标量值)。

关于java - OpenCV java alpha 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441058/

相关文章:

java - 如何在读取文件时检测新行

java - Android Studio - Webview Api 级别 - 29 问题

java - 运行 Tomcat 导致无关项目中的 java.rmi.ServerException

c++ - C printf() 两个字符串在一行上?

python cv2.findContours 在简单的灰度图像中查找轮廓效果不佳

java - 如何解决 StreamCorruptedException : invalid stream header in java?

c++ - C++ Linux 中的命名管道检查缓冲区是否已满

python - 为 python 安装带有额外模块(筛选、冲浪...)的 OpenCV 3.0

python - 使用 Python/OpenCV 从图像中提取固定数量的正方形

c++ - 与 .X 文件的地形碰撞