create_mask.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * create_mask.cpp
  3. *
  4. * Author:
  5. * Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
  6. *
  7. * This tutorial demonstrates how to make mask image (black and white).
  8. * The program takes as input a source image and outputs its corresponding
  9. * mask image.
  10. */
  11. #include "opencv2/imgproc.hpp"
  12. #include "opencv2/imgcodecs.hpp"
  13. #include "opencv2/highgui.hpp"
  14. #include <iostream>
  15. using namespace std;
  16. using namespace cv;
  17. Mat src, img1, mask, final;
  18. Point point;
  19. vector<Point> pts;
  20. int drag = 0;
  21. int var = 0;
  22. int flag = 0;
  23. void mouseHandler(int, int, int, int, void*);
  24. void mouseHandler(int event, int x, int y, int, void*)
  25. {
  26. if (event == EVENT_LBUTTONDOWN && !drag)
  27. {
  28. if (flag == 0)
  29. {
  30. if (var == 0)
  31. img1 = src.clone();
  32. point = Point(x, y);
  33. circle(img1, point, 2, Scalar(0, 0, 255), -1, 8, 0);
  34. pts.push_back(point);
  35. var++;
  36. drag = 1;
  37. if (var > 1)
  38. line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
  39. imshow("Source", img1);
  40. }
  41. }
  42. if (event == EVENT_LBUTTONUP && drag)
  43. {
  44. imshow("Source", img1);
  45. drag = 0;
  46. }
  47. if (event == EVENT_RBUTTONDOWN)
  48. {
  49. flag = 1;
  50. img1 = src.clone();
  51. if (var != 0)
  52. {
  53. polylines( img1, pts, 1, Scalar(0,0,0), 2, 8, 0);
  54. }
  55. imshow("Source", img1);
  56. }
  57. if (event == EVENT_RBUTTONUP)
  58. {
  59. flag = var;
  60. final = Mat::zeros(src.size(), CV_8UC3);
  61. mask = Mat::zeros(src.size(), CV_8UC1);
  62. fillPoly(mask, pts, Scalar(255, 255, 255), 8, 0);
  63. bitwise_and(src, src, final, mask);
  64. imshow("Mask", mask);
  65. imshow("Result", final);
  66. imshow("Source", img1);
  67. }
  68. if (event == EVENT_MBUTTONDOWN)
  69. {
  70. pts.clear();
  71. var = 0;
  72. drag = 0;
  73. flag = 0;
  74. imshow("Source", src);
  75. }
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. CommandLineParser parser(argc, argv, "{@input | lena.jpg | input image}");
  80. parser.about("This program demonstrates using mouse events\n");
  81. parser.printMessage();
  82. cout << "\n\tleft mouse button - set a point to create mask shape\n"
  83. "\tright mouse button - create mask from points\n"
  84. "\tmiddle mouse button - reset\n";
  85. String input_image = parser.get<String>("@input");
  86. src = imread(samples::findFile(input_image));
  87. if (src.empty())
  88. {
  89. printf("Error opening image: %s\n", input_image.c_str());
  90. return 0;
  91. }
  92. namedWindow("Source", WINDOW_AUTOSIZE);
  93. setMouseCallback("Source", mouseHandler, NULL);
  94. imshow("Source", src);
  95. waitKey(0);
  96. return 0;
  97. }