Example 4-1 ...... Example 4-2 draw a rectangle with the mouse in the window

Source: Internet
Author: User

// Example 4-1 draw a rectangle with the mouse in the window </P> <p> # include "stdafx. H "<br/> # include <cv. h> <br/> # include <pighgui. h> <br/> # include <stdlib. h> </P> <p> void my_mouse_callback (INT event, int X, int y, int flag, void * PARAM); <br/> cvrect box; <br/> bool drawing_box = false; </P> <p> void draw_box (iplimage * IMG, cvrect rect) <br/>{< br/> cvrectangle (IMG, cvpoint (box. x, box. y), cvpoint (box. X + box. width, box. Y + box. height), cvscalar (0xff, 0x00,0x00); <br/>}< br/> int main (INT argc, char ** argv) <br/> {<br/> box = cvrect (-1,-1200,700, 0); <br/> iplimage * imge = cvcreateimage (cvsize (), ipl_depth_8u, 3); <br/> cvzero (imge); <br/> iplimage * temp = cvcloneimage (imge); <br/> cvnamedwindow ("box example "); <br/> cvsetmousecallback ("box example", my_mouse_callback, (void *) imge); <br/> while (1) <br/>{< br/> cvcopyimage (imge, temp); <br/> If (drawing_box) <br/>{< br/> draw_box (temp, box ); <br/>}< br/> cvshowimage ("box example", temp); <br/> If (cvwaitkey (15) = 27) <br/>{< br/> break; <br/>}</P> <p> cvreleaseimage (& imge ); <br/> cvreleaseimage (& temp); <br/> cvdestroywindow ("box example"); </P> <p> // char jieshu; <br/> // scanf (& jieshu); <br/> return 0; <br/>}</P> <p> void my_mouse_callback (INT event, int X, int y, int flag, void * PARAM) <br/>{< br/> iplimage * image = (iplimage *) Param; <br/> switch (Event) <br/>{< br/> case cv_event_mousemove: <br/>{< br/> If (drawing_box) <br/>{< br/> box. width = x-box.x; <br/> box. height = y-box.y; <br/>}< br/> break; <br/> case cv_event_lbuttondown: <br/>{< br/> drawing_box = true; <br/> box = cvrect (X, Y, 0, 0); <br/>}< br/> break; <br/> case cv_event_lbuttonup: <br/>{< br/> drawing_box = false; <br/> If (box. width <0) <br/>{< br/> box. X + = Box. width; <br/> box. width * =-1; <br/>}< br/> If (box. height <0) <br/>{< br/> box. Y + = Box. height; <br/> box. height * =-1; <br/>}< br/> draw_box (image, box); <br/>}< br/> break; <br/>}</P> <p> // Example 4-2 use a slider to implement the switch </P> <p> # include "stdafx. H "<br/> # include <cv. h> <br/> # include <pighgui. h> </P> <p> int g_switch_value = 0; </P> <p> void switch_callback (INT position) <br/>{< br/> If (position = 0) <br/>{< br/> switch_off_function (); <br/>}< br/> else <br/> {<br/> switch_on_function (); <br/>}</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> cvnamedwindow ("demo window", 1); <br/> cvcreatetrackbar ("Switch", "demo window", & g_switch_value, 1, switch_callback (); <br/> while (1) <br/> {<br/> If (cvwaitkey (15) = 27) <br/>{< br/> break; <br/>}< br/> return 0; <br/>}</P> <p>

 

Notes:

  • Analyze

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.