private float CalculateUnitsPerPixelX(CameraVsPhysicalPoint neighbor)
		{
			float physicalDistance = (float)Math.Sqrt(
				(neighbor.PhysicalPoint.X - this.PhysicalPoint.X) * (neighbor.PhysicalPoint.X - this.PhysicalPoint.X) +
				(neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y) * (neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y));

			float pixelDistance = (float)Math.Sqrt(
				(neighbor.CameraPoint.X - this.CameraPoint.X) * (neighbor.CameraPoint.X - this.CameraPoint.X) +
				(neighbor.CameraPoint.Y - this.CameraPoint.Y) * (neighbor.CameraPoint.Y - this.CameraPoint.Y));

			return physicalDistance / pixelDistance;
		}
		public CalibrationPoint(
			CameraVsPhysicalPoint center,
			CameraVsPhysicalPoint left,
			CameraVsPhysicalPoint right,
			CameraVsPhysicalPoint above,
			CameraVsPhysicalPoint below)
		{
			this.CameraPoint = center.CameraPoint;
			this.PhysicalPoint = center.PhysicalPoint;
			this.UnitsPerPixelX = CalculateUnitsPerPixelX(left, right);
			this.UnitsPerPixelY = CalculateUnitsPerPixelY(above, below);
		}
        private float CalculateUnitsPerPixelX(CameraVsPhysicalPoint neighbor)
        {
            float physicalDistance = (float)Math.Sqrt(
                (neighbor.PhysicalPoint.X - this.PhysicalPoint.X) * (neighbor.PhysicalPoint.X - this.PhysicalPoint.X) +
                (neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y) * (neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y));

            float pixelDistance = (float)Math.Sqrt(
                (neighbor.CameraPoint.X - this.CameraPoint.X) * (neighbor.CameraPoint.X - this.CameraPoint.X) +
                (neighbor.CameraPoint.Y - this.CameraPoint.Y) * (neighbor.CameraPoint.Y - this.CameraPoint.Y));

            return(physicalDistance / pixelDistance);
        }
 public CalibrationPoint(
     CameraVsPhysicalPoint center,
     CameraVsPhysicalPoint left,
     CameraVsPhysicalPoint right,
     CameraVsPhysicalPoint above,
     CameraVsPhysicalPoint below)
 {
     this.CameraPoint    = center.CameraPoint;
     this.PhysicalPoint  = center.PhysicalPoint;
     this.UnitsPerPixelX = CalculateUnitsPerPixelX(left, right);
     this.UnitsPerPixelY = CalculateUnitsPerPixelY(above, below);
 }
        public CameraVsPhysicalPoint[,] GetCameraVsPhysicalPoints()
        {
            CameraVsPhysicalPoint[,] cameraVsPhysicalPoints = new CameraVsPhysicalPoint[this.XCount, this.YCount];
            for (int i = 0; i < this.XCount; i++)
            {
                for (int j = 0; j < this.YCount; j++)
                {
                    PointF   physicalCenter = GetPhysicalCenter(i, j);
                    MCvBox2D box            = m_InsideRectangles[i, j];

                    cameraVsPhysicalPoints[i, j] = new CameraVsPhysicalPoint(
                        box.center,
                        physicalCenter);
                }
            }

            return(cameraVsPhysicalPoints);
        }
		public CameraVsPhysicalPoint[,] GetCameraVsPhysicalPoints()
		{
			CameraVsPhysicalPoint[,] cameraVsPhysicalPoints = new CameraVsPhysicalPoint[this.XCount, this.YCount];
			for (int i = 0; i < this.XCount; i++)
			{
				for (int j = 0; j < this.YCount; j++)
				{
					PointF physicalCenter = GetPhysicalCenter(i, j);
					MCvBox2D box = m_InsideRectangles[i, j];

					cameraVsPhysicalPoints[i, j] = new CameraVsPhysicalPoint(
						box.center,
						physicalCenter);
				}
			}

			return cameraVsPhysicalPoints;
		}
		private float CalculateUnitsPerPixelX(CameraVsPhysicalPoint left, CameraVsPhysicalPoint right)
		{
			// left or right but not both can be null
			float pointsUsed = 0;
			float unitsPerPixelX = 0;

			if (left != null)
			{
				pointsUsed += 1;
				unitsPerPixelX += CalculateUnitsPerPixelX(left);
			}
			if (right != null)
			{
				pointsUsed += 1;
				unitsPerPixelX += CalculateUnitsPerPixelX(right);
			}

			return unitsPerPixelX / pointsUsed;
		}
        private float CalculateUnitsPerPixelY(CameraVsPhysicalPoint above, CameraVsPhysicalPoint below)
        {
            // above or below but not both can be null
            float pointsUsed     = 0;
            float unitsPerPixelY = 0;

            if (above != null)
            {
                pointsUsed     += 1;
                unitsPerPixelY += CalculateUnitsPerPixelY(above);
            }
            if (below != null)
            {
                pointsUsed     += 1;
                unitsPerPixelY += CalculateUnitsPerPixelY(below);
            }

            return(unitsPerPixelY / pointsUsed);
        }
        private float CalculateUnitsPerPixelX(CameraVsPhysicalPoint left, CameraVsPhysicalPoint right)
        {
            // left or right but not both can be null
            float pointsUsed     = 0;
            float unitsPerPixelX = 0;

            if (left != null)
            {
                pointsUsed     += 1;
                unitsPerPixelX += CalculateUnitsPerPixelX(left);
            }
            if (right != null)
            {
                pointsUsed     += 1;
                unitsPerPixelX += CalculateUnitsPerPixelX(right);
            }

            return(unitsPerPixelX / pointsUsed);
        }
		public void Initialize(CameraVsPhysicalPoint[,] cameraVsPhysicalPoints)
		{
			m_XCount = cameraVsPhysicalPoints.GetLength(0);
			m_YCount = cameraVsPhysicalPoints.GetLength(1);
			m_CalibrationPoints = new CalibrationPoint[m_XCount, m_YCount];

			CameraVsPhysicalPoint center, left, right, above, below;

			for (int i = 0; i < m_XCount; i++)
			{
				for (int j = 0; j < m_YCount; j++)
				{
					center = cameraVsPhysicalPoints[i,j];
					left = (i == 0 ? null : cameraVsPhysicalPoints[i - 1, j]);
					right = (i == m_XCount - 1 ? null : cameraVsPhysicalPoints[i + 1, j]);
					above = (j == 0 ? null : cameraVsPhysicalPoints[i, j - 1]);
					below = (j == m_YCount - 1 ? null : cameraVsPhysicalPoints[i, j + 1]);

					m_CalibrationPoints[i, j] = new CalibrationPoint(center, left, right, above, below);
				}
			}
		}
		private float CalculateUnitsPerPixelY(CameraVsPhysicalPoint above, CameraVsPhysicalPoint below)
		{
			// above or below but not both can be null
			float pointsUsed = 0;
			float unitsPerPixelY = 0;

			if (above != null)
			{
				pointsUsed += 1;
				unitsPerPixelY += CalculateUnitsPerPixelY(above);
			}
			if (below != null)
			{
				pointsUsed += 1;
				unitsPerPixelY += CalculateUnitsPerPixelY(below);
			}

			return unitsPerPixelY / pointsUsed;
		}
		private float CalculateUnitsPerPixelY(CameraVsPhysicalPoint neighbor)
		{
			return (neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y) / (neighbor.CameraPoint.Y - this.CameraPoint.Y);
		}
 private float CalculateUnitsPerPixelY(CameraVsPhysicalPoint neighbor)
 {
     return((neighbor.PhysicalPoint.Y - this.PhysicalPoint.Y) / (neighbor.CameraPoint.Y - this.CameraPoint.Y));
 }