gpt4 book ai didi

java opencv png图像与视频问题的alpha channel

转载 作者:行者123 更新时间:2023-12-02 16:21:24 25 4
gpt4 key购买 nike

大家。我正在尝试使用 java 和 opencv 在视频捕获上放置“.png”图像。

我从官方 opencv java 网站 http://opencv-java-tutorials.readthedocs.io/en/latest/04-opencv-basics.html 执行了教程

在本教程中,使用教程中的 png 图像一切正常。但是当我尝试使用 alpha channel 加载我自己的 png 图像时,我得到了错误的结果。 Png 图像显示带有空格。

这是带有两个图像的视频捕获的打印屏幕:

correct output

incorrect output

my png image

这是加载图像的代码:

@FXML
protected void loadLogo() {
if(logoCheckBox.isSelected()) {
this.logo = Imgcodecs.imread("resources/Poli.png", Imgcodecs.IMREAD_COLOR);
}
}

这是视频捕获并在其上放置 png 图像的代码:
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);

以下是输出图像在 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);

我知道参数 Imgcodecs.IMREAD_COLOR 加载具有 3 个 channel 的图像,没有 alpha,但是当我将此参数更改为 IMREAD_UNCHANGED 时,出现此错误:

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)



这是因为带有视频捕获的输入 Mat 仅包含 3 个 channel 。

我的问题是:如何正确加载和放置 png 图像在视频捕获上?

P.S 当我用黑色填充 png 图像的背景时,它的显示正确,但图像仍然保持透明。

最佳答案

答案是 RGBA 图像不能放置在 RGB 视频捕获上。仅放置 png 图像的 RBG 组件会产生“错误的结果”。

目前,我发现的最佳解决方案是在 RBGA 模式 COLOR_UNCHANCHED 中加载具有 4 个 channel 的 png 图像,采用第 4 个 alpha channel 并将其用作掩码,以使用“copyTo”而不是“addWeighted”功能显示图像。

但是这种方法不适用于 PNG 图像的半透明部分,如阴影和不透明度低的对象。

这是加载具有单独 RGB 和 alpha channel 的图像的代码:

Mat logo = new Mat();
Mat logo_alpha = new Mat();
Mat logo_with_alpha = Imgcodecs.imread("resources/TREE_1.png", Imgcodecs.IMREAD_UNCHANGED);
Vector<Mat> rgba = new Vector<Mat>();
// split RBGA image for separate channels
Core.split(logo_with_alpha, rgba);
// get alpha channel
logo_alpha = rgba.get(3);
// remove alpha channel to get RGB image
rgba.remove(rgba.size()-1);
// get image with only RGB components
Core.merge(rgba, this.logo);

这是视频捕获并在其上放置 png 图像的代码:
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);
// place logo with alpha on the frame
logo.copyTo(imageROI, logo_alpha);

关于java opencv png图像与视频问题的alpha channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47248053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com