DirectShow single-frame capture class without MFC

Source: Internet
Author: User

DirectShow single-frame capture class without MFC

 

Audrey mbogho(View profile)
2017l 22,200 5

Environment:Windows XP, VC ++ 7 (. NET)

Introduction

In computer vision, one usually needs access to video frames as they are streamed in order to analyze or process them in some way. this article shows how to obtain a single bitmap from a video stream using Microsoft DirectShow. A prevous article showed how to do this by using video for Windows. video for Windows, however, is an old API that was meant for use with webcams; it is not capable of communicating with newer devices such as digital video cameras and newer data format standards such as MPEG. directShow was designed to address these limitations through extensibility; in other words, manufacturers of new devices can provide functionality for them through software objects that can be hooked together with existing objects.

In DirectShow, data is streamed by being passed between COM objects known as filters. filters connect with one another in the standard way defined by COM. the manufacturer of the new device just needs to provide a filter that encapsulates its new behavior. A filter can also be written to intercept data, manipulate it in some way, and pass it on downstream. while writing filters requires knowledge of COM, no knowledge of COM is required to use existing filters such as those that come with DirectShow, As you are going to do in this article. note that legacy VFW devices will work with DirectShow.

In DirectShow, data travels from source to sink through filters chained together to form a filter graph. the simplest filter graph will have two filters, a source filter connected to a Renderer Filter. between these two, one might want to add a filter that transforms the data, for example, by removing color from it or changing its format. the connection points on a filter are known as input and output pins, and a filter can have several of them. for example, you can have an AVI splitter that records es data on one input pin, splits it into picture and sound data streams, which it outputs on two pins, one being an input to a video display and the other a sound player.

A component known as the filter graph manager provides methods for connecting filters to form the filter graph; provides high-level control of the filter graph through commands such as run, stop, and pause; and notifies the application of events such as "Capture complete."

To summarize, a basic DirectShow application will take the following actions:

  • Create the filter graph manager.
  • Create source and Renderer filters, and any others to be placed between the two.
  • Use the filter graph manager to connect the filters.
  • Run the graph.
  • Respond to events.
  • Stop the graph.
  • Release all COM objects.

Grabbing a sample

The following is an outline of what is done in your particle example of grabbing a sample from a video stream, and how it is coded. only the essenessenare highlighted here. the picture can be made clearer by looking at cgrabbitmap: grabbitmap () in grabbitmaps. CPP in the demo project.

// Create the filter graph manager.

Ccomptr <igraphbuilder> pgraph;

Pgraph. cocreateinstance (clsid_filtergraph );

// Create the source filter.

Ccomptr <ibasefilter> psource;

// Get default video device. (See below for more on connecting to

// Capture device .)

Getdefacapcapdevice (& psource );

// Create the sample grabber filter. (See below for further

// Clarification on the workings

// Of the sample grabber .)

Ccomptr <isamplegrabber> pgrabber;

Pgrabber. cocreateinstance (clsid_samplegrabber );

Ccomqiptr <ibasefilter, & iid_ibasefilter> pgrabberbase (pgrabber );

// Add the filters to the filter graph.

Pgraph-> addfilter (psource, l "Source ");

Pgraph-> addfilter (pgrabberbase, l "Grabber ");

// Connect the filters (see below for more on connecting filters)

Pgraph-> connect (psourcepin, pgrabpin );

Ccomptr> ipin> pgraboutpin = getoutpin (pgrabberbase, 0 );

Pgraph-> render (pgraboutpin); // This call connects the Renderer

// Filter to complete the Graph

// Set up graph to grab just one sample.

Pgrabber-> setoneshot (true );

// Wait to be notified that a sample has been obtained.

Ccomqiptr <imediaevent, & iid_imediaevent> pevent (pgraph );

Long evcode = 0;

Pevent-> waitforcompletion (infinite, & evcode );

Connecting to a capture device

The getdefacapcapdevice () function in the demo works as follows:

  1. Creates a device enumerator, which lists all devices registered on the system.
  2. Uses this device enumerator to obtain an enumarator for the video input devices category.
  3. Goes through this list of video input devices and stops as soon as an available one is found.
  4. Obtains the filter that manages this device.

The sample Grabber

The sample grabber filter operates in two modes. in the simpler mode, you set a buffer where a copy of The grabbed sample is placed. you use the more complex, more robust mode, in which a callback function is specified. this callback is called whenever a sample becomes available.

