From 89ec4030ba4e426e6e7992336a6665ef9d37ec48 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 30 Sep 2016 13:06:38 +0200 Subject: simple image cropping tool --- crop.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 crop.cpp diff --git a/crop.cpp b/crop.cpp new file mode 100644 index 0000000..8f570e8 --- /dev/null +++ b/crop.cpp @@ -0,0 +1,86 @@ +#include +#include + +#include "opencv2/opencv.hpp" + +using namespace std; +using namespace cv; + +Mat src, img, roi; +Rect crop_rect(0,0,0,0); +vector pt; +const char* win_name="crop"; + +void +show_img () +{ + img = src.clone(); + if(crop_rect.width > 0 && crop_rect.height > 0){ + roi = src(crop_rect); + cout << crop_rect.width << " " << crop_rect.height << endl; + imshow("cropped", roi); + } + + //rectangle(img, crop_rect, Scalar(0,255,0), 1, 8, 0 ); + imshow(win_name,img); +} + +void +mouse_handler (int event, int x, int y, int f, void*) +{ + switch (event) + { + case CV_EVENT_LBUTTONDOWN: + circle(src, Point(x,y), 4, 0); + pt.push_back(cv::Point(x,y)); + show_img(); + break; + } + + if (pt.size() == 2) { + //circle(src, Point(pt[1].x, pt[0].y), 4, 0); + //circle(src, Point(pt[0].x, pt[1].y), 4, 0); + crop_rect = Rect(pt[0], pt[1]); + show_img(); + } +} + +int +main (int argc, char** argv) +{ + namedWindow(win_name, WINDOW_NORMAL); + namedWindow("cropped", WINDOW_NORMAL); + setMouseCallback(win_name, mouse_handler, NULL); + + for (size_t i=1; i