opencv图像污点修复

数字修复可用来解决图形中小区域污染一重构图像,近年来被广泛应用再去出文本和Logo图标等场景中,利用重构去除图像中的画横和污点。常用于修复图像的算法是基于快速进行的修复算法,FMM。论文的名字是An Image Inpainting Technique Based on the Fast March Method.其原理思想是先修复待修复区域边缘上的点,软后扩大区域进行遍历,知道修复完所有待修复的图像像素。
void inpaint(InputArzhuwei ray src, InputArray inpaintMask, OutputAyyay dst, double inpaintRadius, int flags)
函数解析:
实现图像选定区域的修复。输入图像源src为8bit到通道或者3通道图像;inpaintMask为输入掩码图像,8bit单通道;输出图像dst与输入图像同大小同类型;inpaintRadius用于设定像素点修复领域半径;flag为修复算法类型选择;INPAINT_NS是Navier_Stokes提出。INPAINT_TELEA是Alexandru Telea提出的。
代码如下:
 
    
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "opencv2/opencv.hpp"
  4. #include "opencv2/core/core.hpp"
  5. #include
  6. #include "opencv2/photo/photo.hpp"
  7. using namespace std;
  8. using namespace cv;
  9. Mat img, inpaintMask;
  10. Point prevPt(-1, -1);
  11. //鼠标点击绘制噪声
  12. static void onMouse(int event, int x, int y, int flags, void*)
  13. {
  14. //根据鼠标响应确定事件
  15. if (event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON)){
  16. prevPt = Point(-1, -1);
  17. }else if (event == CV_EVENT_LBUTTONDOWN){
  18. prevPt = Point(x, y);
  19. }else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
  20. {
  21. Point pt(x, y);
  22. if (prevPt.x < 0)
  23. prevPt = pt;
  24. //绘制鼠标移动轨迹
  25. line(inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0);
  26. line(img, prevPt, pt, Scalar::all(255), 5, 8, 0);
  27. prevPt = pt;
  28. imshow("image", img);
  29. }
  30. }
  31. int main()
  32. {
  33. cv::Mat src = cv::imread("C:\\Users\\LP\\Desktop\\C++\\ConsoleApplication3\\ConsoleApplication3\\1.jpg");
  34. if (src.empty())
  35. {
  36. return -1;
  37. }
  38. img = src.clone();
  39. inpaintMask = Mat::zeros(img.size(), CV_8U);
  40. cv::imshow("srcImage", img);
  41. //鼠标事件回调函数
  42. setMouseCallback("srcImage", onMouse, 0);
  43. //完成图像污点修复
  44. for (;;)
  45. {
  46. char c = (char)waitKey(0);
  47. if(c == 27)
  48. break;
  49. if (c == 'r')
  50. {
  51. inpaintMask = Scalar::all(0);
  52. src.copyTo(img);
  53. cv::imshow("srcImage", img);
  54. }
  55. if (c == 'i' || c == ' ')
  56. {
  57. Mat resMat;
  58. cv::inpaint(img, inpaintMask, resMat, 3, CV_INPAINT_TELEA);
  59. cv::imshow("resMat", resMat);
  60. }
  61. }
  62. return 0;
  63. }

结果如下:
 

你可能感兴趣的:(opencv)