1.定製版本
我會明確地寫:
const int cols = drawing.cols;
const int rows = drawing.rows;
for (int j = 0; j < rows; j++) {
const uint8_t* p_draw = drawing.ptr(j); //Take a pointer to j-th row of the image to be drawn
uint8_t* p_dest = webcam_img.ptr(j); //Take a pointer to j-th row of the destination image
for (int i = 0; i < cols; i++) {
//Check all three channels BGR
if(p_draw[0] | p_draw[1] | p_draw[2]) { //Using binary OR should ease the optimization work for the compiler
p_dest[0] = p_draw[0]; //If the pixel is not zero,
p_dest[1] = p_draw[1]; //copy it (overwrite) in the destination image
p_dest[2] = p_draw[2];
}
p_dest += 3; //Move to the next pixel
p_draw += 3;
}
}
當然你也可以在參數(const cv::Mat& drawing, cv::Mat& webcam_img)
功能移到此代碼。
2. OpenCV的「純粹」的版本
但純OpenCV的方法是如下:
cv::Mat mask;
//Create a single channel image where each pixel != 0 if it is colored in your "drawing" image
cv::cvtColor(drawing, mask, CV_BGR2GRAY);
//Copy to destination image only pixels that are != 0 in the mask
drawing.copyTo(webcam_img, mask);
效率較低(顏色轉換,以創建蒙版是有點貴),但肯定更緊湊。小記:如果您有一種非常暗的顏色,例如(0,0,1)
,它將在灰度級中轉換爲0
,但它不起作用。
另外請注意,這可能是重新繪製相同覆蓋(線條,圓)在您的目的地形象,基本上調用所做創建您drawing
圖像相同的抽籤操作成本更低。
完美的作品,非常感謝。我在一個函數中實現了第一個解決方案。這工作得很好。 然後我讀了最後一個註釋,只是將我的覆蓋圖重繪到'webcam_img'中。這可能是我說的最快最簡單的解決方案。太好了謝謝。 SemtexB – SemtexB