【发布时间】:2018-11-30 06:56:15
【问题描述】:
我有带有 alpha 通道的视频,我正在尝试将其放置在另一个视频上,如下所示:
public static void overlayImage(Mat background, Mat foreground, Mat output, Point location) {
background.copyTo(output);
for (int y = (int) Math.max(location.y, 0); y < background.rows(); ++y) {
int fY = (int) (y - location.y);
if (fY >= foreground.rows()) {
break;
}
for (int x = (int) Math.max(location.x, 0); x < background.cols(); ++x) {
int fX = (int) (x - location.x);
if (fX >= foreground.cols()) {
break;
}
double opacity;
double[] finalPixelValue = new double[4];
opacity = foreground.get(fY, fX)[3];
finalPixelValue[0] = background.get(y, x)[0];
finalPixelValue[1] = background.get(y, x)[1];
finalPixelValue[2] = background.get(y, x)[2];
finalPixelValue[3] = background.get(y, x)[3];
for (int c = 0; c < output.channels(); ++c) {
if (opacity > 0) {
double foregroundPx = foreground.get(fY, fX)[c];
double backgroundPx = background.get(y, x)[c];
float fOpacity = (float) (opacity / 255);
finalPixelValue[c] = ((backgroundPx * (1.0 - fOpacity)) + (foregroundPx * fOpacity));
if (c == 3) {
finalPixelValue[c] = foreground.get(fY, fX)[3];
}
}
}
output.put(y, x, finalPixelValue);
}
}
}
当我运行这个函数时,我得到 Nullpointer 异常,因为显然前景 Mat 是从 VideoCapture 中获取的,如下所示:
capture.grab() && capture.retrieve(foregroundMat, -1);
仅检索 rgb 图像并删除 alpha 通道。 视频文件本来就很好,它检索到的 mat 应该是 rgba 格式,但事实并非如此。这个问题的原因可能是什么?
【问题讨论】:
-
您使用哪种文件格式来保留视频中的 Alpha 通道?
webm视频格式? -
你好,我使用
webm和mov格式 -
你能分享你初始化
capture对象的代码吗? -
@ZdaR 这里是代码 sn-p
this.trainerCapture = new VideoCapture(videoFile); -
正如你所建议的,我尝试了不同的视频文件,你可以找到它here,但我再次遇到错误:
java.util.concurrent.ExecutionException: java.lang.ArrayIndexOutOfBoundsException: 3在这一行中opacity = foreground.get(fY, fX)[3];它发生在任何其他视频上,即为什么我认为 VideoCapture 打开视频并错误地抓取帧。
标签: java opencv video-capture rgba