everybody. I'm trying to place ".png" image over the video capture using java and opencv.
I performed the tutorial from the official opencv java website http://opencv-java-tutorials.readthedocs.io/en/latest/04-opencv-basics.html
In this tutorial everything works fine with png image from the tutorial. But when i try to load my own png image with alpha channel, i get the wrong result. Png image displays with white spaces.
Here is the print screens of video capture with both images:
Here is the code of loading image:
@FXML
protected void loadLogo() {
if(logoCheckBox.isSelected()) {
this.logo = Imgcodecs.imread("resources/Poli.png", Imgcodecs.IMREAD_COLOR);
}
}
Here is the code of video capture and placing png image over it:
Mat frame = new Mat();
// read the current frame
this.capture.read(frame);
Rect roi = new Rect(frame.cols() - logo.cols(), frame.rows() - logo.rows(), logo.cols(), logo.rows());
Mat imageROI = frame.submat(roi);
Core.addWeighted(imageROI, 1.0, logo, 1.0f, 0.0, imageROI);
And here is how output image place inside ImageView:
MatOfByte buffer = new MatOfByte();
Imgcodecs.imencode(".png", frame, buffer);
Image imageToShow = new Image(new ByteArrayInputStream(buffer.toArray()));
Image imageToShow = Utils.mat2Image(frame);
currentFrame.setImage(imageToShow);
I know that parameter Imgcodecs.IMREAD_COLOR load image with 3 channels, without alpha, but when i change this parameter to IMREAD_UNCHANGED i got this error:
OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels)
This is appear becouse the input Mat with video capture contains only 3 channels.
My question is: how can i load and place png image over video capture correctly ?
P.S When i fill the background of png image with black, its displayd correct, but image is still stay transparent.