2
我使用OpenCV在幾個多邊形(繪製在紙上,參見示例)中進行繪製。opencv在一個圖像上覆蓋另一個圖像,用蒙版和在圖中繪製
一些傳說規格:
- 綠框是那裏繪製場景「邊界」,就在那裏供參考。
- 藍色的球在場景中浮動,當球碰到多邊形時,場景會被重新渲染並具有適當的遮罩,就好像球擊碎了物體一樣。
這裏是參考代碼,假設inpaintedScene
,transformedScene
和outputFrame
是cv::Mat
:
cv::Mat mask_image(outputFrame.size(), CV_8U, BLACK_COLOR);
std::vector<cv::Point*> destroyedPolygons = //some sub-polygons that has been hit by the ball
std::vector<int> destroyedPolygonsPointCount = //points count for each destroyed polygon
for (int i = 0; i < destroyedPolygons.size(); i++)
{
const cv::Point* ppt[1] = { destroyedPolygons[i] };
int npt[] = { destroyedPolygonsPointCount[i] };
cv::fillPoly(mask_image, ppt, npt, 1, WHITE_COLOR);
}
// now that the mask is prepared, copy the points from the inpainted to the scene
inpaintedScene.copyTo(transformedScene, mask_image);
// here I have two options:
//option 1
outputFrame += transformedScene;
//option 2
transformedScene.copyTo(outputFrame, transformedScene);
這些都是其結果他們都不是爲我好:
選擇的結果no.1(+ =):
這對我來說並不好,因爲我在被破壞的多邊形上獲得了透明度。選項2號
結果(CopyTo從):
這也是不好的,因爲你可以看到,多邊形的破壞部分是怎樣的一個「加邊」或「陷害」與黑色(即使多邊形是另一種顏色) - 什麼可以解決這個問題?
重要的是要注意'warpPerspective'可以做一個變換像素落在幾個邊界之間的插值。 'CV_INTER_NN'取最近的鄰居,而其他人對相鄰像素進行一些內插,因此可能以「意外」值結束。 –