OpenCV3之——图像修补inpaint()函数

#include 
#include 
using namespace std;
using namespace cv;

#define WINDOW_NAME1 "原始图"
#define WINDOW_NAME2 "修补后的效果图"

Mat srcImage1, inpaintMask;
Point previousPoint(-1, -1);//初始坐标

static void on_Mouse(int event, int x, int y, int flags, void*);

int main(int argc, char** argv) {
	Mat srcImage = imread("1.jpg", -1);
	if (!srcImage.data) {
		cout << "读入图片错误\n" << endl;
		return false;
	}
	srcImage1 = srcImage.clone();
	inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);

	imshow(WINDOW_NAME1, srcImage1);

	//设置鼠标回调消息
	setMouseCallback(WINDOW_NAME1, on_Mouse, 0);

	//轮询按键
	while (1) {
		//获取按键
		char c = (char)waitKey();

		if (c == 27)
			break;
		if (c == '2') {
			inpaintMask = Scalar::all(0);
			srcImage.copyTo(srcImage1);
			imshow(WINDOW_NAME1, srcImage1);
		}

		if (c == '1' || c == ' ') {
			Mat inpaintedImage;
			inpaint(srcImage1, inpaintMask, inpaintedImage, 3, INPAINT_TELEA);
			imshow(WINDOW_NAME2, inpaintedImage);
		}
	}
	return 0;
}

static void on_Mouse(int event, int x, int y, int flags, void*) {
	if (event == EVENT_LBUTTONUP || !(flags&EVENT_FLAG_LBUTTON))
		previousPoint = Point(-1, -1);
	else if (event == EVENT_LBUTTONDOWN)
		previousPoint = Point(x, y);
	else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON)) {
		Point pt(x, y);
		if (previousPoint.x < 0)
			previousPoint = pt;
		line(inpaintMask, previousPoint, pt, Scalar::all(255), 5, 8, 0);
		line(srcImage1, previousPoint, pt, Scalar::all(255), 5, 8, 0);
		previousPoint = pt;
		imshow(WINDOW_NAME1, srcImage1);
	}
}

OpenCV3之——图像修补inpaint()函数_第1张图片

 OpenCV3之——图像修补inpaint()函数_第2张图片

你可能感兴趣的:(OpenCV学习)