試試這個:
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.
相反,你可以在透明像素從sourceBackground到sourceText這是一個有點複製速度更快,因爲你並不需要255個textTransparent ...
無法測試代碼,請告訴我是否有錯誤。
啊...遲了幾分鐘......太糟糕了......我已經準備好了[圖片](http://imgur.com/a/OOI3q)來回答這個問題:(:(自由自在使用它們; D) – Miki
@Micka,你認爲使用'threshold(textImg,mask,1,255,THRESH_BINARY); textImg.copyTo(bkImg,mask);'會更快嗎? – Merl
@Merl,也許可以。不知道閾值是否適用於彩色圖像。可能有一些方法可以比inRange更快。 – Micka