main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "opencv2/imgcodecs.hpp"
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. using namespace cv;
  8. using namespace std;
  9. Mat src; Mat src_gray;
  10. int thresh = 100;
  11. int max_thresh = 255;
  12. RNG rng(12345);
  13. /// Function header
  14. void thresh_callback(int, void* );
  15. /** @function main */
  16. int main( int argc, char** argv )
  17. {
  18. /// Load source image and convert it to gray
  19. src = imread( "C:\\Users\\Sky\\Downloads\\photo.jpg", IMREAD_COLOR );
  20. /// Convert image to gray and blur it
  21. cvtColor( src, src_gray, COLOR_BGR2GRAY );
  22. blur( src_gray, src_gray, Size(3,3) );
  23. /// Create Window
  24. char* source_window = "Source";
  25. namedWindow( source_window, WINDOW_AUTOSIZE );
  26. imshow( source_window, src );
  27. createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
  28. thresh_callback( 0, 0 );
  29. waitKey(0);
  30. return(0);
  31. }
  32. /** @function thresh_callback */
  33. void thresh_callback(int, void* )
  34. {
  35. Mat canny_output;
  36. vector<vector<Point> > contours;
  37. vector<Vec4i> hierarchy;
  38. /// Detect edges using canny
  39. Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  40. /// Find contours
  41. findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  42. /// Draw contours
  43. Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  44. for( int i = 0; i < contours.size(); i++ )
  45. {
  46. Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
  47. drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
  48. }
  49. /// Show in a window
  50. namedWindow( "Contours", WINDOW_AUTOSIZE );
  51. imshow( "Contours", drawing );
  52. }
  53. //#include "opencv2/imgproc.hpp"
  54. //#include "opencv2/imgcodecs.hpp"
  55. //#include "opencv2/highgui.hpp"
  56. //#include <iostream>
  57. //using namespace cv;
  58. //using std::cout;
  59. //int threshold_value = 0;
  60. //int threshold_type = 3;
  61. //int const max_value = 255;
  62. //int const max_type = 4;
  63. //int const max_binary_value = 255;
  64. //Mat src, src_gray, dst;
  65. //const char* window_name = "Threshold Demo";
  66. //const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
  67. //const char* trackbar_value = "Value";
  68. //static void Threshold_Demo( int, void* )
  69. //{
  70. // /* 0: Binary
  71. // 1: Binary Inverted
  72. // 2: Threshold Truncated
  73. // 3: Threshold to Zero
  74. // 4: Threshold to Zero Inverted
  75. // */
  76. // threshold( src_gray, dst, threshold_value, max_binary_value, threshold_type );
  77. // imshow( window_name, dst );
  78. //}
  79. //int main( int argc, char** argv )
  80. //{
  81. // String imageName("C:\\Users\\Sky\\Downloads\\photo.jpg"); // by default
  82. // if (argc > 1)
  83. // {
  84. // imageName = argv[1];
  85. // }
  86. // src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
  87. // if (src.empty())
  88. // {
  89. // cout << "Cannot read the image: " << imageName << std::endl;
  90. // return -1;
  91. // }
  92. // cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
  93. // namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
  94. // createTrackbar( trackbar_type,
  95. // window_name, &threshold_type,
  96. // max_type, Threshold_Demo ); // Create a Trackbar to choose type of Threshold
  97. // createTrackbar( trackbar_value,
  98. // window_name, &threshold_value,
  99. // max_value, Threshold_Demo ); // Create a Trackbar to choose Threshold value
  100. // Threshold_Demo( 0, 0 ); // Call the function to initialize
  101. // waitKey();
  102. // return 0;
  103. //}
  104. //#include <iostream>
  105. //#include <sstream>
  106. //#include <opencv2/imgcodecs.hpp>
  107. //#include <opencv2/imgproc.hpp>
  108. //#include <opencv2/videoio.hpp>
  109. //#include <opencv2/highgui.hpp>
  110. //#include <opencv2/video.hpp>
  111. //using namespace cv;
  112. //using namespace std;
  113. //const char* params
  114. // = "{ help h | | Print usage }"
  115. // "{ input | C:\\Users\\Sky\\Downloads\\Umka video\\Umka video\\1_light.mp4 | Path to a video or a sequence of image }"
  116. // "{ algo | MOG2 | Background subtraction method (KNN, MOG2) }";
  117. //int main(int argc, char* argv[])
  118. //{
  119. // CommandLineParser parser(argc, argv, params);
  120. // parser.about( "This program shows how to use background subtraction methods provided by "
  121. // " OpenCV. You can process both videos and images.\n" );
  122. // if (parser.has("help"))
  123. // {
  124. // //print help information
  125. // parser.printMessage();
  126. // }
  127. // //create Background Subtractor objects
  128. // Ptr<BackgroundSubtractor> pBackSub;
  129. // if (parser.get<String>("algo") == "MOG2")
  130. // pBackSub = createBackgroundSubtractorMOG2();
  131. // else
  132. // pBackSub = createBackgroundSubtractorKNN();
  133. // VideoCapture capture( samples::findFile( parser.get<String>("input") ) );
  134. // if (!capture.isOpened()){
  135. // //error in opening the video input
  136. // cerr << "Unable to open: " << parser.get<String>("input") << endl;
  137. // return 0;
  138. // }
  139. // Mat frame, fgMask;
  140. // while (true) {
  141. // capture >> frame;
  142. // if (frame.empty())
  143. // break;
  144. // //update the background model
  145. // pBackSub->apply(frame, fgMask);
  146. // //get the frame number and write it on the current frame
  147. // rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
  148. // cv::Scalar(255,255,255), -1);
  149. // stringstream ss;
  150. // ss << capture.get(CAP_PROP_POS_FRAMES);
  151. // string frameNumberString = ss.str();
  152. // putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
  153. // FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
  154. // //show the current frame and the fg masks
  155. // imshow("Frame", frame);
  156. // imshow("FG Mask", fgMask);
  157. // //get the input from the keyboard
  158. // int keyboard = waitKey(30);
  159. // if (keyboard == 'q' || keyboard == 27)
  160. // break;
  161. // }
  162. // return 0;
  163. //}