Writing Video Applications: Dr. Lisa Spencer Last Updated 6/23/06
Writing Video Applications: Dr. Lisa Spencer Last Updated 6/23/06
Writing Video Applications: Dr. Lisa Spencer Last Updated 6/23/06
DirectShow
Component of Direct X Preferred method for new development
DirectX Components
Graphics: Direct3D Audio: DirectSound and DirectMusic Joysticks and force-feedback: DirectInput Networked games: DirectPlay Multimedia: DirectShow DirectSetup
Basic DirectShow
The minimum you need to know to use DirectShow
Getting DirectX
Uninstall any previous DX SDK versions Download the DirectX 9 Software Development kit
Go to http://msdn.microsoft.com/directx/directxdownloads.
If you are using VC++6.0, dig to find the Summer 2003 update, which still included .dsw files. Download the SDK Developer Kit (June06 version is >400 Mb). Run the downloaded .exe file to unzip and install. If GraphEdit isnt listed in the DX utilities on the Start menu, download and install the most recent Extras too.
DirectShow
Based on the Filter Graph Modules that handle a single function can be mixed and matched. This example renders an AVI file.
Filter Types
Source Filters (output pins only)
Capture Video/Audio File
Easier to run Better GUI Sample menu at All Programs->Microsoft DirectX SDK->DirectX Sample Browser
Using GraphEdit
Programs->Microsoft DirectX SDK ->DirectX Utilities->GraphEdit
Make a shortcut to it on your desktop for easy access
To render an AVI file: File->Render Media File, then Play Add filters with Graph->Insert Filters To render live video:
From filter list, under Video Capture Sources, insert filter for camera Right-click on capture pin and select Render pin Press Play
Using GraphEdit
You can add filters in the middle for effects, or change the output to a file writer to save the results. You can see the graph that an application has built: File->Connect To Remote Graph If your application is not listed, add the code from the topic Loading a Graph From an External Process in the DirectShow documentation (included with extras).
Example 1: Filter
Problem: Write a filter that converts a color image to grayscale by copying the green component to red and blue. 1. Create a new filter project named filter1 by one of the methods described previously. 2. In filter1.cpp, in Transform(), change the for loop to:
for (iPixel=0; iPixel<numPixels; iPixel++, prgb+=iPixelSize) { *(prgb ) = (prgb + 1); // B channel *(prgb+2) = (prgb + 1); // R channel }
3. 4.
Regsvr32 Notes
Regsvr32 is used to register a filter, COM object, ActiveX control, etc. Extension for filter is .ax File will be in /Debug or /Release To register: regsvr32 <full path of target> To unregister: regsvr32 /u <full path of target> Only need to register once can recompile without reregistering. Errors may occur if libraries have been updated, so try recompiling old filters if there are problems.
Writing an Application
For VC++.NET, 1. Unzip http://www.cs.ucf.edu/~lspencer/MYPROJECT.zip. (Code was created using wizard on next page.) 2. Rename all files (including res/*) containing MYPROJECT, changing MYPROJECT to your project name. 3. Replace MYPROJECT with your project name inside all the files (in Visual Studio: Edit->Find and Replace>Replace in Files without opening the project.) 4. Load the renamed .dsw file (will be converted to .sln) 5. Newer DX SDKs require changing wsprintfW(wsz, to StringCchPrintfW(wsz, 128 on line 909 of GraphBase.cpp.
Writing an Application
For VC++6.0, 1. Use the AppWizard from
http://www.ifp.uiuc.edu/~chenyq/research/Utils/ DxVideoAppWiz/DxVideoAppWiz.html. Choose 24-bit color, so the image will be 3 channels.
2. In Additional include directories change Multimedia to C++. (The location of streams.h changed in DX9.) 3. The link warning can be avoided by ignoring library libcmtd.lib in Debug and libcmt.lib in Release.
Writing an Application
Writing an Application
To run movies asynchronously (dont skip frames; may run faster than original movie), add the following code to RenderFile() in GraphBase.cpp before starting the graph:
IMediaFilter *pMediaFilter = 0; M_pGraphBuilder->QueryInterface(IID_IMediaFilter, (void**)&pMediaFilter; pMediaFilter->SetSyncSource(NULL); pMediaFilter->Release;
The Grabber calls a user function (SampleCB) when it receives a new frame.
Example 2: Application
Problem: Write an application that finds the greenest pixel, and puts an X over it. 1. Follow the previous instructions for creating a new video application. 2. Edit xxxDoc::SampleCB (code is on next slides) 3. Run the application. The X should follow a green laser pointer.
Example 2: Draw X
// Draw X around it int i, inside = 5, outside = 10; RGBTRIPLE color = {0, 0, 255}; // red if (best_pixel - outside - outside * cxImage * iPixelSize >= pData && best_pixel + outside + outside * cxImage * iPixelSize < pData + lDataLen) { for (i = inside; i < outside; i++) { rgb = (RGBTRIPLE *)(best_pixel + (i + i * cxImage) * iPixelSize); *rgb = color; rgb = (RGBTRIPLE *)(best_pixel + (i - i * cxImage) * iPixelSize); *rgb = color; rgb = (RGBTRIPLE *)(best_pixel - (i + i * cxImage) * iPixelSize); *rgb = color; rgb = (RGBTRIPLE *)(best_pixel - (i - i * cxImage) * iPixelSize); *rgb = color; } } return NOERROR; }
Advanced DirectShow
How to write an application without the AppWizards
Skip section
COM
COM is the Component Object Model, which is the basis for OLE. Call CoInitialize() once at the beginning to initialize COM. Call CoUninitialize() at termination to release COM.
COM
Create each COM object with CoCreateInstance()
specify the following:
CLSID of the object IID of the desired interface a place to return a pointer to the interface.
COM
Use ip->QueryInterface() to get additional interfaces for the same object
Specify the following:
IID of the interface A place to return a pointer to the interface
DirectShow Steps
1. Initialize COM:
CoInitialize(NULL);
DirectShow Steps
3. Get pointers to the other interfaces we need:
The IMediaControl interface provides methods for controlling the flow of data through the filter graph, including running, pausing and stopping.
IMediaControl *pMediaControl = NULL; pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
DirectShow Steps
3. Get pointers to the other interfaces we need:
The IMediaEvent interface contains methods for retrieving event notifications and for overriding the Filter Graph Managers default handling of events.
IMediaEventEx *pEvent = NULL; pGraph->QueryInterface(IID_IMediaEvent, (void **)& pEvent );
DirectShow Steps
4. For capturing live video, we need a Capture Graph:
Create the Capture Graph
ICaptureGraphBuilder2 *pCapture = NULL; CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pCapture);
DirectShow Steps
5. Use the Filter Graph Manager to build a filter graph.
For an AVI file:
pGraph->RenderFile(LC:\\Example.avi, NULL);
DirectShow Steps
6. (Optional) Send the video to another window.
IVideoWindow *pVidWin = NULL; pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin); pVidWin->put_Owner((OAHWND)g_hwnd); // or CWnd::m_hWnd in MFC pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS); RECT grc; GetClientRect(g_hwnd, &grc); pVidWin->SetWindowPosition(0, 0, grc.right, grc.bottom);
DirectShow Steps
7. Use the Filter Graph Manager to run the filter graph.
For an AVI file:
pMediaControl->Run(); pEvent->WaitForCompletion(INFINITE, &evCode);
DirectShow Steps
8. Clean up
if (pVidWin) pVidWin->Release(); pMediaControl->Release(); pEvent->Release(); pCapture->Release(); pGraph->Release(); CoUninitialize();
Use the property page to manipulate the filter. For live capture, we have to connect the pins ourselves
Open CV
Intel Open Source Computer Vision Library
OpenCV
Intels Open Source Computer Vision Library Available for anyone, including commercial use Source and examples are available. Built on top of Intels Image Processing Library (IPL) (if present), optimized for speed.
Structural Analysis
Contour Processing Shape Fitting
Object Recognition
Eigen Objects Hidden Markov Models
Image Analysis
Feature Detection Pyramids Moments Histogram Contour Retrieving
3D Reconstruction
Camera Calibration Fundamental Matrix View Morphing
OpenCV Resources
Web page is
http://www.intel.com/technology/computing/opencv/index.htm
Newsgroup is at http://groups.yahoo.com/group/OpenCV/
Subscribe to the newsgroup with a free Yahoo ID.
Downloads at http://sourceforge.net/projects/opencvlibrary
Version Beta 5 was released July 2005
For help, read documentation, search sample apps or search newsgroup. If all else fails, post to the newsgroup.
Using OpenCV
Many of the functions only work with limited image formats (like 8-bit grayscale). In the old pdf manual, this was listed in Appendix A. Theres no way to load a grayscale image or input a BW stream in DirectShow. Use CvtColor() to convert.
From Appendix A:
Input Format: 8u Output Format: 8u # chan: 1
Example 3 Steps
1. Create a DirectShow filter using the method shown previously 2. In Transform()
a. b. c. d. Convert the input bitmap to an IplImage Convert the input image to grayscale Run the Canny algorithm Convert back to RGB
Example 3: Properties
Step 3: Hook up the property page - Create the GUI controls - Add variables - Use these variables in the Canny function
From Appendix A: src can be 8u, 8s, or 32f; 1 or 3 channels. accum must be 32f; same number of channels as src.
Example 4: Steps
1. Convert the input bitmap to an IplImage 2. Update accumulated image
a. First time, create and initialize it b. If it already exists, call RunningAvg()
3. Convert accumulated image back to 8u, overwriting the input bitmap 4. Hook up the property page 5. Run it!
In xxxDoc::SampleCB()
// update the mean image If (!m_meanImage) { m_meanImage = cvCreateImage(size, IPL_DEPTH_32F, 3); cvConvertScale(src_rgb, m_meanImage, 1.0, 0.0); } else { cvRunningAvg(src_rgb, m_meanImage, 0.05, NULL); }
Example 4: Properties
Step 5: Hook up the property page
a. Add a menu item b. Make a new dialog box with variables assigned to the controls c. Add corresponding variables to xxxDoc and initialize them in the constructor. d. Add a handler for the new menu item:
CXxxDlg dlg; dlg.m_* = m_*; if (dlg.DoModal() == IDOK) { m_* = dlg.m_*; }
Example 4: Output
Step 5: Run it!
Open CV HighGUI
HighGUI is an additional OpenCV library with some user interface functions. Documentation was added in Beta 3 version.
Example 5: HighGUI
Problem: Given a binary image, fill each set of connected components with a different color, ignoring holes. Input: Output:
Example 5: HighGUI
We need some tools to deal with contours:
int cvFindContours(IplImage* img, CvMemStorage* storage, CvSeq**firstContour, int headerSize=sizeof(CvContour), CvContourRetrievalMode mode=CV_RETR_LIST, CvChainApproxMethod method=CV_CHAIN_APPROX_SIMPLE); void cvDrawContours( IplImage *img, CvSeq* contour, int externalColor, int holeColor, int maxLevel, int thickness=1 );
FindContours
int cvFindContours(IplImage* img, CvMemStorage* storage, CvSeq**firstContour,int headerSize=sizeof(CvContour), CvContourRetrievalMode mode=CV_RETR_LIST, CvChainApproxMethod method=CV_CHAIN_APPROX_SIMPLE); img Single channel image of IPL_DEPTH_8U type. Non-zero pixels are treated as 1-pixels. The function modifies the content of the input parameter. storage Contour storage location. firstContour Output parameter. Pointer to the first contour on the highest level. headerSize Size of the sequence header; must be equal to or greater than sizeof(CvChain) when the method CV_CHAIN_CODE is used, and equal to or greater than sizeof(CvContour) otherwise.
FindContours
int cvFindContours(IplImage* img, CvMemStorage* storage, CvSeq**firstContour,int headerSize=sizeof(CvContour), CvContourRetrievalMode mode=CV_RETR_LIST, CvChainApproxMethod method=CV_CHAIN_APPROX_SIMPLE); mode Retrieval mode. CV_RETR_EXTERNAL retrieves only the extreme outer contours (list); CV_RETR_LIST retrieves all the contours (list); CV_RETR_CCOMP retrieves the two-level hierarchy (list of connected components); CV_RETR_TREE retrieves the complete hierarchy (tree).
FindContours
CV_RETR_EXTERNAL returns W1, W2, W3 CV_RETR_LIST returns W1, W2, W3, W5, W6, B2, B3, B4 CV_RETR_CCOMP returns W1->B2, B3; W2->null; W3->B4; W5->null; W6->null CV_RETR_TREE returns W1 W2 W3 W5 W6
FindContours
int cvFindContours(IplImage* img, CvMemStorage* storage, CvSeq**firstContour,int headerSize=sizeof(CvContour), CvContourRetrievalMode mode=CV_RETR_LIST, CvChainApproxMethod method=CV_CHAIN_APPROX_SIMPLE); method Approximation method. CV_CHAIN_CODE outputs contours in the Freeman chain code. CV_CHAIN_APPROX_NONE translates all the points from the chain code into points. CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments, that is, it leaves only their ending points; CV_CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_TC89_KCOS are two versions of the Teh-Chin approximation algorithm.
DrawContours
void cvDrawContours( IplImage *img, CvSeq* contour, int externalColor, int holeColor, int maxLevel,int thickness=1 ); img Image where the contours are to be drawn. Like in any other drawing function, every output is clipped with the ROI. contour Pointer to the first contour. externalColor Color to draw external contours with. holeColor Color to draw holes with. maxLevel Maximal level for drawn contours. If 0, only the contour is drawn. If 1, the contour and all contours after it on the same level are drawn. If 2, all contours after and all contours one level below the contours are drawn, etc. thickness if positive or zero, the thickness of lines the contours are drawn with. If negative (CV_FILLED), the contour is filled.
Example 5: Steps
Now were ready to list the steps to solve the problem: 1. Load the input image 2. Display the input image 3. Convert the input to grayscale 4. Find the contours 5. Make an output image and draw the contours 6. Save and display the output image
Example 5: Results
Input Image: CV_RETR_EXTERNAL, filled
CV_RETR_EXT, width=2
CV_RETR_LIST, width=2
Contact
Drop me an email if you found this useful, or have suggestions.
LSpencer@cs.ucf.edu