You can download the source code here:
Http://download.csdn.net/source/1779188
P.s.: The function of playing a video and capturing a camera screen is added later. The function of playing a video is only 'play' and 'stop', and cannot be paused, A step-by-step guide to the use of Microsoft Visual C ++ and the intel opencv library seems to be related to multithreading to prevent mutex, after learning and understanding this part, try to pause the function and other more complex applications.
The following is the project file after the new feature is added (which can be run directly ):
Http://download.csdn.net/source/1838608
In section 2.8 of opencv tutorial-basics, the created MFC image is displayed directly in the upper left corner of the dialog box panel, which is not very beautiful; the Quick Application of opencv in MFC introduces the use of SDI (Single Document Interface) to display images, A step-by-step guide to the use of Microsoft Visual C ++ and the intel opencv library use vs2005 to read and process images and videos, however, its image and video display interface is not in the dialog box, but a new window is created. So let's take a look at how to use the picture control in the dialog box to display and process images.
1. Create an MFC
First, create an MFC Dialog Box Application (dialog-based application) as follows:
In VS2005 and 2008, we can use a Solution to combine several projects (each Project basically contains only one Program ), when we want to build a multi-Program application (for example, a client Program and a server application), we can use Solution to combine these Projects and share files and libraries. Generally, you need to create a primary path for Solution, which contains the paths of all Projects. However, in this article, we only build a simple Project. Therefore, you do not need to check the "Create directory for solution" option in the Create New Project dialog box of MFC.
Click OK -- Next to go to the Next step. Here we create a Dialog-based Application. Most options can be set by default, but the following "Use Unicode libraries" is best removed. If this option is selected, the program code uses the 16-bit Unicode Character Set for encoding, but many functions use char * (ASCII stings) characters, it is very troublesome to convert a string from Unicode to ASCII. Unicode compilation may encounter the following errors:
Cannot convert parameter 1 from 'cstring' to 'const char *'
Cannot convert from 'const char [11] 'to 'lpcwstr', which means there is a problem in the conversion of Unicode and Multi-byte strings. In the previous study note, we mentioned that "the input parameter type of the member function LoadBMP should be const char *", which should be just a standard method. Here we remove the "Use Unicode libraries" option, the root cause.
You can perform subsequent settings based on your needs. My settings are as follows:
2. write code
Choose Resource View> mymfc (project name)> mymfc. rc-> Dialog double-click IDD_MYMFC_DIALOG to view an initial GUI. Add two buttons and one Picture control to it, as shown below:
Select a single control and right-click Properties. You can see the ID of the control. You can edit this number by yourself. For example, the ID of the Picture control is set to IDC_ShowImg, this ID number is used in the image display function.
First, load the lib file in the Project Properties: menu Project-> Properties-> Configuration Properties-> Linker-> Input-> additional dependencies to add cxcore200.lib cv200.lib highgui200.lib.
Then add the following code under # include "resource. h" of mymfc. h:
# Include "cv. h"
# Include "highgui. h"
# Define IMAGE_WIDTH 256
# Define IMAGE_HEIGHT 256
# Define IMAGE_CHANNELS 3 right-click CmymfcDlg on the Class View panel, select Add-> Add Variable, and Add a IplImage * type Variable TheImage. Then click CmymfcDlg, double-click OnInitDialog in the list in the following window and Add TheImage initialization code under "// TODO: Add extra initialization here:
CvSize ImgSize;
ImgSize. height = IMAGE_HEIGHT;
ImgSize. width = IMAGE_WIDTH;
TheImage = cvCreateImage (ImgSize, IPL_DEPTH_8U, IMAGE_CHANNELS); then double-click OnPaint in if (IsIconic ())... Add the following code to the else to redraw the window:
CDialog: OnPaint (); // redraw dialog box
CDialog: UpdateWindow (); // update the windows window. If you do not call this operation, the image is displayed with a problem.
ShowImage (TheImage, IDC_ShowImg); // The redrawing function is then used to double-click InitInstance in the member list under CmymfcApp, and in the two "// TODO: place code here to handle when the dialog is ..." Add the following:
CvReleaseImage (& dlg. TheImage); that is, when you press "OK" or "Cancel", the memory occupied by TheImage is released.
The next step is to write the function for reading and processing images.
Return to the GUI editing interface of mymfc, right-click the ReadImg button, select Add Event Handler, and create the message Response Program clicked by the button:
The handle name is set to OnBnClickedReadimg. The main response operations include selecting an image file in the pop-up dialog box, reading an image file, uniformly scaling the image to 256*256, and displaying the image. The Code is as follows:
// TODO: Add your control notification handler code here
CFileDialog dlg (
TRUE, _ T ("*. bmp"), NULL,
OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
_ T ("image files (*. bmp ;*. jpg) | *. bmp ;*. jpg | All Files (*. *) | *. * | "), NULL
); // Option image conventions
Dlg. m_ofn.lpstrTitle = _ T ("Open Image"); // name of the title in the Open file dialog box
If (dlg. DoModal ()! = IDOK) // determine whether to obtain the image
Return;
CString mPath = dlg. GetPathName (); // obtain the image path
IplImage * ipl = cvLoadImage (mPath, 1); // read the image and cache it to a local variable ipl
If (! Ipl) // determine whether the image is loaded successfully
Return;
If (TheImage) // clears the data of the previous image.
CvZero (TheImage );
ResizeImage (ipl); // scale the image to make it equal to 256 in width or height, and then copy it to TheImage.
ShowImage (TheImage, IDC_ShowImg); // call the image display function
Cvreleaseimage (& IPL); // release the memory occupied by IPL, which contains two new member functions: resizeimage and showimage. The former function is to zoom in and out the images of different sizes, then, the image is saved to TheImage of 256*256 by setting the ROI. The latter shows the image theimage to the center of the idc_shouimg window of the Image Display Control. To implement these two functions, right-click cmymfcdlg on the Class View panel, select Add-> Add function, and create two functions: void showimage (iplimage * IMG, uint ID) and void resizeimage (iplimage * IMG ). The implementation code of these two functions is as follows:
Void cmymfcdlg: resizeimage (iplimage * IMG)
{
// Read the width and height of the image
Int W = IMG-> width;
Int H = IMG-> height;
// Find out who is better at width and High School
Int max = (W> H )? W: h;
// Calculate the proportional factor required to scale the image to the theimage area.
Float scale = (float) max/256.0f );
// The width and height of the scaled Image
Int nw = (int) (w/scale );
Int nh = (int) (h/scale );
// To save the scaled image to the center of TheImage, calculate the expected coordinate value of the image in the upper left corner of TheImage.
Int tlx = (nw> nh )? 0: (int) (256-nw)/2;
Int tly = (nw> nh )? (Int) (256-nh)/2: 0;
// Set the ROI of TheImage to save the image to img.
CvSetImageROI (TheImage, cvRect (tlx, tly, nw, nh ));
// Scale the image img and add it to TheImage
CvResize (img, TheImage );
// Reset TheImage's ROI to read the next image
CvResetImageROI (TheImage );
}
Void CmymfcDlg: ShowImage (IplImage * img, uint id) // ID is the ID of the Picture Control.
{
CDC * pDC = GetDlgItem (ID)-> GetDC (); // obtain the DC of the display control
HDC hDC = pDC-> GetSafeHdc (); // obtain the HDC (device handle) for plotting
CRect rect;
GetDlgItem (ID)-> GetClientRect (& rect );
Int rw = rect. right-rect. left; // obtain the width and height of the image control.
Int rh = rect. bottom-rect. top;
Int iw = img-> width; // read the width and height of the image.
Int ih = img-> height;
Int tx = (int) (rw-iw)/2; // display the image right in the center of the control.
Int ty = (int) (rh-ih)/2;
SetRect (rect, tx, ty, tx + iw, ty + ih );
CvvImage cimg;
Cimg. CopyOf (img); // copy the image
Cimg. DrawToHDC (hDC, & rect); // draws an image to a specified area of the display control.
ReleaseDC (pDC );
} The ResizeImage function was modified based on the cvShowMultiImages function used to display multiple images in a Single Window in learning notes (5, the function ShowImage is based on the Code in the post "how to display the image to the picture control of MFC". The following posts can also be referred:
1. MFC picture control questions
2. MFC picture control (hide) text display
3. Problems with MFC displaying images (jpg) in Picture Control
4. How does vc display jpg, jif, and bmp images in picture control?
5. Use Picture Control to display BMP Images
Finally, we need to perform a simple "kan" edge processing on the read image. Therefore, we need to create a button "EdgeDetect" and the corresponding response code is as follows:
Void CmymfcDlg: OnBnClickedEdgedetect ()
{
// TODO: Add your control notification handler code here
IplImage * gray = 0, * edge = 0;
Gray = cvCreateImage (cvSize (IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
Edge = cvCreateImage (cvSize (IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
Cvcvtcolor (theimage, gray, cv_bgr2gray );
Cvcanny (Gray, edge, 30,100, 3 );
Cvcvtcolor (edge, theimage, cv_gray2bgr );
Showimage (theimage, idc_showimg); // call the image display function
Cvreleaseimage (& gray );
Cvreleaseimage (& edge );
} Here I mainly refer to the p33 code in opencv tutorial-basics, but I didn't create a new myiplclass class as I did in the book to read and process images. I think it is not appropriate to directly process theimage in this article. It is safer to create a class as per the tutorial. This involves the process of returning the object instance by function, deep copy and shortest copy, copy structure, use of dynamic memory, and the code of opencv related interfaces. Further study and understanding will be required in the future!
Finally, you can modify the product version, name, and other information of the program in resource view Panel> mymfc (project name)> mymfc. rc> version.
To see the effect after the program is generated:
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chenyusiyuan/archive/2009/10/29/4744097.aspx