2

I'm trying to extract a specific frame from a video file. I have a frame that I want when I play a video file with the aforge library. I call a new frame event, and if the new frame matches my specific frame, then it shows me a message: "Frame Match". This specific frame randomly appears in a video file. Here is my code:

private void Form1_Load(object sender, EventArgs e)
{
    IVideoSource videoSource = new FileVideoSource(@"e:\media\test\a.mkv");
    playerControl.VideoSource = videoSource;
    playerControl.Start( );
    videoSource.NewFrame += new AForge.Video.NewFrameEventHandler(Video_NewFrame );
}

private void Video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    //Create Bitmap from frame
    Bitmap FrameData = new Bitmap(eventArgs.Frame);
    //Add to PictureBox
    pictureBox1.Image   = FrameData;
    //compare current frame to specific fram
    if (pictureBox1.Image == pictureBox2.Image)
    {
        MessageBox.Show("Frame Match");  
    }
}

pictureBox2.image is a fixed frame that I want to match. This code is working fine when I play video files and extract new frames, but I am unable to compare new frames to specific frames. Please guide me on how to achieve this.

alex
  • 6,818
  • 9
  • 52
  • 103
user3159467
  • 21
  • 1
  • 2

2 Answers2

3

You can take a look at: https://github.com/dajuric/accord-net-extensions

var capture = new FileCapture(@"C:\Users\Public\Videos\Sample Videos\Wildlife.wmv");
capture.Open();
capture.Seek(<yourFrameIndex>, SeekOrigin.Begin);
var image = capture.ReadAs<Bgr, byte>();

or you can use standard IEnumerable like:

var capture = new FileCapture(@"C:\Users\Public\Videos\Sample Videos\Wildlife.wmv");
capture.Open();
var image = capture.ElementAt(<yourFrameIndex>); //will actually just cast image

Examples are included.

moved to: https://github.com/dajuric/dot-imaging

dajuric
  • 2,373
  • 2
  • 20
  • 43
  • Really nice, @dajuric! I'm trying to do this right now, but I seem unable to convert the return type to ImageSource or Bitmap. How do I work with that Image? Thanks a lot!! – SuperJMN Apr 26 '16 at 21:45
  • Also, I'm getting this exception when creating the FileCapture instance: Cannot load DLL file 'opencv_highgui248'. What should I do? – SuperJMN Apr 26 '16 at 22:35
0

As far as I can understand your problem the issue is that you can't compare image to image this way. I think you will find that the way to do this is to build a histogram table and then compare image histograms.

Some of the related things to look into are:

how to compare two images

image comparer class form VS 2015 unit testing

The second one is from unit testing library so not sure of performance (haven't tried myself yet)

Community
  • 1
  • 1
Tom
  • 21
  • 5