日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

OpenCV学习2-----使用inpaint函数进行图像修复

發(fā)布時間:2023/12/19 综合教程 36 生活家
生活随笔 收集整理的這篇文章主要介紹了 OpenCV学习2-----使用inpaint函数进行图像修复 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

安裝opencv時,在opencv的安裝路徑下,

sourcessamplescpp 路徑里面提供了好多經(jīng)典的例子,很值得學(xué)習(xí)。

這次的例子是利用inpaint函數(shù)進行圖像修復(fù)。

CV_EXPORTS_W void inpaint( InputArray src, InputArray inpaintMask,
                           OutputArray dst, double inpaintRadius, int flags );

其中

InputArray src 表示要修復(fù)的圖像,

InputArray inpaintMask表示修復(fù)模板

OutputArray dst 表示修復(fù)后的圖像,

double inpaintRadius 表示修復(fù)的半徑,

int flags 表示修復(fù)使用的算法 。 opencv提供了兩種選擇CV_INPAINT_TELEA 和 CV_INPAINT_NS。

感覺兩種算法修復(fù)效果都還不錯,但是都需要事先準(zhǔn)備修復(fù)模板mask,也就是inpaintMask 這個參數(shù)。

例子里面用鼠標(biāo)在圖片上劃線,劃線的同時也更新了mask,而真正應(yīng)用的時候需要事先設(shè)計好這個mask。

文末有最終效果圖。

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

#include <iostream>

using namespace cv;
using namespace std;

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

    cout << "Hot keys: 
"
        "	ESC - quit the program
"
        "	r - restore the original image
"
        "	i or SPACE - run inpainting algorithm
"
        "		(before running it, paint something on the image)
" << endl;
}

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

static void onMouse(int event, int x, int y, int flags, void*)
{
    if (event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON))
        prevPt = Point(-1, -1);
    else if (event == CV_EVENT_LBUTTONDOWN)
        prevPt = Point(x, y);
    else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
    {
        Point pt(x, y);
        if (prevPt.x < 0)
            prevPt = pt;
        line(inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0);//mask
        line(img, prevPt, pt, Scalar::all(255), 5, 8, 0);
        prevPt = pt;
        imshow("image", img);
    }
}


int main(int argc, char** argv)
{
    char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
    Mat img0 = imread(filename, -1);
    if (img0.empty())
    {
        cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>
" << endl;
        return 0;
    }

    help();

    namedWindow("image", 1);

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

    imshow("image", img);
    setMouseCallback("image", onMouse, 0);

    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;
            //inpaint(img, inpaintMask, inpainted, 3, CV_INPAINT_TELEA);//CV_INPAINT_NS
            inpaint(img, inpaintMask, inpainted, 3, CV_INPAINT_TELEA);
            imshow("inpainted image", inpainted);
        }
    }

    return 0;
}

圖1 原圖

圖2CV_INPAINT_NS 算法修復(fù)效果圖

圖3CV_INPAINT_TELEA算法修復(fù)效果圖

總結(jié)

以上是生活随笔為你收集整理的OpenCV学习2-----使用inpaint函数进行图像修复的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。