2

我使用OpenCV在几个多边形(绘制在纸上,参见示例)中进行绘制。opencv在一个图像上覆盖另一个图像,用蒙版和在图中绘制

一些传说规格:

  • 绿框是那里绘制场景“边界”,就在那里供参考。
  • 蓝色的球在场景中浮动,当球碰到多边形时,场景会被重新渲染并具有适当的遮罩,就好像球击碎了物体一样。

这里是参考代码,假设inpaintedScenetransformedSceneoutputFramecv::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(+ =):

enter image description here

这对我来说并不好,因为我在被破坏的多边形上获得了透明度。选项2号

结果(CopyTo从):

enter image description here

这也是不好的,因为你可以看到,多边形的破坏部分是怎样的一个“加边”或“陷害”与黑色(即使多边形是另一种颜色) - 什么可以解决这个问题?

回答

1

找到了!

我已经添加到warpPerspectivetransformedScene “近邻” 插值:

cv::warpPerspective(transformedScene, transformedScene, warpImageMat, outputFrame.size(), CV_INTER_NN); 

其中warpImageMat的类型是cv::Mat

here OpenCV的

干杯更多warpPerspective功能!

+0

重要的是要注意'warpPerspective'可以做一个变换像素落在几个边界之间的插值。 'CV_INTER_NN'取最近的邻居,而其他人对相邻像素进行一些内插,因此可能以“意外”值结束。 –

相关问题