diff --git a/example_12-02.cpp b/example_12-02.cpp index a2caa8d..d525328 100644 --- a/example_12-02.cpp +++ b/example_12-02.cpp @@ -12,10 +12,15 @@ using std::cout; using std::endl; using std::vector; +void help(char** argv) { + cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions" + << "\nHough Circle detect\nUsage: " << argv[0] <<" \n" + << "Example:\n" << argv[0] << " ../stuff.jpg\n" << endl; +} + int main(int argc, char** argv) { + help(argv); if (argc != 2) { - cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions" - << "\nHough Circle detect\nUsage: " << argv[0] <<" \n" << endl; return -1; } diff --git a/example_12-03.cpp b/example_12-03.cpp index 90f8fc3..912d4b0 100644 --- a/example_12-03.cpp +++ b/example_12-03.cpp @@ -11,6 +11,11 @@ using std::endl; cv::Mat img_preview; cv::Mat img; cv::Mat markers; +cv::Mat drawRect; +int x_0 = -1; +int y_0 = -1; +int x_1, y_1; +int drawr = 0; bool finished; @@ -34,18 +39,39 @@ static void onMouseClick(int event, int x, int y, int, void*) { return; } - if (event == cv::EVENT_LBUTTONDOWN) { - cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1), + if (event == cv::EVENT_LBUTTONDOWN && drawr == 0) { + if(x_0 < 0) { + x_0 = x; + y_0 = y; + cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1), 0, 0, 360, cv::GC_FGD, 3); - cv::ellipse(img_preview, cv::Point(x, y), cv::Size(1, 1), + cv::ellipse(drawRect, cv::Point(x, y), cv::Size(1, 1), 0, 0, 360, cv::Scalar(0, 0, 255), 3); + drawr = 1; + } + + cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview); + cv::imshow("image", img_preview); return; } - - if (event == cv::EVENT_RBUTTONDOWN) { + if( event == cv::EVENT_LBUTTONUP) { + drawr = 2; + } + if(drawr == 1) { //Just moving + drawRect.setTo(0); + cv::rectangle(drawRect, cv::Point(x_0,y_0), cv::Point(x,y), cv::Scalar(0,0,255), -1); + + cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview); + x_1 = x; y_1 = y; + cv::imshow("image", img_preview); + return; + } + + if (drawr == 2) { cv::Mat bg; cv::Mat fg; + cv::rectangle(markers, cv::Point(x_0,y_0), cv::Point(x_1,y_1), cv::GC_PR_FGD, -1); cv::grabCut(img, markers, cv::Rect(0, 0, img.cols - 1, img.rows - 1), bg, fg, 5, cv::GC_EVAL); displayResult(); @@ -53,14 +79,20 @@ static void onMouseClick(int event, int x, int y, int, void*) { } } -int main(int argc, char** argv) { - if (argc != 2) { +void help(char** argv) { cout << "\nExample 12-3. Using GrabCut for background removal" - << "\n- Use left click on the image to select foreground point" - << "\n- Use right clock on the image to perform GrabCut" + << "\n- Use left mouse to drag a rectangle over the object" + << "\n- On release of left mouse button, we will perform GrabCut" << "\n- Press any key to terminate program" << "\nUsage: " - << argv[0] << " \n" << endl; + << argv[0] << " \n" + << "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl; +} + + +int main(int argc, char** argv) { + help(argv); + if (argc != 2) { return -1; } @@ -74,6 +106,7 @@ int main(int argc, char** argv) { markers.setTo(cv::GC_PR_BGD); img_preview = img.clone(); + drawRect = img.clone(); finished = false; diff --git a/example_12-04.cpp b/example_12-04.cpp index cb1ff2b..6f0acb0 100644 --- a/example_12-04.cpp +++ b/example_12-04.cpp @@ -90,15 +90,20 @@ static void onMouseClick(int event, int x, int y, int, void*) { } } -int main(int argc, char** argv) { - if (argc != 2) { - cout << "\nExample 12-4. Using Watershed for image segmentation" +void help(char** argv) { + cout << "\nExample 12-4. Using Watershed for image segmentation" << "\n- Use left click on the image to place marker for the new segment" << "\n- Use right clock on the image to perform Watershed" << "\n- Press any key to terminate program" << "\nUsage: " - << argv[0] << " \n" << endl; - return -1; + << argv[0] << " \n" + << "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl; +} + +int main(int argc, char** argv) { + help(argv); + if (argc != 2) { + return -1; } img = cv::imread(std::string(argv[1]), CV_LOAD_IMAGE_COLOR);