OpenCV函数合并Mat的黑色/透明像素除外

Mer*_*erl 1 c++ opencv

我有一个Mat包含黑色背景的文本和一个包含背景的Mat。我想合并2个Mat,使文本位于背景上方。两个Mat均为BGR(CV_8UC3),并且大小相同(行和行)。

OpenCV如何做到这一点?

我知道一对夫妇的功能,但|=+=正在改变文字颜色和addWeighted()唯一的,如果我想一个影像有点透明的工作。我只想对两者进行完全合并(文本图像中的黑色像素除外)。我正在寻找一个OpenCV函数,而不是自己编辑像素,LUT如果那是一个选择,那会很好。

在此处输入图片说明

Mic*_*cka 5

尝试这个:

sourceText = yourTextImage;
sourceBackground = yourBackgroundImage;

cv::Mat textTransparent;
cv::inRange(sourceText, cv::Scalar(0,0,0), cv::Scalar(0,0,0), textTransparent);
cv::imshow("transparent pixel", textTransparent); // this should show all the transparent pixel as being white. If that's not the case, adjust the range.
std::cout << "is the transparency mask ok? Press any key with openCV window focus." << std::endl;

cv::Mat result = sourceBackground.clone();
sourceText.copyTo(sourceBackground, 255-textTransparent); // copy pixels, using the created mask.
Run Code Online (Sandbox Code Playgroud)

相反,您可以将透明像素从sourceBackground复制到sourceText,这会更快一些,因为您不需要255-textTransparent ...

无法测试代码,请告诉我是否有错误。