Mouse (Draw rectangle event)-Not much

Source: Internet
Author: User
Tags scalar

1#include <iostream>2#include <opencv2/opencv.hpp>3 4 #defineWindow_name "Mouse Event window"5 6 using namespacestd;7 using namespaceCV;8 9 Rect G_rectangle;Ten BOOLG_bdrawingbox =false; OneRNG G_rng (12345); A  - voidDrawRectangle (Mat &img, Rect box) - { theRectangle (img, box.tl (), box.br (), Scalar (G_rng.uniform (0,255), G_rng.uniform (0,255), G_rng.uniform (0,255))); - } -  - /*Note: You cannot write an uninitialized matrix class in the Destroy function, so you need to write a flag bit and then use it within the while (1) loop .*/ + voidOnmouse (int Event,intXintYintFlagvoid*param) - { +Mat &img = * (cv::mat*) param; A  at     Switch(Event) -     {  -         //when moving the mouse -      CaseCv_event_mousemove: -     { -                                if(G_bdrawingbox) in                                { -G_rectangle.width = x-g_rectangle.x; toG_rectangle.height = y-g_rectangle.y; +                                } -     } the          Break; *         //When you click the left mouse button $      CaseCv_event_lbuttondown:Panax Notoginseng     { -G_bdrawingbox =true; theG_rectangle = Rect (x, Y,0,0); +     } A          Break; the         //when you release the left mouse button +      CaseCv_event_lbuttonup: -     { $G_bdrawingbox =false; $  - DrawRectangle (IMG, g_rectangle); -     } the          Break; -     }Wuyi } the  - intMain () Wu { -Mat Srcimage ( -, -, CV_8UC3); About Mat tempimage; $  - Srcimage.copyto (tempimage); -  -Srcimage = Scalar::all (0); A  + Namedwindow (window_name); theSetmousecallback (Window_name, Onmouse, (void*) &srcimage); -  $      while(1) the     { the Srcimage.copyto (tempimage); the  the         if(G_bdrawingbox) - DrawRectangle (Tempimage, g_rectangle); in  the imshow (Window_name, tempimage); the  About         if(Waitkey (Ten) == -) the              Break; the  the         /*DrawRectangle (Srcimage, g_rectangle); + imshow (Window_name, srcimage); - if (waitkey () = =) the Break ;*/Bayi     } the  the     return 0; -}

Mouse (Draw rectangle event)-not very good

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.