Click here to Skip to main content
6,630,586 members and growing! (15,522 online)
Email Password   helpLost your password?
Multimedia » DirectX » General     Intermediate

Prototyping DirectShow filters in C#

By daniel049

This article shows how to implement a DirectShow filter in C#.
C#, VC7, Windows, .NET, DirectX, Dev
Posted:14 Dec 2005
Views:107,296
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 3.07 Rating: 4.39 out of 5

1

2

3
2 votes, 40.0%
4
3 votes, 60.0%
5

Sample Image

Introduction

DirectShow filters are the basic building blocks of multimedia applications on the Windows platform. Normally, they are written in C++. In this article, I'll show how to implement a couple of DirectShow transformation filters in C#. Since Microsoft doesn't recommend using managed code for filters, I'll call them prototypes but they perform quite well in most circumstances. We'll develop Sobel transformation as a DirectShow filter. Sobel transformations are often used for video segmentation which is the process of identifying objects in a scene.

Background

The strategy we use to implement a DirectShow filter in C# relies on having a C++ class that contains a pointer to a managed object which implements our filter. The C++ class just forwards all the processing to the managed object. The C++ class is generated using Cutler's DirectShow Filters Wizard. We just clean up the wizard-generated code to include a pointer to our C# object and implement all the filter processing as methods of the managed object.

I've written this Sobel filter to have an idea of the kind of performance we'd get with C#. If you want to "take your CPU for a spin", this is a good test. As written (i.e., with no optimization effort), there are around 20 integer multiplications per pixel. Usually, you'd transform an image to black and white before applying Sobel transformation to the image. So I've also written a transformation filter that converts a RGB24 bitmap to black and white.

Using the code

If you just want to have a filter that implements Sobel transformations, you can download and install the managed DLL and Ax files (from the download link). Then after registering the Ax file and installing the managed DLL in the Global Assembly Cache (GAC), you can start the GraphEdit utility and add the "CsSobelV2" filter (found in the DirectShow filters category of GraphEdit). Then render a media file to see a black and white "sketch" of the frames in the video. (Note that the open-source DirectShowLib must also be in the GAC.) More information can be found in the "readme.txt" file that is included in the download.

If you want to implement your own filter using these filters as a starting point, you can modify the Transform and ProcessFrame methods of the MySobelV2 class. This is sufficient if you are satisfied with a single media type for the video frame, here Rgb24, and you can live with the default memory allocator of the C++ DirectShow base classes (this covers a lot of situations).

Sobel transformation

The Sobel transformation uses the following two matrices:

  // define the horizontal Sobel mat

  int [,] hx = new  int [,] { {-1, 0, 1}, 
                    {-2, 0, 2}, {-1, 0, 1} };

  // define the vertical Sobel mat

  int[,] hy = new int [,]  { { 1,  2,  1}, 
                  { 0,  0,  0}, {-1, -2, -1} };

And consists of the following code, found in ProcessFrame, which is repeated for every pixel in every frame of a video:

        gradX = 0;
        gradY = 0;
        for (int row = -1; row <= 1; row++)
        {
          for (int col = -1; col <= 1; col++)
          {
            position = GetPosition( x + col, y + row ); 
            intensity = pwSource[position];
            gradX += (intensity * hx[col + 1,row + 1]);
            gradY += (intensity * hy[col + 1,row + 1]); 
          }
        }
         
        // compute the square of the gradient        

        gradMag = gradX * gradX + gradY * gradY;

        // threshold default at 128

        gradMag = (gradMag < threshold*threshold) ? 255 : 0;
               
        // assign the pixel to the destination

        position = GetPosition( x, y );
        pwTarget[position] = pwTarget[position+1] = 
              pwTarget[position+2] = (byte)gradMag;

The intuition behind the transformation is relatively simple. Since the contour of objects consists of a region in a picture where the color changes rapidly, the Sobel transformation amplifies these differences and simply sets to white those pixels whose changes are too small (and black, the other ones). The horizontal Sobel transformation looks at the left and right neighboring pixels and multiplies these values by 2 or -2, it also looks at the upper right and left, and lower right and left pixels. After summing all these values, you compute the square of the Euclidian distance and use an arbitrary threshold to divide the values.

Points of Interest

I've also included the code for a black and white transformation filter for uncompressed Rgb24 video frames. As mentioned, you'd normally perform this transformation before feeding the result to the Sobel filter. So you can test the performances of a filter graph with two filters written in managed code.

Property pages for filters are handy when used in a utility like GraphEdit. But being able to access custom filter properties from C# (easily) is even more useful. So I've modified Cutler's Wizard-generated C++ code to include a property that can be changed in C#. Similar changes could be done to implement different properties.

Limitations and known issues

The media type accepted by the filters is uncompressed Rgb24 video. Since this is one of the most "natural" media types, it's not a big limitation but the filters were not designed to be very flexible. They are just prototypes to experiment with.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

daniel049


Member
You can read my blog entries at:
http://wwww.informikon.com/blog/
Location: United States United States

Other popular DirectX articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 93 (Total in Forum: 93) (Refresh)FirstPrevNext
GeneralGreat idea PinmemberSteven Nagy17:47 6 Oct '09  
GeneralMSIL error at compiling CsSobelV2.ax Pinmemberpatkoxx4:44 4 Jun '09  
GeneralRe: MSIL error at compiling CsSobelV2.ax Pinmembertsylverberg21:50 30 Jul '09  
GeneralDllRegisterServer in CsSobel.ax Failed. PinmemberroboticEDAR13:31 11 May '09  
QuestionVisual studios 2008 PinmemberMember 47309907:45 6 May '09  
AnswerRe: Visual studios 2008 PinmemberroboticEDAR12:19 7 May '09  
AnswerRe: Visual studios 2008 PinmemberroboticEDAR13:31 27 May '09  
GeneralThere Are Several NON-Commercial RTSP Procol Source Filters! PinmemberGUI Developer5:37 26 Oct '08  
QuestionAbstract Class Error Pinmembernickmaggie4:56 31 Aug '07  
AnswerRe: Abstract Class Error Pinmemberdaniel04910:44 1 Sep '07  
GeneralCompiling on VS 2003 version 7.1 Framework 1.1 PinmemberRuss539:01 1 Aug '07  
GeneralRe: Compiling on VS 2003 version 7.1 Framework 1.1 Pinmemberdaniel0493:43 2 Aug '07  
Generalnot working Pinmemberyair_coh5:23 26 May '07  
GeneralRe: not working Pinmemberredpirate3:40 13 Jun '07  
GeneralRe: not working PinmemberToolmakerSteve221:29 17 Jul '07  
GeneralRe: not working Pinmemberppaijwar18:34 28 Nov '07  
GeneralRe: not working PinmemberroboticEDAR14:39 12 May '09  
GeneralRe: not working PinmemberroboticEDAR12:23 7 May '09  
Questiona cry for help from FDaSilva PinstaffSean Ewington14:59 9 May '07  
AnswerRe: a cry for help from FDaSilva Pinmemberdaniel04911:33 17 May '07  
GeneralVS Express Solution and Projects file can be downloaded Pinmemberdaniel0497:04 17 Nov '06  
GeneralRe: VS Express Solution and Projects file can be downloaded Pinmemberpatkoxx7:40 11 Jun '09  
GeneralAccesing to a custom interface Pinmemberburakburak12:30 5 Sep '06  
GeneralRe: Accesing to a custom interface Pinmemberdaniel04914:39 5 Sep '06  
Generalcannot find streams.h PinmemberNimble9917:39 3 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Dec 2005
Editor: Smitha Vijayan
Copyright 2005 by daniel049
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project