|
@@ -0,0 +1,280 @@
|
|
|
|
+#include "opencv2/highgui/highgui.hpp"
|
|
|
|
+#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
+#include "opencv2/imgcodecs.hpp"
|
|
|
|
+#include <iostream>
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+
|
|
|
|
+using namespace cv;
|
|
|
|
+using namespace std;
|
|
|
|
+
|
|
|
|
+Mat src; Mat src_gray;
|
|
|
|
+int thresh = 100;
|
|
|
|
+int max_thresh = 255;
|
|
|
|
+RNG rng(12345);
|
|
|
|
+
|
|
|
|
+/// Function header
|
|
|
|
+void thresh_callback(int, void* );
|
|
|
|
+
|
|
|
|
+/** @function main */
|
|
|
|
+int main( int argc, char** argv )
|
|
|
|
+{
|
|
|
|
+ /// Load source image and convert it to gray
|
|
|
|
+ src = imread( "C:\\Users\\Sky\\Downloads\\photo.jpg", IMREAD_COLOR );
|
|
|
|
+
|
|
|
|
+ /// Convert image to gray and blur it
|
|
|
|
+ cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
|
|
|
+ blur( src_gray, src_gray, Size(3,3) );
|
|
|
|
+
|
|
|
|
+ /// Create Window
|
|
|
|
+ char* source_window = "Source";
|
|
|
|
+ namedWindow( source_window, WINDOW_AUTOSIZE );
|
|
|
|
+ imshow( source_window, src );
|
|
|
|
+
|
|
|
|
+ createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
|
|
|
|
+ thresh_callback( 0, 0 );
|
|
|
|
+
|
|
|
|
+ waitKey(0);
|
|
|
|
+ return(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** @function thresh_callback */
|
|
|
|
+void thresh_callback(int, void* )
|
|
|
|
+{
|
|
|
|
+ Mat canny_output;
|
|
|
|
+ vector<vector<Point> > contours;
|
|
|
|
+ vector<Vec4i> hierarchy;
|
|
|
|
+
|
|
|
|
+ /// Detect edges using canny
|
|
|
|
+ Canny( src_gray, canny_output, thresh, thresh*2, 3 );
|
|
|
|
+ /// Find contours
|
|
|
|
+ findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
|
|
|
+
|
|
|
|
+ /// Draw contours
|
|
|
|
+ Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
|
|
|
+ for( int i = 0; i < contours.size(); i++ )
|
|
|
|
+ {
|
|
|
|
+ Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
|
|
|
+ drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Show in a window
|
|
|
|
+ namedWindow( "Contours", WINDOW_AUTOSIZE );
|
|
|
|
+ imshow( "Contours", drawing );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//#include "opencv2/imgproc.hpp"
|
|
|
|
+//#include "opencv2/imgcodecs.hpp"
|
|
|
|
+//#include "opencv2/highgui.hpp"
|
|
|
|
+//#include <iostream>
|
|
|
|
+//using namespace cv;
|
|
|
|
+//using std::cout;
|
|
|
|
+//int threshold_value = 0;
|
|
|
|
+//int threshold_type = 3;
|
|
|
|
+//int const max_value = 255;
|
|
|
|
+//int const max_type = 4;
|
|
|
|
+//int const max_binary_value = 255;
|
|
|
|
+//Mat src, src_gray, dst;
|
|
|
|
+//const char* window_name = "Threshold Demo";
|
|
|
|
+//const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
|
|
|
|
+//const char* trackbar_value = "Value";
|
|
|
|
+//static void Threshold_Demo( int, void* )
|
|
|
|
+//{
|
|
|
|
+// /* 0: Binary
|
|
|
|
+// 1: Binary Inverted
|
|
|
|
+// 2: Threshold Truncated
|
|
|
|
+// 3: Threshold to Zero
|
|
|
|
+// 4: Threshold to Zero Inverted
|
|
|
|
+// */
|
|
|
|
+// threshold( src_gray, dst, threshold_value, max_binary_value, threshold_type );
|
|
|
|
+// imshow( window_name, dst );
|
|
|
|
+//}
|
|
|
|
+//int main( int argc, char** argv )
|
|
|
|
+//{
|
|
|
|
+// String imageName("C:\\Users\\Sky\\Downloads\\photo.jpg"); // by default
|
|
|
|
+// if (argc > 1)
|
|
|
|
+// {
|
|
|
|
+// imageName = argv[1];
|
|
|
|
+// }
|
|
|
|
+// src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
|
|
|
|
+// if (src.empty())
|
|
|
|
+// {
|
|
|
|
+// cout << "Cannot read the image: " << imageName << std::endl;
|
|
|
|
+// return -1;
|
|
|
|
+// }
|
|
|
|
+// cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
|
|
|
|
+// namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
|
|
|
|
+// createTrackbar( trackbar_type,
|
|
|
|
+// window_name, &threshold_type,
|
|
|
|
+// max_type, Threshold_Demo ); // Create a Trackbar to choose type of Threshold
|
|
|
|
+// createTrackbar( trackbar_value,
|
|
|
|
+// window_name, &threshold_value,
|
|
|
|
+// max_value, Threshold_Demo ); // Create a Trackbar to choose Threshold value
|
|
|
|
+// Threshold_Demo( 0, 0 ); // Call the function to initialize
|
|
|
|
+// waitKey();
|
|
|
|
+// return 0;
|
|
|
|
+//}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//#include <iostream>
|
|
|
|
+//#include <sstream>
|
|
|
|
+//#include <opencv2/imgcodecs.hpp>
|
|
|
|
+//#include <opencv2/imgproc.hpp>
|
|
|
|
+//#include <opencv2/videoio.hpp>
|
|
|
|
+//#include <opencv2/highgui.hpp>
|
|
|
|
+//#include <opencv2/video.hpp>
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//using namespace cv;
|
|
|
|
+//using namespace std;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//const char* params
|
|
|
|
+// = "{ help h | | Print usage }"
|
|
|
|
+// "{ input | C:\\Users\\Sky\\Downloads\\Umka video\\Umka video\\1_light.mp4 | Path to a video or a sequence of image }"
|
|
|
|
+// "{ algo | MOG2 | Background subtraction method (KNN, MOG2) }";
|
|
|
|
+//int main(int argc, char* argv[])
|
|
|
|
+//{
|
|
|
|
+// CommandLineParser parser(argc, argv, params);
|
|
|
|
+// parser.about( "This program shows how to use background subtraction methods provided by "
|
|
|
|
+// " OpenCV. You can process both videos and images.\n" );
|
|
|
|
+// if (parser.has("help"))
|
|
|
|
+// {
|
|
|
|
+// //print help information
|
|
|
|
+// parser.printMessage();
|
|
|
|
+// }
|
|
|
|
+// //create Background Subtractor objects
|
|
|
|
+// Ptr<BackgroundSubtractor> pBackSub;
|
|
|
|
+// if (parser.get<String>("algo") == "MOG2")
|
|
|
|
+// pBackSub = createBackgroundSubtractorMOG2();
|
|
|
|
+// else
|
|
|
|
+// pBackSub = createBackgroundSubtractorKNN();
|
|
|
|
+// VideoCapture capture( samples::findFile( parser.get<String>("input") ) );
|
|
|
|
+// if (!capture.isOpened()){
|
|
|
|
+// //error in opening the video input
|
|
|
|
+// cerr << "Unable to open: " << parser.get<String>("input") << endl;
|
|
|
|
+// return 0;
|
|
|
|
+// }
|
|
|
|
+// Mat frame, fgMask;
|
|
|
|
+// while (true) {
|
|
|
|
+// capture >> frame;
|
|
|
|
+// if (frame.empty())
|
|
|
|
+// break;
|
|
|
|
+// //update the background model
|
|
|
|
+// pBackSub->apply(frame, fgMask);
|
|
|
|
+// //get the frame number and write it on the current frame
|
|
|
|
+// rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
|
|
|
+// cv::Scalar(255,255,255), -1);
|
|
|
|
+// stringstream ss;
|
|
|
|
+// ss << capture.get(CAP_PROP_POS_FRAMES);
|
|
|
|
+// string frameNumberString = ss.str();
|
|
|
|
+// putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
|
|
|
|
+// FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
|
|
|
|
+// //show the current frame and the fg masks
|
|
|
|
+// imshow("Frame", frame);
|
|
|
|
+// imshow("FG Mask", fgMask);
|
|
|
|
+// //get the input from the keyboard
|
|
|
|
+// int keyboard = waitKey(30);
|
|
|
|
+// if (keyboard == 'q' || keyboard == 27)
|
|
|
|
+// break;
|
|
|
|
+// }
|
|
|
|
+// return 0;
|
|
|
|
+//}
|
|
|
|
+
|