【笔记】OpenCV图像修复和复原 inpaint():src、inpaintMask、dst、inpaintRadius、flags

注:

对于自己模糊处理得到的图片然后在去模糊使用 SRN。

注:

对于CV_EVENT_LBUTTONDOWN CV_RGB2GRAY CV_THRESH_BINARY 都显示未定义的标识符 我的头文件和你网页上显示的一样 使用的是vs2017 opencv4.1.0 这种情况该怎么解决?

#include<opencv2> #include<opencv2> 加入这两个头文件后 正常了 请问 为什么你的程序里不用加这个呢 是不在在哪里可以设置不用加这两个头文件呢?

CV_RGB2GRAY改成COLOR_RGB2GRAY CV_THRESH_BINARY改成THRESH_BINARY

正文:

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

#include <imgproc\imgproc.hpp>
#include <highgui\highgui.hpp>
#include <photo\photo.hpp>
 
using namespace cv;
 
//全区域阈值处理+Mask膨胀处理
int main()
{
	Mat imageSource = imread("Test.jpg");
	if (!imageSource.data)
	{
		return -1;
	}
	imshow("原图", imageSource);
	Mat imageGray;
	//转换为灰度图
	cvtColor(imageSource, imageGray, CV_RGB2GRAY, 0);
	Mat imageMask = Mat(imageSource.size(), CV_8UC1, Scalar::all(0));
 
	//通过阈值处理生成Mask
	threshold(imageGray, imageMask, 240, 255, CV_THRESH_BINARY);
	Mat Kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
	//对Mask膨胀处理,增加Mask面积
	dilate(imageMask, imageMask, Kernel);
 
	//图像修复
	inpaint(imageSource, imageMask, imageSource, 5, INPAINT_TELEA);
	imshow("Mask", imageMask);
	imshow("修复后", imageSource);
	waitKey();
}

原始图像:

 

#include <imgproc/imgproc.hpp>
#include <highgui/highgui.hpp>
#include <core/core.hpp>
#include <photo/photo.hpp>
 
using namespace cv;
 
Point ptL, ptR; //鼠标画出矩形框的起点和终点
Mat imageSource, imageSourceCopy;
Mat ROI; //原图需要修复区域的ROI
 
//鼠标回调函数
void OnMouse(int event, int x, int y, int flag, void *ustg);
 
//鼠标圈定区域阈值处理+Mask膨胀处理
int main()
{
	imageSource = imread("Test.jpg");
	if (!imageSource.data)
	{
		return -1;
	}
	imshow("原图", imageSource);
	setMouseCallback("原图", OnMouse);
	waitKey();
}
void OnMouse(int event, int x, int y, int flag, void *ustg)
{
	if (event == CV_EVENT_LBUTTONDOWN)
	{
		ptL = Point(x, y);
		ptR = Point(x, y);
	}
	if (flag == CV_EVENT_FLAG_LBUTTON)
	{
		ptR = Point(x, y);
		imageSourceCopy = imageSource.clone();
		rectangle(imageSourceCopy, ptL, ptR, Scalar(255, 0, 0));
		imshow("原图", imageSourceCopy);
	}
	if (event == CV_EVENT_LBUTTONUP)
	{
		if (ptL != ptR)
		{
			ROI = imageSource(Rect(ptL, ptR));
			imshow("ROI", ROI);
			waitKey();
		}
	}
	//单击鼠标右键开始图像修复
	if (event == CV_EVENT_RBUTTONDOWN)
	{
		imageSourceCopy = ROI.clone();
		Mat imageGray;
		cvtColor(ROI, imageGray, CV_RGB2GRAY); //转换为灰度图
		Mat imageMask = Mat(ROI.size(), CV_8UC1, Scalar::all(0));
 
		//通过阈值处理生成Mask
		threshold(imageGray, imageMask, 235, 255, CV_THRESH_BINARY);
		Mat Kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
		dilate(imageMask, imageMask, Kernel);  //对Mask膨胀处理
		inpaint(ROI, imageMask, ROI, 9, INPAINT_TELEA);  //图像修复
		imshow("Mask", imageMask);
		imshow("修复后", imageSource);
	}
}

 

 

#include <imgproc/imgproc.hpp>
#include <highgui/highgui.hpp>
#include <core/core.hpp>
#include <photo/photo.hpp>
 
using namespace cv;
 
Point ptL, ptR; //鼠标画出矩形框的起点和终点
Mat imageSource, imageSourceCopy;
Mat ROI; //原图需要修复区域的ROI
 
//鼠标回调函数
void OnMouse(int event, int x, int y, int flag, void *ustg);
 
//鼠标圈定区域
int main()
{
	imageSource = imread("Test.jpg");
	if (!imageSource.data)
	{
		return -1;
	}
	imshow("原图", imageSource);
	setMouseCallback("原图", OnMouse);
	waitKey();
}
void OnMouse(int event, int x, int y, int flag, void *ustg)
{
	if (event == CV_EVENT_LBUTTONDOWN)
	{
		ptL = Point(x, y);
		ptR = Point(x, y);
	}
	if (flag == CV_EVENT_FLAG_LBUTTON)
	{
		ptR = Point(x, y);
		imageSourceCopy = imageSource.clone();
		rectangle(imageSourceCopy, ptL, ptR, Scalar(255, 0, 0));
		imshow("原图", imageSourceCopy);
	}
	if (event == CV_EVENT_LBUTTONUP)
	{
		if (ptL != ptR)
		{
			ROI = imageSource(Rect(ptL, ptR));
			imshow("ROI", ROI);
			waitKey();
		}
	}
	//单击鼠标右键开始图像修复
	if (event == CV_EVENT_RBUTTONDOWN)
	{
		imageSourceCopy = Mat(imageSource.size(), CV_8UC1, Scalar::all(0));
		Mat imageMask = imageSourceCopy(Rect(ptL, ptR));
		//生成一个跟ROI大小一样的值全为1的区域
		Mat imageMaskCopy = Mat(imageMask.size(), CV_8UC1, Scalar::all(1));
		imageMaskCopy.copyTo(imageMask);
		inpaint(imageSource, imageSourceCopy, imageSource, 9, INPAINT_TELEA);  //图像修复
		imshow("Mask", imageSourceCopy);
		imshow("修复后", imageSource);
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值