|
| 1 | +Image Inpainting {#tutorial_xphoto_inpainting} |
| 2 | +================ |
| 3 | + |
| 4 | +Introduction |
| 5 | +------------ |
| 6 | +In this tutorial we will show how to use the algorithm Rapid Frequency Selective Reconstructiom (FSR) for image inpainting. |
| 7 | + |
| 8 | +Basics |
| 9 | +------ |
| 10 | +Image Inpainting is the process of reconstructing damaged or missing parts of an image. |
| 11 | +This is achieved by replacing distorted pixels by pixels similar to the neighboring ones. There are several algorithms for inpainting, using different approaches for such replacement. |
| 12 | + |
| 13 | +One of those algorithms is called **Rapid Frequency Selectice Reconstruction (FSR)**. |
| 14 | +FSR reconstructs image signals by exploiting the property that small areas of images can be represented sparsely in the Fourier domain. See @cite GenserPCS2018 and @cite SeilerTIP2015 for details. |
| 15 | + |
| 16 | +FSR can be utilized for the following areas of application: |
| 17 | + |
| 18 | +-# **Error Concealment (Inpainting)**: |
| 19 | + The sampling mask indicates the missing pixels of the distorted input image to be reconstructed. |
| 20 | + |
| 21 | +-# **Non-Regular Sampling**: |
| 22 | + For more information on how to choose a good sampling mask, please review @cite GroscheICIP2018 and @cite GroscheIST2018. |
| 23 | + |
| 24 | +Example |
| 25 | +------- |
| 26 | +The following sample code shows how to use FSR for inpainting. |
| 27 | +The non-zero pixels of the error mask indicate valid image area, while zero pixels indicate area to be reconstructed. |
| 28 | +You can create an arbitrary mask manually using tools like Paint or GIMP. Start with a plain white image and draw some distortions in black. |
| 29 | + |
| 30 | + @code{.cpp} |
| 31 | + |
| 32 | + #include <opencv2/opencv.hpp> |
| 33 | + #include <opencv2/xphoto/inpainting.hpp> |
| 34 | + #include <iostream> |
| 35 | + |
| 36 | + using namespace cv; |
| 37 | + |
| 38 | + int main(int argc, char** argv) |
| 39 | + { |
| 40 | + // read image and error pattern |
| 41 | + Mat original_, mask_; |
| 42 | + original_ = imread("images/kodim22.png"); |
| 43 | + mask_ = imread("images/pattern_random.png", IMREAD_GRAYSCALE); |
| 44 | + |
| 45 | + // make sure that mask and source image have the same size |
| 46 | + Mat mask; |
| 47 | + resize(mask_, mask, original_.size(), 0.0, 0.0, cv::INTER_NEAREST); |
| 48 | + |
| 49 | + // distort image |
| 50 | + Mat im_distorted(original_.size(), original_.type(), Scalar::all(0)); |
| 51 | + original_.copyTo(im_distorted, mask); // copy valid pixels only (i.e. non-zero pixels in mask) |
| 52 | + |
| 53 | + // reconstruct the distorted image |
| 54 | + // choose quality profile fast (xphoto::INPAINT_FSR_FAST) or best (xphoto::INPAINT_FSR_BEST) |
| 55 | + Mat reconstructed; |
| 56 | + xphoto::inpaint(im_distorted, mask, reconstructed, xphoto::INPAINT_FSR_FAST); |
| 57 | + |
| 58 | + imshow("orignal image", original_); |
| 59 | + imshow("distorted image", im_distorted); |
| 60 | + imshow("reconstructed image", reconstructed); |
| 61 | + waitKey(); |
| 62 | + |
| 63 | + return 0; |
| 64 | + } |
| 65 | + @endcode |
| 66 | + |
| 67 | +Original and distorted image: |
| 68 | + |
| 69 | + |
| 70 | +Reconstruction: |
| 71 | + |
| 72 | + |
| 73 | +Left image: fast quality profile (run time 8 seconds). Right image: best quality profile (1 minute 51 seconds). |
| 74 | + |
| 75 | +Additional Resources |
| 76 | +-------------------- |
| 77 | +[Comparison of FSR to existing inpainting methods in OpenCV](https://github.com/opencv/opencv_contrib/files/3730212/inpainting_comparison.pdf) |
0 commit comments