Based on optics, the image measurement technology, which integrates optoelectronics, computer technology, laser technology, image processing technology and so on, has formed a new measurement technology in the field of measurement, and the image measurement system based on digital image processing technology has been widely used in the measurement of geometrical quantity, aviation and other remote sensing, Micro-dimension measurement and appearance measurement of precision complex parts, as well as optical interference mapping, stress-strain field state distribution and image-related technical fields. In the image measurement system based on digital image processing technology, the problem that must be solved is the image acquisition, i.e. the acquisition of image data and the acquisition of image data for later image processing.
There are generally two ways to capture video images, one is the use of video capture card with the SDK development tool, this capture method is related to the device, depending on the video capture card and camera type, is not conducive to flexible applications; another way to capture this is Microsoft Visual C + + since 4. Version 0 begins to support videos for Windows (VFW), which makes video capture programming much easier, using VFW technology to increase the flexibility of video capture and reduce reliance on video devices. In vc++6.0, components such as Mciavi, Drawdib, Avifile, and AVICap are included. Through the coordination between them, can complete playback, editing, file management and video capture functions, for video image processing and analysis to bring very great convenience, this article on the use of VFW for real-time video data acquisition of some of the practical problems encountered in the discussion.
Introduction to VFW library functions
The real-time acquisition of video data is mainly through the call AVICap32.dll to create AVICap window class, which is completed by message, macro function, structure and callback function in AVICap window class. AVICap has an obvious advantage in capturing video, it can directly access the video buffer, does not need to generate intermediate files, high real-time, it can also save digital video to the previously built files. The practical application shows that this method can improve the efficiency of video capture and program operation, reduce the dependence on hardware, and improve the compatibility and portability of the program.
VFW's video capture function mainly includes capturing video stream to AVI file (capcapturesequence), capturing video stream to cache (Capcapturesequencenofile), capturing video stream to AVI file ( capcapturesingleframe), local preview (Cappreview/capoverlay), and capture single frame preview (capgrabframe/capgrabframenostop). VFW also provides callback functions that allow applications to accurately control the capture of video streams, detect errors, monitor state changes, and process real-time data while capturing gaps in two-frame data and capturing new frames per capture.
a discussion on several practical problems
1, callback function to deal with the problem
Callback functions are one of the most useful programming mechanisms so far. In Windows, callback functions are required by window procedures, hook procedures, and asynchronous procedure calls, using the callback method throughout the callback process. People can register callback methods to get load/unload notifications, unhandled exception notifications, database/window state modification notifications, file system modification notifications, menu item selections, completed asynchronous operation notifications, filter a set of entries, and so on. There are several such macro functions in the VFW, such as macro functions that are used to set up callback functions that respond after an event occurs. It is similar to the interrupt service mechanism, the condition is satisfied, the program will automatically enter the corresponding callback function body, the function of what exactly to do, all by the developer with its parameters of their own programming to determine. Using VFW to obtain real-time video data can usually use the callback mechanism of video processing (call-backmechanism) to get the first address and length of the real-time data buffer and to process the image data, and also can carry on the direct transmission of the video data, in this aspect many articles have made the concrete introduction. However, according to most of the articles, in the specific application process, when the callback function is defined as follows, the program is always unable to compile:
{ Lresult CALLBACK Framecallbackproc (HWND ghwnd,lpvideohdr lpvdata) unsigned char *data; data=lpvdata->lpdata;//gets the video data's first address and stores it in the data array for processing } |
Through the study, found that the root cause is the callback function is based on C programming Windows SDK Technology, not for C + +, can be a C function directly as a callback function, but if you try to directly use C + + member functions as a callback function will have errors, or even compile can not pass. The error is that ordinary C + + member functions implicitly have a pass-through function as an argument, that is, the "This" pointer, which enables a program function to access data members of C + + by passing a pointer to itself to its member function. This also makes sense of why multiple instances of a C + + class can share member functions but do have different data members. Because the this pointer is used to install a member function of type callback as a callback function, a callback function installation fails because the implied this pointer causes the number of function parameters to not match. The key to solving this problem is to not allow this pointer to work, and you can solve the problem of using callback functions in C + + by using the following two typical techniques. This method is versatile and suitable for any C + +.
(1) Do not use the member function, the direct use of ordinary C functions, in order to achieve in the C function can access the class member variable, you can make the Ufida operator (friend), in C + + the C function as a friend of the class can be. This processing mechanism is the same as using callback functions in normal C programming.
(2) using a static member function, a static member function does not use the this pointer as a suppressed parameter, so that it can be used as a callback function. Static member functions have two main features: first, they can be used without class instances; second, static member variables and static member functions are accessible, and non-static member variables and non-static member functions are inaccessible. Because using a class member function as a callback function in C + + is intended to access all member variables and member functions, it would not be practical to do so. The solution is also simple: to use a static class pointer as a class member, you can access all member variables and member functions by initializing the static pointer when the class is created, such as Pthis=this, and then through the static pointer in the callback function. This approach applies to cases where there is only one instance of a class, because more than one class instance will share static class members and static member functions, causing the static pointer to point to the class instance that was last created. To avoid this situation, you can use a parameter of the callback function to pass the this pointer, which enables data member sharing. This method is a little troublesome, and there is no more to repeat.
Therefore, you can define the callback function as follows:
Static Lresult CALLBACK Framecallbackproc (HWND ghwnd,lpvideohdr lpvdata) { unsigned char *data; data=lpvdata->lpdata;//gets the video data's first address and stores it in the data array for processing } |
2, the problem of image acquisition
The collection of video data is the key of the whole application, according to the different application can be the video frame collected files or collection of the cache directly to deal with. Using VFW to obtain real-time video data can usually use the callback mechanism of video processing (call-backmechanism) to get the first address and length of the real-time data buffer and to process the image data in real time, but the image processing program cannot be too long in this process, otherwise the video display is not fluent. In addition, in the actual acquisition process, some image acquisition card driver to the data obtained through the callback mechanism compression, such as DC10, and in the image measurement system high-precision requirements, compressed image data directly affect the future image processing work. By adopting the following two kinds of typical techniques, the image data of the video frame without compression can be collected.
(1) It is found that although the driver of the Image acquisition card compresses the data obtained by the callback mechanism, the image data in the frame image buffer is not compressed by using the capeditcopy () macro function in VFW, and the image data is not compacted. Instead of using the callback mechanism, you can capture a frame of an image directly using Capgrabframenostop (), then copy the copy of the data to the Clipboard, and then obtain the first address of the image data in memory through the DIB (Device independent Bitmap) operation. For subsequent image data processing. The specific code fragment is as follows:
Gets the image data handle capeditcopy () copied to the Clipboard, specifying the data by the Cf_dib parameter HANDLE Hdata; :: GlobalFree ((hglobal) hdata); Hdata= (HANDLE) copyhandle (:: GetClipboardData (Cf_dib)); |
(2) using Capfilesavedib to convert the image data in the buffer into a DIB bitmap, save it as a file, and then read the image data from the bitmap to the memory for subsequent processing when it needs to be processed. This way because there is a file storage and read delay, for real-time image processing, the response speed is slightly slower than the former, after many experiments proved that, as long as the image processing algorithm is not very large computational capacity, still can ensure a better real-time.
3, the problem of Image Collection window establishment
A capture window needs to be created before the video capture, and all the capture operations and their settings are based on it. A AVICap view window handle describes the details of the audio and video streams, which frees the programmer's application from AVI file format, audio Video buffer management, low-level audio video driver access, and so on. AVICap is a predefined Windows window class that uses the window class to create a child window that can be associated with a video capture device driver, which is used to display real-time video images captured by the acquisition device.
However, in the actual application process, the application may be based on a single document (SDI), Multiple documents or dialog box based interface, because of the three types of different, the capture window should be created according to specific requirements. Regardless of which type is used, the key is how to obtain the parent window handle of the Capture window, depending on the specific requirements of the display of live video. Typically, the capture window can be created in the following two ways:
(1) Gets the handle of the dynamically created parent window and dynamically creates the capture window. The specific code fragment is as follows:
CFrameWnd M_wndsource; if (!m_wndsource.createex (ws_ex_topmost,null, "source", Ws_caption,crect (100,100,150,180), null,0)) return-1; M_wndsource.showwindow (Sw_hide); M_wndcap=capcreatecapturewindow (LPSTR) "Video Capture test program", ws_child| Ws_visible,0,0,300,240,m_wndsource.m_hwnd, 0); |
(2) Get the handle of the display real-time video window and create the Capture window statically. The specific code fragment is as follows:
M_hcapwnd = Capcreatecapturewindow (LPSTR) TEXT ("Video Capture Test Program"), ws_child| Ws_visible, 0,0,768,576,this->m_hwnd,0); |
Concluding remarks
The use of VFW technology to achieve real-time video data acquisition, improve the effect of video capture and program operation efficiency, but also reduce the dependence on hardware, improve the compatibility and portability of the program. This method is used in many image measurement systems based on digital image processing technology. In this paper, the practical problems encountered in the concrete application are discussed in detail, and the concrete solutions are given.