1 Replies - 1284 Views - Last Post: 27 February 2008 - 12:43 PM Rate Topic: -----

#1 David24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 08

unsafe code i modified isn't working quite right..

Post icon  Posted 23 February 2008 - 09:45 AM

my best attempt to get the following code to store 2 ints representing the average location of x and y coordinates is throwing NullExeption errors. this is unsafe C# code by the way. i'm a novice so hopefully you will notice some obvious, glaring, simple solution for this. it's very important to me that i get this working. it would be a better motion detector than the one i currently use on my motion tracking turret. (although the old detector does the job well enough, it uses a blobcounting (from the Aforge library) algorithm that just doesn't act right)

		   else
			{
				// calculate motion without suppressing noise
				byte* motion = (byte*)currentFrame.ToPointer();

				for (int i = 0; i < frameSize; i++, motion++)
				{
					pixelsChanged += (*motion & 1);//compares the last bit of the motion value if it was set to one.
				}
			}

			// highlight motion regions
			if (highlightMotionRegions)
			{
				byte* src = (byte*)imageData.Scan0.ToPointer();
				byte* motion = (byte*)currentFrame.ToPointer();
				int srcOffset = imageData.Stride - width * 3;

				// shift to the red channel
				src += 2;

				int coordinateCount = 0;

				for (int y = 0; y < height; y++) // do this 480 times
				{
					for (int x = 0; x < width; x++, motion++, src += 3)//do this 640 times
					{
						*src |= *motion; //the address of source or motion = the address of source

						//i added this. if this is a motion pixel, store it's coordinates
						if (*motion > 0)
						{
							//x is guide to what pixel is being tested so store it as a coordinate
							xCoord[coordinateCount] = x;
							//the same with y
							yCoord[coordinateCount] = y;
							//increase the array number so the next values aren't written over the first
							coordinateCount++;						   
						}

					}
				   src += srcOffset;//moves the pointer to the next adress where to get the next data.
				}

				//i added this to get the average value of the ints in the x and y arrays
				int sumX = 0;
				for (int i = 0; i < coordinateCount; i++)
				{
					sumX += xCoord[i];
				}
				int targetX = sumX / coordinateCount;

				int sumY = 0;
				for (int i = 0; i < coordinateCount; i++)
				{
					sumY += yCoord[i];
				}
				int targetY = sumY / coordinateCount;
			}

			// unlock source image
			image.UnlockBits(imageData);


Is This A Good Question/Topic? 0
  • +

Replies To: unsafe code i modified isn't working quite right..

#2 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

Re: unsafe code i modified isn't working quite right..

Posted 27 February 2008 - 12:43 PM

Here is the code to get the average out of 2 numbers. Please use it:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{
	public class Example
	{
		// Function Average finds an average value of double d1 and double d2.
		public static double Average(double d1, double d2)
		{
			double dA = (d1 + d2) / 2;
			return dA;
		}
		public static void Main(string[] args)
		{
			double v1 = 1.0; double v2 = 4.0;
			double Result = Average(v1, v2);
			Console.WriteLine("The average of v1 = " + v1 + " and v2 = " + v2 + " is " + Result);
			Console.WriteLine("The average of v1 = " + v1 + " and v2 = " + v2 + " is " + Average(v1, v2));
		}
	}
}



Another example:

using System;
namespace MyProgram
{
	public class Student
	{
		public string Name;
		public double dGPA;
	}
	public class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Enter number of students");
			Console.WriteLine();
			string s = Console.ReadLine();
			int Number = Convert.ToInt32(s);
			Student[] students = new Student[Number];
			for (int i = 0; i < Number; i++)
			{
				Console.WriteLine("Enter Student Name " + (i + 1) + " : ");
				Console.WriteLine();
				string Name = Console.ReadLine();
				Console.WriteLine("Enter GPA: ");
				Console.WriteLine("");
				string GPA = Console.ReadLine();
				double dGPA = Convert.ToDouble(GPA);
				Student thisStudent = new Student();
				thisStudent.Name = Name;
				thisStudent.dGPA = dGPA;
				students[i] = thisStudent;
			}
			double sum = 0;
			for (int i = 0; i < Number; i++)
			{
				sum += students[i].dGPA;
			}
			double avg = sum / students.Length;
			Console.WriteLine();
			Console.WriteLine("The average of " + students.Length + " is " + avg);
		}
	}
}



The 3rd example:

using System;
using System.Collections.Generic;
using System.Text;

namespace Functions
{
	class Program
	{
		public static void Average(double d1, double d2, double d3)
		{
			double dAverage = (d1 + d2 + d3) / 3;
			Console.WriteLine("The average of GPA 1 = " + d1 + " and GPA 2 = " + d2 + " GPA 3 = " + d3 + " is " + dAverage);
		}
		public static void Average(string s1, double d1, string s2, double d2)
		{
			double dAverage = (d1 + d2) / 2;
			Console.WriteLine("The average of" + s1 + " whose value is " + d1 + " and " + s2 + " whose value is " + d2 + " is " + dAverage);
		}
		static void Main(string[] args)
		{
			//access the eber functions
			double d3 = 5;
			Average(" grade 1 ", 3.5, " grade 2 ", 4.0);
			Average(4.0, 4.5, d3);
			Console.WriteLine("Press Enter...");
			Console.Read();
		}
	}
}



By the way, the question was not from the advanced area. Please change the priority.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1