当前位置:网站首页>[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood

[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood

2022-06-25 23:19:00 Ten year dream Lab

The left side is the repaired   Source image on the right  

Source code :

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help(char** argv)
{
    cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
        << "with surrounding image areas.\n"
        "Using OpenCV version %s\n" << CV_VERSION << "\n"
        "Usage:\n" << argv[0] << " [image_name -- Default fruits.jpg]\n" << endl;

    cout << "Hot keys: \n"
        "\tESC - quit the program\n"
        "\tr - restore the original image\n"
        "\ti or SPACE - run inpainting algorithm\n"
        "\t\t(before running it, paint something on the image)\n" << endl;
}

Mat img, inpaintMask;
Point prevPt(-1, -1);

static void onMouse(int event, int x, int y, int flags, void*)
{
    if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON))// Non left key press    Left key up 
        prevPt = Point(-1, -1);// Reset the previous point 
    else if (event == EVENT_LBUTTONDOWN) // Press the left key for the first time 
        prevPt = Point(x, y);// Last point 
    else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))// Left key press   Mouse movement 
    {
        Point pt(x, y);// Current point 
        if (prevPt.x < 0)
            prevPt = pt;// Last point 
        line(inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0);// Draw the repair area , Nonzero pixel 
        line(img, prevPt, pt, Scalar::all(255), 5, 8, 0);// Draw... On the original 
        prevPt = pt;// Update the previous point 
        imshow("image", img);
    }
}


int main(int argc, char** argv)
{
    cv::CommandLineParser parser(argc, argv, "{@image|fruits.jpg|}");
    help(argv);
    // Load image 
    string filename = samples::findFile(parser.get<string>("@image"));
    Mat img0 = imread(filename, IMREAD_COLOR);
    if (img0.empty())
    {
        cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
        return 0;
    }

    namedWindow("image", WINDOW_AUTOSIZE);

    img = img0.clone();
    inpaintMask = Mat::zeros(img.size(), CV_8U);// Repair mask 

    imshow("image", img);
    setMouseCallback("image", onMouse, NULL);// Mouse callback 

    for (;;)
    {
        char c = (char)waitKey();

        if (c == 27)
            break;

        if (c == 'r')
        {
            inpaintMask = Scalar::all(0);
            img0.copyTo(img);
            imshow("image", img);
        }

        if (c == 'i' || c == ' ')
        {
            Mat inpainted;
            /** @brief  Use region neighborhood to restore the selected region in the image .

            @param src  Input  8  position 、16  Bit unsigned or  32  Bit floating point  1  Channel or  8  position  3  Channel image .
            @param inpaintMask  Repair mask ,8  position  1  Channel image . Non zero pixels represent areas that need to be repaired .
            @param dst  Output and  src  Images of the same size and type .
            @param inpaintRadius  The radius of the circular neighborhood of each repair point considered by the algorithm .
            @param  The tag may be  cv::INPAINT_NS  or  cv::INPAINT_TELEA  How to fix it 

             This function reconstructs the selected image region from the pixels near the region boundary . This function can be used to remove dust and scratches from scanned photos , Or remove unwanted objects from still images or videos . For more information , see also  <http://en.wikipedia.org/wiki/Inpainting>.

            @ note 
               -  Examples of using repair techniques can be found in  opencv_source_code/samples/cpp/inpaint.cpp  find 
               - (Python)  Examples of using repair techniques can be found in  opencv_source_code/samples/python/inpaint.py  find 
             */
            inpaint(img, inpaintMask, inpainted, 3, INPAINT_TELEA);
            imshow("inpainted image", inpainted);
        }
    }

    return 0;
}

原网站

版权声明
本文为[Ten year dream Lab]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251953403920.html