【发布时间】:2018-08-13 16:20:36
【问题描述】:
我在使用 OpenCV 3.4.1 和 Cuda 进行 alpha 合成时遇到了奇怪的行为。
当我将带有全零 Alpha 通道的全黑图像混合到另一张图像中,并递归地重复此过程时,图像变得更暗,亮度稳定在 50% 左右。
这是某种错误,还是我误解了有关 Alpha 合成的基本原理?关于不同混合模式的文档很少。
我编写了一个简短的示例代码来说明会发生什么。运行一次时,矩形似乎保持绿色。但是,将其放入循环中会使矩形慢慢淡出,直到变成深绿色。
程序启动时的初始结果
几秒钟后的结果
#include <stdio.h>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudaarithm.hpp>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat frame(1080, 1920, CV_8UC4, Scalar(0, 0, 0, 255));
namedWindow("test", CV_WINDOW_OPENGL);
// Drawing a green rectangle with 100% opacity
rectangle(frame, Rect(10, 10, 200, 200), Scalar(0, 255, 0, 255), CV_FILLED);
cuda::GpuMat gpuFrame(frame);
while(true){
// Creating an all-black image with 0% opacity
cuda::GpuMat gpuBlank(1080, 1920, CV_8UC4, Scalar(0, 0, 0, 0));
// Alpha-compositing them together should keep the image untouched..?
cuda::alphaComp(gpuFrame, gpuBlank, gpuFrame, cuda::ALPHA_OVER);
// Splitting the image into 4 mats, to remove any resulting opacity from the image
// Hopefully coming out with another 100% opacity image to blend again
vector<cuda::GpuMat> frameChannels;
cuda::split(gpuFrame, frameChannels);
cuda::GpuMat fullAlpha(1080, 1920, CV_8UC1, Scalar(255));
if (gpuFrame.channels() > 3) {
frameChannels.pop_back();
}
frameChannels.push_back(fullAlpha);
cuda::merge(frameChannels, gpuFrame);
imshow("test", gpuFrame);
waitKey(1);
}
}
【问题讨论】:
标签: c++ opencv alphablending