// string imageName("d:/image/size13x13.png"); // by default #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/core/core.hpp" #include #include using namespace cv; using namespace std; /** * @function main * @brief Main function */ void cbMouse(int event, int x, int y, int flags, void*); void tb1_Callback(int value, void *); void pushbuttonCallBack(int, void *font); Mat orig_img, tmp_img; const char main_win[] = "main_win"; const char sub_win[] = "sub_win"; char msg[50]; void tb1_Callback(int value, void *) { sprintf(msg, "Trackbar 1 changed. New value=%d", value); cout << msg << endl; // displayOverlay(main_win, msg); return; } void cbMouse(int event, int x, int y, int flags, void*) { // Static vars hold values between calls static Point p1, p2; static bool p2set = false; // Left mouse button pressed if (event == EVENT_LBUTTONDOWN) { p1 = Point(x, y); // Set orig. point p2set = false; } else if (event == EVENT_MOUSEMOVE && flags == EVENT_FLAG_LBUTTON) { // Check moving mouse and left button down // Check out bounds if (x > orig_img.size().width) x = orig_img.size().width; else if (x < 0) x = 0; // Check out bounds if (y > orig_img.size().height) y = orig_img.size().height; else if (y < 0) y = 0; p2 = Point(x, y); // Set final point p2set = true; // Copy orig. to temp. image orig_img.copyTo(tmp_img); // Draws rectangle rectangle(tmp_img, p1, p2, Scalar(0, 0, 255)); // Draw temporal image with rect. imshow(main_win, tmp_img); } else if (event == EVENT_LBUTTONUP && p2set) { // Check if left button is released // and selected an area // Set subarray on orig. image // with selected rectangle Mat submat = orig_img(Rect(p1, p2)); // Here some processing for the submatrix //... // Mark the boundaries of selected rectangle rectangle(orig_img, p1, p2, Scalar(0, 0, 255), 2); imshow("main_win", orig_img); } return; } void pushbuttonCallBack(int, void *font) { // Add text to the image addText(orig_img, "Push button clicked", Point(50, 50), *((QtFont *)font)); imshow(main_win, orig_img); // Shows original image return; } int main(void) { const char track1[] = "TrackBar 1"; int tb1_value = 50; // Initial value of trackbar 1 orig_img = imread("d:/image/cameraman2.tif"); // Open and read the image if (orig_img.empty()) { cout << "Error!!! Image cannot be loaded..." << endl; return -1; } namedWindow(main_win); // Creates main window // Creates a font for adding text to the image // Creation of CallBack functions setMouseCallback(main_win, cbMouse, NULL); createTrackbar(track1, main_win, &tb1_value, 100, tb1_Callback); imshow(main_win, orig_img); // Shows original image cout << "Press any key to exit..." << endl; waitKey(); // destroyWindow("Example1"); return 0; }