代碼如下:
#include <opencv2/opencv.hpp>
#include <opencv2/xphoto/inpainting.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv)
{
// read image and error pattern
Mat original_, mask_;
original_ = imread("images/kodim22.png");
mask_ = imread("images/pattern_random.png", IMREAD_GRAYSCALE);
// make sure that mask and source image have the same size
Mat mask;
resize(mask_, mask, original_.size(), 0.0, 0.0, cv::INTER_NEAREST);
// distort image
Mat im_distorted(original_.size(), original_.type(), Scalar::all(0));
original_.copyTo(im_distorted, mask); // copy valid pixels only (i.e. non-zero pixels in mask)
// reconstruct the distorted image
// choose quality profile fast (xphoto::INPAINT_FSR_FAST) or best (xphoto::INPAINT_FSR_BEST)
Mat reconstructed;
xphoto::inpaint(im_distorted, mask, reconstructed, xphoto::INPAINT_FSR_FAST);
imshow("orignal image", original_);
imshow("distorted image", im_distorted);
imshow("reconstructed image", reconstructed);
waitKey();
return 0;
}
原圖和破壞後的圖:
修復後的圖:
左圖爲快速方法結果,右圖爲best質量方法結果
參考文檔:https://docs.opencv.org/4.2.0/dc/d2f/tutorial_xphoto_inpainting.html