使用opencv函数inPaint()人像去痣

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"
#include
using namespace std;
using namespace cv;

#define WINDOW_NAME1 "原始图"   

Mat imgSrc, inpaintMask;
Point prePoint;  //上一个鼠标点

static void On_Mouse(int event, int x, int y, int flags, void*)  //鼠标画线响应函数
{
	if (event == EVENT_LBUTTONDOWN)
		prePoint = Point(x, y);
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))//画白色线条
	{
		Point pt(x, y);
		line(inpaintMask, prePoint, pt, Scalar::all(255), 3, 8, 0);//单通道掩膜,需要修复的像素此时为白色
		line(imgSrc, prePoint, pt, Scalar::all(255), 3, 8, 0);
		prePoint = pt;
		imshow(WINDOW_NAME1, imgSrc);
	}
}


//程序主要部分
int main( int argc, char** argv )
{
	 imgSrc = imread("zhi.jpg", 1);//三通道彩色图像
	 Mat imgTemp=imgSrc.clone();
	 imshow("最初图像",imgTemp);
	if (!imgSrc.data)
	{
		cout << "no image!filepath error!~!" << endl;
		return false;
	}
	inpaintMask = Mat::zeros(imgSrc.size(), CV_8UC1);
	imshow(WINDOW_NAME1, imgSrc);
	imshow("最初图像",imgTemp);
	setMouseCallback(WINDOW_NAME1, On_Mouse, 0);
 
	while (1)
	{
		int key = waitKey();
		switch (key)
		{
 
		    case 'a':
		    {
					Mat inpaintedImage;
					inpaint(imgSrc, inpaintMask, inpaintedImage, 5, INPAINT_TELEA);
					imshow("修补方法:_TELEA", inpaintedImage);
		    }
		    break;
		    case 'b':
		    {
					Mat inpaintedImage;
					inpaint(imgSrc, inpaintMask, inpaintedImage,5, INPAINT_NS);
					imshow("修补方法:_NS" , inpaintedImage);
		    }
		    break;
		}
		if (key == 27)
			break;
	}
      return 0;
}
原图:

使用opencv函数inPaint()人像去痣_第1张图片


把痣的位置圈出来:

使用opencv函数inPaint()人像去痣_第2张图片


见证奇迹的时刻,修补后是这样了

使用opencv函数inPaint()人像去痣_第3张图片

美美哒

你可能感兴趣的:(图像处理)