1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/photo/photo.hpp" #include <iostream> using namespace cv; using namespace std;
#define WINDOW_NAME0 "Original Reference" #define WINDOW_NAME1 "Original" #define WINDOW_NAME2 "Result"
Mat srcImage0, srcImage1, inpaintMask; Point previousPoint(-1, -1);
static void ShowHelpText() { printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"); printf("\n\n\t\t\t此为本书OpenCV3版的第78个配套示例程序\n"); printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION); printf("\n\n ----------------------------------------------------------------------------\n");
printf("\n\n\n\t欢迎来到【图像修复】示例程序~\n"); printf("\n\t请在进行图像修复操作之前,在【原始图】窗口中进行适量的绘制" "\n\n\t按键操作说明: \n\n" "\t\t【鼠标左键】-在图像上绘制白色线条\n\n" "\t\t键盘按键【ESC】- 退出程序\n\n" "\t\t键盘按键【1】或【SPACE】-进行图像修复操作 \n\n"); }
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) { ShowHelpText();
Mat srcImage = imread("1.jpg", -1); if (!srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定图片存在~! \n"); return false; } srcImage0 = srcImage.clone(); srcImage1 = srcImage.clone(); inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);
imshow(WINDOW_NAME0, srcImage0); 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; }
|