bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.61k stars 1.59k forks source link

How to add a transparent background image to opencv_ core.addWeighted #1912

Open lironghai opened 2 years ago

lironghai commented 2 years ago

How to add a transparent background image to opencv core.addWeighted; At present, transparent parts in transparent pictures are filled with white;
I want to get the reversed color through mat, but I can't find the corresponding method, similar to Core bitwise
not(coreMask, coreMask);

lironghai commented 2 years ago

my code

    FFmpegFrameGrabber.tryLoad();
    FFmpegFrameRecorder.tryLoad();
    FFmpegLogCallback.set();
    System.setProperty("java.awt.headless", "false");
    String url = "rtsp://admin:ad123456@192.168.10.166:554/cam/realmonitor?channel=1&subtype=0";
    FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(url);
    grabber.setOption("rtsp_transport", "tcp");
    grabber.setOption("timeout", "2000000");
    grabber.setOption("rw_timeout", "2000000");

// grabber.setVideoOption("gpu", "1"); grabber.setFrameRate(1); grabber.setImageWidth(1920); grabber.setImageHeight(1080); String encodeName = "h264_nvenc"; String encodeName2 = "h264_cuvid"; // grabber.setVideoCodecName("vaapi"); // grabber.setVideoCodecName("h264_cuvid"); System.out.println("grabber start"); grabber.start();

    CanvasFrame canvasFrame = new CanvasFrame("opencvtest");
    canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvasFrame.setAlwaysOnTop(false);

// OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat(); Java2DFrameConverter frameConverter = new Java2DFrameConverter(); // OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage(); OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

    Point point = new Point(1000, 500);
    Scalar scalar = new Scalar(255, 0, 0, 1.0);

    Mat logo = opencv_imgcodecs.imread("E:\\other_project\\TestUtil\\src\\main\\resources\\16.png");
    Mat mask = opencv_imgcodecs.imread("E:\\other_project\\TestUtil\\src\\main\\resources\\16.png");
    Mat mask1 = opencv_imgcodecs.imread("E:\\other_project\\TestUtil\\src\\main\\resources\\16.png", 0);
    //org.opencv.core.Mat coreMask = Imgcodecs.imread("E:\\other_project\\TestUtil\\src\\main\\resources\\16.png");;
    opencv_imgproc.threshold(mask1,mask1,0,0,opencv_imgcodecs.IMWRITE_PNG_BILEVEL);
    //Core.bitwise_not(coreMask, coreMask);
   // Mat copyMask = this.bufferedImageToMat(this.matToBufferedImage(coreMask));

    double alpha = 0.5;
    int num = 0 ;
    while (true){

        Mat mat = converter.convertToMat(grabber.grabFrame());
        opencv_imgproc.putText(mat, new BytePointer("test word!",StandardCharsets.US_ASCII), point, opencv_imgproc.CV_FONT_VECTOR0, 1.2, scalar, 1, 20, false);
        Rect rect = new Rect(10, 10, logo .cols(), logo .rows());
        Mat ROI = mat.apply(rect);

        opencv_core.addWeighted(ROI, 0.0, logo , 0.0, 0.0, ROI);
        // logo.copyTo(ROI, mask1);
        canvasFrame.showImage(converter.convert(mat));
        ROI.release();
        ROI.close();
    }
saudet commented 2 years ago

Are you by any chance looking to do alpha blending? Something like http://bytedeco.org/news/2014/12/23/third-release/ ?