Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Optical Flow or Motion Estimation Using the Watson-Ahumada (WA) Algorithm

Rate me:
Please Sign up or sign in to vote.
4.98/5 (24 votes)
24 Nov 2014CPOL12 min read 139.9K   13.9K   123  
Optical Flow or Motion Estimation Using the Watson-Ahumada (WA) Algorithm
  • simple.zip
    • anim.10.tif
    • anim.11.tif
    • anim.12.tif
    • anim.13.tif
    • anim.14.tif
    • anim.15.tif
    • anim.16.tif
    • anim.17.tif
    • anim.18.tif
    • anim.19.tif
    • flow.10.pcm
    • flow.11.pcm
    • flow.12.pcm
    • flow.13.pcm
    • flow.14.pcm
    • flow.15.pcm
    • flow.16.pcm
    • flow.17.pcm
    • flow.18.pcm
    • flow.19.pcm
  • wav2.zip
  • medium.zip
    • anim.10.tif
    • anim.11.tif
    • anim.12.tif
    • anim.13.tif
    • anim.14.tif
    • anim.15.tif
    • anim.16.tif
    • anim.17.tif
    • anim.18.tif
    • anim.19.tif
    • flow.10.pcm
    • flow.11.pcm
    • flow.12.pcm
    • flow.13.pcm
    • flow.14.pcm
    • flow.15.pcm
    • flow.16.pcm
    • flow.17.pcm
    • flow.18.pcm
    • flow.19.pcm
  • vcbox.zip
    • vcbox.0.gif
    • vcbox.1.gif
    • vcbox.1.pcm
    • vcbox.2.gif
    • vcbox.2.pcm
    • vcbox.3.gif
    • vcbox.3.pcm
    • vcbox.4.gif
    • vcbox.4.pcm
    • vcbox.5.gif
    • vcbox.5.pcm
    • vcbox.6.gif
    • vcbox.6.pcm
    • vcbox.7.gif
    • vcbox.7.pcm
    • vcbox.8.gif
    • vcbox.8.pcm
    • vcbox.9.gif
    • vcbox.9.pcm
  • complex.zip
    • anim.10.tif
    • anim.11.tif
    • anim.12.tif
    • anim.13.tif
    • anim.14.tif
    • anim.15.tif
    • anim.16.tif
    • anim.17.tif
    • anim.18.tif
    • anim.19.tif
    • flow.10.pcm
    • flow.11.pcm
    • flow.12.pcm
    • flow.13.pcm
    • flow.14.pcm
    • flow.15.pcm
    • flow.16.pcm
    • flow.17.pcm
    • flow.18.pcm
    • flow.19.pcm
  • grid.zip
    • grid.0.gif
    • grid.1.gif
    • grid.1.pcm
    • grid.2.gif
    • grid.2.pcm
    • grid.3.gif
    • grid.3.pcm
    • grid.4.gif
    • grid.4.pcm
    • grid.5.gif
    • grid.5.pcm
    • grid.6.gif
    • grid.6.pcm
    • grid.7.gif
    • grid.7.pcm
    • grid.8.gif
    • grid.8.pcm
    • grid.9.gif
    • grid.9.pcm
  • blocks.zip
using System;
using System.Drawing;
using Sid;

namespace WAv2_examples
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		static void Blocks()
		{
			string dir = "c:/c#/optic_flow_test_data/blocks/";
			int N = 8;
			string[]	filenames = new string[N];
			for(int i = 1; i <= N; i++)
				filenames[i-1] = dir + "blocks."+i+".gif";
			WAdetector WA = new WAdetector();
			float[,,] f = WA.WAComputeOpticalFlow(filenames);
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2];
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2];
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second;
			string filename = dir + "blocks_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename);
		}

		static void Grid()
		{
			string dir = "c:/c#/optic_flow_test_data/grid/";
			int N = 8;
			string[]	filenames = new string[N];
			for(int i = 0; i < N; i++)
				filenames[i] = dir + "grid."+i+".gif";
			WAdetector WA = new WAdetector();
			float[,,] f = WA.WAComputeOpticalFlow(filenames);
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2];
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2];
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second;
			string filename = dir + "grid_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename);
		}

		static void Vcbox()
		{
			string dir = "c:/c#/optic_flow_test_data/vcbox/";
			int N = 8;
			string[]	filenames = new string[N];
			for(int i = 0; i < N; i++)
				filenames[i] = dir + "vcbox."+i+".gif";
			WAdetector WA = new WAdetector();
			float[,,] f = WA.WAComputeOpticalFlow(filenames);
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2];
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2];
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second;
			string filename = dir + "vcbox_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename);
		}

		static void Complex()
		{
			string dir = "c:/c#/optic_flow_test_data/complex/";
			int N = 8;
			string[]	filenames = new string[N];
			for(int i = 0; i < N; i++)
				filenames[i] = dir + "anim."+(i+10)+".tif";
			WAdetector WA = new WAdetector();
			float[,,] f = WA.WAComputeOpticalFlow(filenames);
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2];
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2];
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second;
			string filename = dir + "complex_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename);
		}

		static void Medium()
		{
			string dir = "c:/c#/optic_flow_test_data/medium/";
			int N = 8;
			string[]	filenames = new string[N];
			for(int i = 0; i < N; i++)
				filenames[i] = dir + "anim."+(i+10)+".tif";
			WAdetector WA = new WAdetector();
			float[,,] f = WA.WAComputeOpticalFlow(filenames);
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2];
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2];
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second;
			string filename = dir + "medium_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename);
		}

		static void Simple()
		{
			string dir = "c:/c#/optic_flow_test_data/simple/"; // the directory where images are stored
			int N = 8;	// number of files we wish to read for optical flow estimation
			string[]	filenames = new string[N];
			for(int i = 0; i < N; i++)
				filenames[i] = dir + "anim."+(i+10)+".tif";
			WAdetector WA = new WAdetector();	
			float[,,] f = WA.WAComputeOpticalFlow(filenames);	// compute optical flow for the sequence of images specified in filenames
			float[,,]	quiver_data = new float[f.GetLength(0),f.GetLength(1),2];	// for the quiver plot
			for(int y = 0; y < quiver_data.GetLength(0); y++)
				for(int x = 0; x < quiver_data.GetLength(1); x++)
				{
					quiver_data[y,x,0] = f[y,x,0]*f[y,x,2]; // scale vx by weight w
					quiver_data[y,x,1] = f[y,x,1]*f[y,x,2]; // scale vy by weight w
				}
			Bitmap bmp = WAdetector.Quiver(quiver_data);	// creates a bitmap to visualize the data in quiver_data
			int date = DateTime.Now.Year*1000 + DateTime.Now.Month*100 + DateTime.Now.Day;	// get date in yyyymmdd format
			int time = DateTime.Now.Hour*1000 + DateTime.Now.Minute*100 + DateTime.Now.Second; // get time in hhmmss format
			string filename = dir + "simple_flow-" + date + "-" + time + ".bmp";
			bmp.Save(filename); // save the quiver plot
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine("simple...");
			Simple();
			Console.WriteLine("medium...");
			Medium();
			Console.WriteLine("complex...");
			Complex();
			Console.WriteLine("vcbox...");
			Vcbox();
			Console.WriteLine("grid...");
			Grid();
			Console.WriteLine("blocks...");
			Blocks();
			Console.WriteLine("done!");
			Console.ReadLine();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Creator of bookmine.net

Comments and Discussions