1.1.2-learning the mixed programming of opencv and MFC-drawing tool for drawing a rectangle in a straight line

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/nuptboyzhb/3961685

Drawing tools


1. draw a straight line

Add the "line" menu item and create a Class Wizard;

Add the member variable my_draw_flag to the cxxxxxxview class and initialize it to 0 in the constructor.

Export my_draw_flag = 1 to draw a straight line in the processing function of the 'line' menu item.

Add window message processing, wm_lbuttondown, wm_mousemove, and wm_lbuttonup

Add member variables

The value is initialized to 0 in the constructor;

Added the onlbuttondownrunflag and onlbuttonuprunflag member variables of the int type. And initialize it to 0 in the constructor of the View class;

Add iplimage member variable

Ø in the onlbuttondown (uint nflags, cpoint point) function, save the point when the mouse is pressed:

My_cvpoint1 = cvpoint (point. X, point. y );

Onlbuttondownrunflag = 1;

Onlbuttonuprunflag = 0;

Bufimg = cvcloneimage (workimg); // bufimg is used to save the image after each painting.

Ø in the onmousemove (uint nflags, cpoint point) function, edit the Code as follows:

If (onlbuttondownrunflag & onlbuttonuprunflag = 0)

{

Cvpointiner_point = cvpoint (point. X, point. y );

Iplimage * SRC;

If (! Workimg)

{

Return;

}

Intthickness, colorr, colorg, colorb;

Thickness = 2; // line width

Colorr = 0;

Colorg = 255;

Colorb = 0;

Src = cvcloneimage (bufimg );

Switch (my_draw_flag)

{

Case0:

Break;

Case1:

Cvflip (SRC );

Cvline (SRC, my_cvpoint1, iner_point, cvscalar (colorr, colorg, colorb), thickness );

Cvflip (SRC );

Workimg = cvcloneimage (SRC); // display the current line

Invalidate (); // redraw

Break;

}

Cvreleaseimage (& SRC );

}

Cscrollview: onmousemove (nflags, point );

Ø message processing function by mouse

Void ccvmfcview: onlbuttonup (uintnflags, cpoint point)

Onlbuttonuprunflag = 1;

Onlbuttondownrunflag = 0;

 

 

Draw a rectangle

1. Add the menu item to draw a rectangle

2. Set the attributes as follows:

3. Create a Class Wizard and edit the message response function:

Void ccvmfcview: onmydrawrectangle ()

{

// Todo: add your command handler code here

My_draw_flag = 2;

}

4. In the mousemove message response function, add

Cvflip (SRC );

Cvrectangle (SRC, my_cvpoint1, iner_point, cvscalar (colorr, colorg, colorb), thickness );

Cvflip (SRC );

Workimg = cvcloneimage (SRC); // display the current rectangle

Invalidate (); // redraw

Circle

The procedure for adding a menu is the same as that for adding a rectangle:

The key is to write the case 3 code.

Case 3:

Int r = 0;

R = (INT) SQRT (iner_point.x-my_cvpoint1.x) * (iner_point.x-my_cvpoint1.x)

+ (Iner_point.y-my_cvpoint1.y) * (iner_point.y-my_cvpoint1.y ));

Cvflip (SRC );

Cvcircle (SRC, my_cvpoint1, R, cvscalar (colorb, colorg, colorr), thickness );

Cvflip (SRC );

Workimg = cvcloneimage (SRC); // display the current rectangle

Invalidate (); // redraw

Break;

 

 

 

 

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.