用OpenCV自带的inpaint()演示
CV_EXPORTS_W void inpaint(
InputArray src,
InputArray inpaintMask,
OutputArray dst,
double inpaintRadius,
int flags
);
- src 输入图像
- inpaintMask 掩膜,和输入矩阵相同size的黑白矩阵。白色表示感兴趣区域。这里指需要修复的区域。
- dst 输出图像
- inpaintRadius 是渲染半径,一般用小半径3,大半径容易模糊。
- flags 修复方法,有两种:INPAINT_NS 和 INPAINT_TELEA。
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("images/favorite/cat.jpg");
Mat dst;
Rect rect0(624, 655, 400, 20);//这里的参数是我手动修改凑出来的
Mat mask = Mat::zeros(src.size(), CV_8UC1);
mask(rect0).setTo(255);
inpaint(src, mask, dst, 3, INPAINT_NS);
imshow("mask", mask);
imshow("src", src);
imshow("dst", dst);
waitKey(0);
return 0;
}
画图程序
创建鼠标回调函数
CV_EXPORTS void setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0);
#include <opencv2/opencv.hpp>
using namespace cv;
Mat mask_board;
int width = 5;
int paint_or_wipe = 1;
void draw_dot(int event, int x, int y, int flags, void *ustc)
{
Scalar color;
if(paint_or_wipe)
color = (255);
else
color = (0);
if(event == EVENT_LBUTTONDOWN)
rectangle(mask_board, Point(x-width/2, y-width/2), Point(x+width/2, y+width/2), color, -1);
if(event == EVENT_MOUSEMOVE and flags == EVENT_FLAG_LBUTTON)
rectangle(mask_board, Point(x-width/2, y-width/2), Point(x+width/2, y+width/2), color, -1);
}
int main()
{
Mat src = imread("images/favorite/cat.jpg");
mask_board = Mat::zeros(src.size(), CV_8UC1);
imshow("mask_board", mask_board);
setMouseCallback("mask_board", draw_dot);
int k = 1;
createTrackbar("paint_or_wipe", "mask_board", &k, 1);
while (1)
{
paint_or_wipe = k;
imshow("mask_board", mask_board);
if(waitKey(1) == 'q')
break;
}
return 0;
}
做一个去除水印小工具
利用OpenCV自带的highgui来做一个小应用,支持用鼠标划出水印位置,然后除水印。
#include <opencv2/opencv.hpp>
using namespace cv;
Mat mask_board;
const int width = 7;
int paint_or_wipe = 1;
int show = 1;
void draw_dot(int event, int x, int y, int flags, void *ustc)
{
Scalar color;
if(paint_or_wipe)
color = (255);
else
color = (0);
if(event == EVENT_LBUTTONDOWN or (event == EVENT_MOUSEMOVE and flags == EVENT_FLAG_LBUTTON))
rectangle(mask_board, Point(x - width / 2, y - width / 2), Point(x + width / 2, y + width / 2), color, -1);
}
int main()
{
Mat src = imread("images/favorite/my.jpg");
Mat dst;
mask_board = Mat(src.size(), CV_8UC1, Scalar(0));
imshow("image", src);
setMouseCallback("image", draw_dot);
createTrackbar("paint_or_wipe", "image", &paint_or_wipe, 1);
createTrackbar("show", "image", &show, 1);
while (1)
{
if(waitKey(1) == 'q')
break;
inpaint(src, mask_board, dst, 3, INPAINT_NS);
if(!show)
bitwise_and(src, Scalar(0, 0, 0), dst, mask_board);
imshow("image", dst);
}
waitKey(0);
return 0;
}
(完)