2016-08-18 103 views
1

我有一個Mat包含帶有黑色背景的文本和包含背景的Mat。我想合併2個Mat,所以文本位於背景的頂部。兩個墊子都是BGR(CV_8UC3)和相同的尺寸(cols和rows)。OpenCV函數合併Mat除黑色/透明像素

什麼是OpenCV這樣做的方式?

我知道幾個函數,但|=+=正在改變文本顏色,addWeighted()只適用於如果我想要一個圖像稍微透明。我只想做一個完整的兩個合併(除了不是文本圖像中的黑色像素)。我正在尋找一個OpenCV函數,而不是像素編輯自己,如果這是一個選項,那麼LUT會很好。

enter image description here

回答

4

試試這個:

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 ...

無法測試代碼,請告訴我是否有錯誤。

+1

啊...遲了幾分鐘......太糟糕了......我已經準備好了[圖片](http://imgur.com/a/OOI3q)來回答這個問題:(:(自由自在使用它們; D) – Miki

+0

@Micka,你認爲使用'threshold(textImg,mask,1,255,THRESH_BINARY); textImg.copyTo(bkImg,mask);'會更快嗎? – Merl

+0

@Merl,也許可以。不知道閾值是否適用於彩色圖像。可能有一些方法可以比inRange更快。 – Micka