且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在openCV中删除图像的特定部分

更新时间:2023-01-15 17:44:57

如果您已经有了要替换的点或什至更好的轮廓(例如第二张图中以绿色绘制的轮廓),请查看inpaint : http://docs.opencv.org/3.1.0/d1/d0d/group__photo.html#gaedd30dfa0214fec4c88138b51d678085

If you already have the points or even better contours you want to replace (like these drawn in green in your 2nd image), have a look at inpaint: http://docs.opencv.org/3.1.0/d1/d0d/group__photo.html#gaedd30dfa0214fec4c88138b51d678085

我认为这正是您要寻找的!参见此处的示例: https://en.wikipedia.org/wiki/Inpainting

I think this is exactly what you are looking for! See here for an example: https://en.wikipedia.org/wiki/Inpainting

过程非常简单:

  1. 制作一个填充有轮廓( drawContours ,厚度: CV_FILLED )的蒙版,以对其各自的环境进行修补/替换.
  2. inpaint (带有此蒙版).
  1. Make a mask filled with your contours (drawContours, thickness: CV_FILLED) to be inpainted/replaced with their respective environment and
  2. inpaint with this mask.

我应该提到,这仅适用于8bit图像.

I should mention that this does only work with 8bit-images.

下面的(未经测试的)代码段应该可以实现.

The following (untested) code snippet should do it.

Mat mask = Mat::zeros(img.size(), CV_8UC1);
for (int i = 0; i < contours.size(); i++)
  drawContours(mask, contours, i, Scalar::all(255), CV_FILLED);
Mat dst;
double radius = 20;    
inpaint(img, mask, dst, radius, INPAINT_NS);
//inpaint(img, mask, dst, radius, INPAINT_TELEA);
imshow("dst", dst);
waitKey();

要绘制几个点的轮廓,请执行以下操作:轮廓只是点的向量,打包成另一个向量.因此,对于给定的 vector< Point&gt ;,解决方案应该是这样的.点.

To make a contour of a couple of points: A contour is just a vector of points, packed into another vector. So, the solution should be something like this with a given vector<Point> points.

vector<vector<Point> > contours;
contours.push_back(points);
drawContours(mask, contours, 0, Scalar::all(255), CV_FILLED);