opencv学习之图像修补

#include"stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include<opencv2/photo/photo.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std; 


#define WINDOW_NAME1 "【原始图】"
#define WINDOW_NAME2 "【修补后的图】"

Mat srcImage, inpaintMask;
Point prevoisPoint(-1, -1);
void on_Mouse(int, int, int, int, void*);

int main()
{
	Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\beauty.jpg",-1);
	if (!image.data)cout << "Please check the fold exit or not!" << endl;

	srcImage = image.clone();

	inpaintMask = Mat::zeros(srcImage.size(), CV_8U);

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

	while (1) {
		//获取键值
		char c = (char)waitKey(0);

		if (c == 27 || c == 'q' || c == 'Q')break;
		//进行图像恢复
		if (c == '2') {
			inpaintMask = Scalar::all(0);
			image.copyTo(srcImage);
			imshow(WINDOW_NAME1, srcImage);
		}
		//进行图像修补操作
		if (c == '1') {
			Mat inpaintedImage;
			inpaint(srcImage, inpaintMask, inpaintedImage, 3, INPAINT_TELEA);
			imshow(WINDOW_NAME2, inpaintedImage);
		}
	}
	

	return 0;
}

void on_Mouse(int event, int x, int y, int flags, void*) {
	//鼠标左键弹起消息
	if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON)) {
		prevoisPoint = Point(-1, -1);
	}
	//鼠标按下消息
	else if (event == EVENT_LBUTTONDOWN) {
		prevoisPoint = Point(x, y);
	}
	//鼠标按下移动进行绘制
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)) {
		Point pt(x, y);
		if (prevoisPoint.x < 0) {
			prevoisPoint = pt;
		}
		//画线
		line(inpaintMask, prevoisPoint, pt, Scalar::all(150), 5, 8, 0);
		line(srcImage, prevoisPoint, pt, Scalar::all(150), 5, 8, 0);
		prevoisPoint = pt;
		imshow(WINDOW_NAME1, srcImage);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值