【问题标题】:opencv overlaying one image over another with masking and in-paintingopencv用掩蔽和修复将一个图像覆盖在另一个图像上
【发布时间】:2014-07-05 23:15:05
【问题描述】:

我正在使用 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);

这些都是对我没有好处的结果:

选项 1 (+=) 的结果:

这对我不利,因为我得到了被破坏的多边形的透明度。

选项 2 (copyTo) 的结果:

这也不好,因为如您所见,多边形的被破坏部分是一种黑色的“边框”或“框架”(即使多边形是另一种颜色) - 什么可以解决这个问题?

【问题讨论】:

    标签: c++ opencv image-processing image-manipulation image-masking


    【解决方案1】:

    找到了!

    我已使用“最近邻”插值将 warpPerspective 添加到 transformedScene

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

    其中warpImageMatcv::Mat 的类型

    阅读here 更多关于warpPerspective OpenCV 的功能

    干杯!

    【讨论】:

    • 重要的是要注意warpPerspective 可能会在转换后的像素落在几个之间的边界处进行插值。 CV_INTER_NN 采用最近的邻居,而其他人则对相邻像素进行一些插值,因此可能最终得到“意外”值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多