laplace.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "opencv2/videoio.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. using namespace cv;
  8. using namespace std;
  9. static void help(char** argv)
  10. {
  11. cout <<
  12. "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
  13. "It captures from the camera of your choice: 0, 1, ... default 0\n"
  14. "Call:\n"
  15. << argv[0] << " -c=<camera #, default 0> -p=<index of the frame to be decoded/captured next>\n" << endl;
  16. }
  17. enum {GAUSSIAN, BLUR, MEDIAN};
  18. int sigma = 3;
  19. int smoothType = GAUSSIAN;
  20. int main( int argc, char** argv )
  21. {
  22. cv::CommandLineParser parser(argc, argv, "{ c | 0 | }{ p | | }");
  23. help(argv);
  24. VideoCapture cap;
  25. string camera = parser.get<string>("c");
  26. if (camera.size() == 1 && isdigit(camera[0]))
  27. cap.open(parser.get<int>("c"));
  28. else
  29. cap.open(samples::findFileOrKeep(camera));
  30. if (!cap.isOpened())
  31. {
  32. cerr << "Can't open camera/video stream: " << camera << endl;
  33. return 1;
  34. }
  35. cout << "Video " << parser.get<string>("c") <<
  36. ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
  37. ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
  38. ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
  39. int pos = 0;
  40. if (parser.has("p"))
  41. {
  42. pos = parser.get<int>("p");
  43. }
  44. if (!parser.check())
  45. {
  46. parser.printErrors();
  47. return -1;
  48. }
  49. if (pos != 0)
  50. {
  51. cout << "seeking to frame #" << pos << endl;
  52. if (!cap.set(CAP_PROP_POS_FRAMES, pos))
  53. {
  54. cerr << "ERROR: seekeing is not supported" << endl;
  55. }
  56. }
  57. namedWindow("Laplacian", WINDOW_AUTOSIZE);
  58. createTrackbar("Sigma", "Laplacian", &sigma, 15, 0);
  59. Mat smoothed, laplace, result;
  60. for(;;)
  61. {
  62. Mat frame;
  63. cap >> frame;
  64. if( frame.empty() )
  65. break;
  66. int ksize = (sigma*5)|1;
  67. if(smoothType == GAUSSIAN)
  68. GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
  69. else if(smoothType == BLUR)
  70. blur(frame, smoothed, Size(ksize, ksize));
  71. else
  72. medianBlur(frame, smoothed, ksize);
  73. Laplacian(smoothed, laplace, CV_16S, 5);
  74. convertScaleAbs(laplace, result, (sigma+1)*0.25);
  75. imshow("Laplacian", result);
  76. char c = (char)waitKey(30);
  77. if( c == ' ' )
  78. smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
  79. if( c == 'q' || c == 'Q' || c == 27 )
  80. break;
  81. }
  82. return 0;
  83. }