图像修补

1、实现图像修补的函数

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

参数一:输入图像阵列,Mat 类且是8位单通道或三通道图像

参数二:修复掩摸,8位单通道图像

参数三:输出图像

参数四:修复算法的参考半径

参数五:修补方法的标志符

INPAINT_NS  基于Noviers_stokes方程的方法

INPAINT_TELEA Alexandru telea方法

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/photo/photo.hpp>
#include <iostream>
#define WINDOW_NAME1  "原始图"
#define WINDOW_NAME2  "修复图"
using namespace std;
using namespace cv;

Mat srcImage1,inpaintMask;
Point previousPoint(-1,-1);
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);
	}
}
int main(int argc,char *argv[])
{
	if(argc!=2)
	{
		cout<<"input error"<<endl;
		return -1;
	}
	Mat srcImage = imread(argv[1],-1);
	if(srcImage.empty())
	{
		cout<<"read photo fail"<<endl;
		return -1;		
	}
	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(0);
		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;
}


来自:opencv编程入门 毛星云pdf


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值