Beispiel #1
0
        /// <summary>
        /// Set the row color of a Bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <param name="y"></param>
        /// <param name="colors"></param>
        public static void SetRowColors(this Bitmap image, int y, Color[] colors)
        {
            LockBitmap lbmp = new LockBitmap(image);

            for (int x = 0; x < colors.Length; x++)
            {
                lbmp.SetPixel(x, y, colors[x]);
            }
            lbmp.UnlockBits();
        }
		/// <summary>
		/// Overlay Pixel Location to a bitmap
		/// </summary>
		/// <param name="image"></param>
		/// <param name="pixels"></param>
        public static void OverlayPixels(this Bitmap image, List<PointF> pixels)
		{
			LockBitmap lbmp = new LockBitmap(image);
			for (int iPx = 0; iPx < pixels.Count; iPx++)
			{
				int x = (int)(pixels[iPx].X);
				int y = (int)(pixels[iPx].Y);
				lbmp.SetPixel(x, y, Color.Red);
			}
			lbmp.UnlockBits();
		}
Beispiel #3
0
        /// <summary>
        /// Overlay Pixel Location to a bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <param name="pixels"></param>
        public static void OverlayPixels(this Bitmap image, List <PointF> pixels)
        {
            LockBitmap lbmp = new LockBitmap(image);

            for (int iPx = 0; iPx < pixels.Count; iPx++)
            {
                int x = (int)(pixels[iPx].X);
                int y = (int)(pixels[iPx].Y);
                lbmp.SetPixel(x, y, Color.Red);
            }
            lbmp.UnlockBits();
        }
        private List <PointF> SubProcess(LockBitmap before, LockBitmap after, LockBitmap debuggingImage,
                                         double laserThreshold)
        {
            int           firstRowLaserCol = before.Width / 2;
            List <PointF> laserLocations   = new List <PointF>(before.Height);


            int left_Clip   = 0;
            int top_Clip    = 0;
            int right_Clip  = left_Clip + before.Width;
            int bottom_Clip = top_Clip + before.Height;

            int width   = before.Width;
            int height  = before.Height;
            int rowStep = width;

            int numLocations = 0;

            int numMerged = 0;

            // The location that we last detected a laser line
            int prevLaserCol = firstRowLaserCol;


            /** The LaserRanges for each column */
            LaserRange[] laserRanges = new LaserRange[before.Height + 1];

            double[] magSquare = new double[before.Width];

            //            for (int y = 0; y < height && numLocations < height; y++)
            for (int y = top_Clip; y < bottom_Clip && numLocations < height; y++)
            {
                // The column that the laser started and ended on
                int numLaserRanges = 0;
                laserRanges[numLaserRanges].startCol = -1;
                laserRanges[numLaserRanges].endCol   = -1;

                //		        for (int  x = 0; x < rowStep; x += 1)
                /**/
                for (int x = left_Clip; x < right_Clip; x += 1)
                {
                    /**/
                    // Perform image subtraction
                    int r     = before.GetRed(x, y) - after.GetRed(x, y);
                    int magSq = r * r;
                    magSquare[x] = magSq;
                    byte mag = (byte)(255.0f * (magSq * 0.000015379f));
                    if (debuggingImage != null)
                    {
                        if (mag > laserThreshold)
                        {
                            debuggingImage.SetPixel(x, y, Color.FromArgb(255, mag, mag, mag));
                        }
                        else if (magSq > 0)
                        {
                            debuggingImage.SetPixel(x, y, Color.FromArgb(255, mag, mag, 0));
                        }
                        else
                        {
                            debuggingImage.SetPixel(x, y, Color.Black);
                        }
                    }

                    // Compare it against the threshold
                    if (mag > laserThreshold)
                    {
                        // The start of pixels with laser in them
                        if (laserRanges[numLaserRanges].startCol == -1)
                        {
                            laserRanges[numLaserRanges].startCol = x;
                        }
                    }
                    // The end of pixels with laser in them
                    else if (laserRanges[numLaserRanges].startCol != -1)
                    {
                        int laserWidth = x - laserRanges[numLaserRanges].startCol;
                        if (laserWidth <= m_maxLaserWidth && laserWidth >= m_minLaserWidth)
                        {
                            // If this range was real close to the previous one, merge them instead of creating a new one
                            bool wasMerged = false;
                            if (numLaserRanges > 0)
                            {
                                int rangeDistance = laserRanges[numLaserRanges].startCol - laserRanges[numLaserRanges - 1].endCol;
                                if (rangeDistance < RANGE_DISTANCE_THRESHOLD)
                                {
                                    laserRanges[numLaserRanges - 1].endCol    = x;
                                    laserRanges[numLaserRanges - 1].centerCol = (laserRanges[numLaserRanges - 1].startCol + laserRanges[numLaserRanges - 1].endCol) / 2;
                                    wasMerged = true;
                                    numMerged++;
                                }
                            }

                            // Proceed to the next laser range
                            if (!wasMerged)
                            {
                                // Add this range as a candidate
                                laserRanges[numLaserRanges].endCol    = x;
                                laserRanges[numLaserRanges].centerCol = (laserRanges[numLaserRanges].startCol + laserRanges[numLaserRanges].endCol) / 2;

                                numLaserRanges++;
                            }

                            // Reinitialize the range
                            laserRanges[numLaserRanges].startCol = -1;
                            laserRanges[numLaserRanges].endCol   = -1;
                        }
                        // There was a false positive
                        else
                        {
                            laserRanges[numLaserRanges].startCol = -1;
                        }
                    }
                }                 // foreach column


                // If we have a valid laser region
                if (numLaserRanges > 0)
                {
                    int rangeChoice = DetectBestLaserRange(laserRanges, numLaserRanges, prevLaserCol);
                    prevLaserCol = laserRanges[rangeChoice].centerCol;

                    double centerCol = DetectLaserRangeCenter(laserRanges[rangeChoice], magSquare);

                    PointF location = new PointF((float)centerCol, y);
                    laserLocations.Add(location);

                    // If this is the first row that a laser is detected in, set the firstRowLaserCol member
                    if (laserLocations.Count == 1)
                    {
                        firstRowLaserCol = laserRanges[rangeChoice].startCol;
                    }
                }
            }             // foreach row
		/// <summary>
		/// Set the row color of a Bitmap
		/// </summary>
		/// <param name="image"></param>
		/// <param name="y"></param>
		/// <param name="colors"></param>
		public static void SetRowColors(this Bitmap image, int y, Color[] colors)
		{
			LockBitmap lbmp = new LockBitmap(image);
			for (int x = 0; x < colors.Length; x++)
				lbmp.SetPixel(x, y, colors[x]);
			lbmp.UnlockBits();
		}
		private List<PointF> SubProcess(LockBitmap before, LockBitmap after, LockBitmap debuggingImage,
				 double laserThreshold)
		{
			int firstRowLaserCol = before.Width / 2;
            List<PointF> laserLocations = new List<PointF>(before.Height);


			int left_Clip = 0;
			int top_Clip = 0;
			int right_Clip = left_Clip + before.Width;
			int bottom_Clip = top_Clip + before.Height;

			int width = before.Width;
			int height = before.Height;
			int rowStep = width;

			int numLocations = 0;

			int numMerged = 0;

			// The location that we last detected a laser line
			int prevLaserCol = firstRowLaserCol;


			/** The LaserRanges for each column */
			LaserRange[] laserRanges = new LaserRange[before.Height + 1];

			double[] magSquare = new double[before.Width];

			//            for (int y = 0; y < height && numLocations < height; y++)
			for (int y = top_Clip; y < bottom_Clip && numLocations < height; y++)
			{

				// The column that the laser started and ended on
				int numLaserRanges = 0;
				laserRanges[numLaserRanges].startCol = -1;
				laserRanges[numLaserRanges].endCol = -1;

				//		        for (int  x = 0; x < rowStep; x += 1)
				/**/
				for (int x = left_Clip; x < right_Clip; x += 1)
				{
					/**/
					// Perform image subtraction
					int r = before.GetRed(x, y) - after.GetRed(x, y);
					int magSq = r * r;
					magSquare[x] = magSq;
					byte mag = (byte)(255.0f * (magSq * 0.000015379f));
					if (debuggingImage != null)
					{
						if (mag > laserThreshold)
							debuggingImage.SetPixel(x,y, Color.FromArgb(255, mag, mag, mag));
						else if (magSq > 0)
							debuggingImage.SetPixel(x,y, Color.FromArgb(255, mag, mag, 0));
						else
							debuggingImage.SetPixel(x,y, Color.Black);
					}

					// Compare it against the threshold
					if (mag > laserThreshold)
					{
						// The start of pixels with laser in them
						if (laserRanges[numLaserRanges].startCol == -1)
						{
							laserRanges[numLaserRanges].startCol = x;
						}

					}
					// The end of pixels with laser in them
					else if (laserRanges[numLaserRanges].startCol != -1)
					{
						int laserWidth = x - laserRanges[numLaserRanges].startCol;
						if (laserWidth <= m_maxLaserWidth && laserWidth >= m_minLaserWidth)
						{
							// If this range was real close to the previous one, merge them instead of creating a new one
							bool wasMerged = false;
							if (numLaserRanges > 0)
							{
								int rangeDistance = laserRanges[numLaserRanges].startCol - laserRanges[numLaserRanges - 1].endCol;
								if (rangeDistance < RANGE_DISTANCE_THRESHOLD)
								{
									laserRanges[numLaserRanges - 1].endCol = x;
									laserRanges[numLaserRanges - 1].centerCol =(laserRanges[numLaserRanges - 1].startCol + laserRanges[numLaserRanges - 1].endCol) / 2;
									wasMerged = true;
									numMerged++;
								}
							}

							// Proceed to the next laser range
							if (!wasMerged)
							{
								// Add this range as a candidate
								laserRanges[numLaserRanges].endCol = x;
								laserRanges[numLaserRanges].centerCol = (laserRanges[numLaserRanges].startCol + laserRanges[numLaserRanges].endCol) / 2;

								numLaserRanges++;
							}

							// Reinitialize the range
							laserRanges[numLaserRanges].startCol = -1;
							laserRanges[numLaserRanges].endCol = -1;
						}
						// There was a false positive
						else
						{
							laserRanges[numLaserRanges].startCol = -1;
						}
					}
				} // foreach column


				// If we have a valid laser region
				if (numLaserRanges > 0)
				{
					int rangeChoice = DetectBestLaserRange(laserRanges, numLaserRanges, prevLaserCol);
					prevLaserCol = laserRanges[rangeChoice].centerCol;

					double centerCol = DetectLaserRangeCenter(laserRanges[rangeChoice],magSquare);

                    PointF location = new PointF((float)centerCol, y);
					laserLocations.Add(location);

					// If this is the first row that a laser is detected in, set the firstRowLaserCol member
					if (laserLocations.Count == 1)
					{
						firstRowLaserCol = laserRanges[rangeChoice].startCol;
					}

				}
			} // foreach row