Configuration and programming example for the first time using opencv

Source: Internet
Author: User

Recently, I started to develop a multi-touch project. After reading a lot of information on the Internet, I found that some of my predecessors have written the core functions and established a library called touchlib. Touchlib library functions ultimately call functions in opencv. in order to learn from touchlib's mature algorithms in their own project solutions, it is necessary to be familiar with touchlib. Therefore, we are bound to understand the use of opencv in VC. I downloaded opencv 1.0 from the Internet and installed it in the "J: \ Program Files \ opencv \" directory. Before using VC 6.0 to call opencv, you need to make the following Configuration:
(1) run VC 6.0 and open opencv under the opencv/_ make directory. select the DSW file and click 'build (build) '-> 'batch build (batch build)' (compile for about a few minutes ).
(2) Click 'tools'-> 'options'-> 'directory (directories) tab' and add the following under include files:
J: \ Program Files \ opencv \ CV \ include
J: \ Program Files \ opencv \ otherlibs \ highgui
J: \ Program Files \ opencv \ cxcore \ include
J: \ Program Files \ opencv \ otherlibs \ cvcam \ include
(3) Add under library files
J: \ Program Files \ opencv \ Lib
(4) Add under source files
J: \ Program Files \ opencv \ CV \ SRC
J: \ Program Files \ opencv \ cxcore \ SRC
J: \ Program Files \ opencv \ cvaux \ SRC
J: \ Program Files \ opencv \ otherlibs \ highgui
J: \ Program Files \ opencv \ otherlibs \ _ graphics \ SRC
(5) | * keep in mind * |: before each project is created, choose Project> Settings> connection) in the '->' object/Library module (Object/library modules) 'tab, add:
Cv. Lib highgui. Lib cxcore. Lib cvcam. Lib

The following is an example of a simple application of opencv.
(1) create a dialog-based MFC project opencv_dlg1, and add the "# include <cv. h>" and "# include (2) Add the picture control with the id_show_window ID. The image is displayed in the control.
(3) Add the member variable iplimage * m_cvimage to the copencv_dlg1dlg class and set the attribute to public. In the constructor copencv_dlg1dlg: copencv_dlg1dlg (), initialize it as null, that is, add the "m_cvimage = NULL;" statement.
(4) opencv has a ready-made function cvshowimage () to display images. However, this function is used to display images in another pop-up window, to display images in the created picture control, you must add the member function void drawpictohdc (iplimage * IMG, uint ID) to the copencv_dlg1dlg class and set the attribute to public. The code of the drawpictohdc () function is:
//----------------------------------------------------------------------------
Void copencv_dlg1dlg: drawpictohdc (iplimage * IMG, uint ID)
{
CDC * PDC = getdlgitem (ID)-> getdc ();
HDC = PDC-> getsafehdc ();
Crect rect;
Cwnd * pwnd;
Pwnd = getdlgitem (ID );
Pwnd-> setwindowpos (null, 0, 0, IMG-> width, IMG-> height, swp_nozorder | swp_nomove );
Pwnd-> getclientrect (& rect );
Cvvimage cimg;
Cimg. copyof (IMG );
Cimg. drawtohdc (HDC, & rect );
Releasedc (PDC );
}
// ================================================ ====
(5) Add the button. The ID is idc_read_pic and the title is "read image". This button is used to open an image. Add the message response function onreadpic () to this button ():
//----------------------------------------------------------------------------
Void copencv_dlg1dlg: onreadpic ()
{
// Todo: add your control notification handler code here
Cstring open_filename;
Cfiledialog fdlg (true, "BMP", "*. BMP", ofn_hidereadonly | ofn_overwriteprompt,
"Original image (*. BMP) | *. BMP | all files (*. *) | *. * |", null );
If (fdlg. domodal () = true)
{
Open_filename = fdlg. getfilename ();
If (m_cvimage)
Cvreleaseimage (& m_cvimage );
M_cvimage = cvloadimage (open_filename, 1 );

If (m_cvimage = 0)
MessageBox ("reading failed ");
Else
Drawpictohdc (m_cvimage, idc_show_window );
}
}
// ================================================ =====

Now, run the program. When you click "read image", a dialog box is displayed, prompting you to select the image to be read. After "OK", the program displays the image in the picture control.
(6) Add the Edit Control with the idc_edit_th ID. Use the wizard tool MFC classwizard to add the INT-type member variable m_th to the ID. The idc_edit_th control allows you to enter the image segmentation threshold and the program automatically updates it to m_th.
(7) Add the button. The ID is idc_threshold and the title is "threshold segmentation". Click it to perform binarization segmentation on the opened images. Add the message response function onthreshold () to this button ():
//----------------------------------------------------------------------------
Void copencv_dlg1dlg: onthreshold ()
{
// Todo: add your control notification handler code here
Updatedata (true );
If (m_cvimage = NULL)
MessageBox ("no source image found! "," Warning ");
Else
{
Iplimage * DST = cvcreateimage (cvsize (m_cvimage-> width, m_cvimage-> height), m_cvimage-> depth, m_cvimage-> nchannels );
Cvthreshold (m_cvimage, DST, m_th, 255, cv_thresh_binary );
// Prototype: void cvthreshold (iplimage * SRC, iplimage * DST, float Thresh, float maxvalue, cvthreshtype type );
Cvnamedwindow ("threshold"); // you can specify the window name.
Cvshowimage ("threshold", DST); // display the image
Cvwaitkey (0 );
}
}
// ================================================ ======
Now, when you click the "read image" button to open an image, it will be displayed in the picture control. In the Edit Control idc_edit_th, enter the threshold value and click the "Threshold Value Segmentation" button, perform binarization on the read image. The processing result is displayed in a new pop-up window.

The above program is a pilot program, which can reflect the general steps of opencv programming. However, this program still has a bug: that is, the image displayed in the picture control will not be re-painted, that is, if you use another window to block the image in the picture control, when the window is moved, the blocked part of the image is blank. Further program error is required.

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.