The way that callback mode works is rather convoluted. These are the steps:

  1. Implement a completely separate interface, isamplegrabbercb, and define either its buffercb () method or its samplecb () method. (See grabbitmaps. cpp in the demo project .)
  2. Create an object of isamplegrabbercb:

Isamplegrabbercb CB;

  1. Indicate which callback method you have defined by calling isamplegrabber: setcallback ():
  2. Grabber-> setcallback (& CB, 1); // 1 as second Arg indicates
  3. // Buffercb (),
  4. // 2 indicates samplecb ().

There are certain circumstances where the behavior of the sample Grabber is flawed. for this reason, Microsoft provides an improved filter, strangely named the grabber sample, which one has to compile and register (by using regsvr32 ). it can be found in the {DirectX SDK home dir} \ samples \ c ++ \ DirectShow \ filters \ grabber folder. although the grabber sample compiles and registers cleanly, none of the examples use it, so it may take some effort to figure out how it works. however, it's worth the effort, for in addition to avoiding the bugs in the sample grabber, using the grabber sample does not involve implementing a separate interface.

Connecting Filters

Connecting filters involves a negotiation between output and input pins. this is necessary because, for example, it is possible for a downstream filter to request data in a format that the upstream filter does not support. it is also possible for an upstream filter to offer data that the downstream filter cannot handle. negotiation allows such connections to be rejected. the steps for connecting two filters are as follows:

  1. Obtain the output pin of the upstream filter.
  2. Obtain the input pin of the downstream filter.
  3. Ask the filter graph manager to connect them.
  4. The filter graph manager asks the output pin to connect with the input pin.
  5. If the output pin accepts the connection, and the input pin also accepts it, the connection succeeds.

The filter graph for your example is wrongly strated in the figure below.

 

A word about DirectShow sample code

Although DirectShow is packaged together with DirectX, Microsoft seems ambivalent in how tight this coupling shocould be. initially, DirectShow was a separate technology known as activemovie. then, there was a move to make it part of DirectX as indicated by the name change. however, current downloads of DirectX do not include the DirectShow Code samples. instead, these are available separately in a download package known as extras. the user has to copy them back inside the DirectX structure where they compile nicely (the docs has CT you to find them there without telling you that you have to put them there yourself ). this, I recall reading somewhere, is part of a move to completely separate DirectShow from DirectX in future.

A second thing to note about the samples is that the most recent versions are built using Visual Studio. net and come with. vcproj project file only, so you're on your own if you have VC ++ 6, but it can be done.

Class usage

To use this class, you call cgrabbitmap: grabbitmap () like this:

Pbitmapinfo pbitmap = NULL;

Ulong bitmapsize = 0;

Cgrabbitmap GB;

If (GB. grabbitmap (& pbitmap, & bitmapsize ))

{

// Use pbitmap

// Delete it

}

Have a look at main. cpp in the demo project to see how I used the class.

An executable is already ded. But, if you want to build the class yourself, you need to do the following:

  1. Download the DirectX sdks from http://msdn.microsoft.com/directx/directxdownloads/default.aspx.
  2. Also, download the extras from that same page. The extras include DirectShow base classes and samples.
  3. Copy the DirectShow folder from the extras into {DirectX SDK home dir} \ samples \ c ++.
  4. Compile the baseclasses found inside the DirectShow folder in order to create strmbasd. Lib (Debug) or strmbase. Lib (release ).
  5. In You Project Settings, specify the basecalsses folder in the include path as well as {DirectX SDK home dir }.
  6. Also, link with the following libraries: strmbasd. lib, winmm. lib, odbc32.lib, odbccp32.lib, and quartz. lib. remember that strmbasd. lib (or strmbase. lib for release version) is in the debug (or release) folder in the baseclasses folder.

That's all! I have used this class with a DV camera connected via USB and via IEEE 1394 and with a USB webcam. It works well in all cases.

About the author
I'm a doctoral candidate studying the evaluation of computer vision. this is an important area because for policyears computer vision research was moving full steam ahead without solid experimental evito to back it up. properly done, evaluation will answer questions about how reliable a vision system is, or, even more interesting, it will tell us under what circumstances a given vision system is most reliable. armed with this knowledge, users can select the systems or input parameters that suit their needs best. this shoshould result in increased demand for computer vision technology.

Downloads

  • Grabbitmaps.zip-demonstrates how to use cgrabbitmap to grab an image from DV or web cam, via firewire or USB, using DirectShow, display it and save it.

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.