public void CollectActivePixelsTest() { // create grayscale image UnmanagedImage image24 = UnmanagedImage.Create(7, 7, PixelFormat.Format24bppRgb); UnmanagedImage image8 = UnmanagedImage.Create(7, 7, PixelFormat.Format8bppIndexed); Drawing.FillRectangle(image24, new Rectangle(1, 1, 5, 5), Color.FromArgb(255, 255, 255)); Drawing.FillRectangle(image24, new Rectangle(2, 2, 3, 3), Color.FromArgb(1, 1, 1)); Drawing.FillRectangle(image24, new Rectangle(3, 3, 1, 1), Color.FromArgb(0, 0, 0)); Drawing.FillRectangle(image8, new Rectangle(1, 1, 5, 5), Color.FromArgb(255, 255, 255)); Drawing.FillRectangle(image8, new Rectangle(2, 2, 3, 3), Color.FromArgb(1, 1, 1)); Drawing.FillRectangle(image8, new Rectangle(3, 3, 1, 1), Color.FromArgb(0, 0, 0)); List <IntPoint> pixels24 = image24.CollectActivePixels(); List <IntPoint> pixels8 = image8.CollectActivePixels(); Assert.AreEqual(pixels24.Count, 24); Assert.AreEqual(pixels8.Count, 24); for (int i = 1; i < 6; i++) { for (int j = 1; j < 6; j++) { if ((i == 3) && (j == 3)) { continue; } Assert.IsTrue(pixels24.Contains(new IntPoint(j, i))); Assert.IsTrue(pixels8.Contains(new IntPoint(j, i))); } } pixels24 = image24.CollectActivePixels(new Rectangle(1, 0, 5, 4)); pixels8 = image8.CollectActivePixels(new Rectangle(1, 0, 5, 4)); Assert.AreEqual(pixels24.Count, 14); Assert.AreEqual(pixels8.Count, 14); for (int i = 1; i < 4; i++) { for (int j = 1; j < 6; j++) { if ((i == 3) && (j == 3)) { continue; } Assert.IsTrue(pixels24.Contains(new IntPoint(j, i))); Assert.IsTrue(pixels8.Contains(new IntPoint(j, i))); } } }
public void SetPixelsTest(PixelFormat pixelFormat) { UnmanagedImage image = UnmanagedImage.Create(320, 240, pixelFormat); Color color = Color.White; List <IntPoint> points = new List <IntPoint>(); points.Add(new IntPoint(0, 0)); points.Add(new IntPoint(319, 0)); points.Add(new IntPoint(0, 239)); points.Add(new IntPoint(319, 239)); points.Add(new IntPoint(160, 120)); points.Add(new IntPoint(-1, -1)); points.Add(new IntPoint(320, 0)); points.Add(new IntPoint(0, 240)); points.Add(new IntPoint(320, 240)); image.SetPixels(points, color); List <IntPoint> pixels = image.CollectActivePixels(); Assert.AreEqual(5, pixels.Count); Assert.IsTrue(pixels.Contains(new IntPoint(0, 0))); Assert.IsTrue(pixels.Contains(new IntPoint(319, 0))); Assert.IsTrue(pixels.Contains(new IntPoint(0, 239))); Assert.IsTrue(pixels.Contains(new IntPoint(319, 239))); Assert.IsTrue(pixels.Contains(new IntPoint(160, 120))); }
public void SetPixelTest(PixelFormat pixelFormat) { UnmanagedImage image = UnmanagedImage.Create(320, 240, pixelFormat); Color color = Color.White; byte value = 255; image.SetPixel(0, 0, color); image.SetPixel(319, 0, color); image.SetPixel(0, 239, color); image.SetPixel(319, 239, value); image.SetPixel(160, 120, value); image.SetPixel(-1, -1, color); image.SetPixel(320, 0, color); image.SetPixel(0, 240, value); image.SetPixel(320, 240, value); List <IntPoint> pixels = image.CollectActivePixels(); Assert.AreEqual(5, pixels.Count); Assert.IsTrue(pixels.Contains(new IntPoint(0, 0))); Assert.IsTrue(pixels.Contains(new IntPoint(319, 0))); Assert.IsTrue(pixels.Contains(new IntPoint(0, 239))); Assert.IsTrue(pixels.Contains(new IntPoint(319, 239))); Assert.IsTrue(pixels.Contains(new IntPoint(160, 120))); }
public void ConvertTest2() { // Load a test image BitmapSource sourceImage = TestTools.GetImage("image1.bmp"); // Make sure values are binary sourceImage = new Threshold().Apply(sourceImage); // Create the converters var imageToMatrix = new BitmapSourceToMatrix(); var matrixToImage = new MatrixToBitmapSource(); // Convert to matrix double[,] matrix; // initialization is not needed imageToMatrix.Convert(sourceImage, out matrix); // Revert to image BitmapSource resultImage; // initialization is not needed matrixToImage.Convert(matrix, out resultImage); Assert.AreEqual(PixelFormats.Gray32Float, resultImage.Format); UnmanagedImage img1 = sourceImage.ToUnmanagedImage(); UnmanagedImage img2 = resultImage.ToUnmanagedImage(); List <IntPoint> p1 = img1.CollectActivePixels(); List <IntPoint> p2 = img2.CollectActivePixels(); bool equals = new HashSet <IntPoint>(p1).SetEquals(p2); Assert.IsTrue(equals); }
public void SetPixelTestUnsupported(PixelFormat pixelFormat) { UnmanagedImage image = UnmanagedImage.Create(320, 240, pixelFormat); Color color = Color.White; byte value = 255; image.SetPixel(0, 0, color); image.SetPixel(319, 0, color); image.SetPixel(0, 239, color); image.SetPixel(319, 239, value); image.SetPixel(160, 120, value); image.SetPixel(-1, -1, color); image.SetPixel(320, 0, color); image.SetPixel(0, 240, value); image.SetPixel(320, 240, value); Assert.Throws <UnsupportedImageFormatException>(() => image.CollectActivePixels(), ""); }
public void ConvertTest() { var target = new MatrixToBitmapSource(); byte[,] input = { { 0, 0, 0 }, { 0, 128, 0 }, { 0, 0, 128 }, }; UnmanagedImage bitmap = input.ToBitmapSource().ToUnmanagedImage(); var pixels = bitmap.CollectActivePixels(); Assert.AreEqual(2, pixels.Count); Assert.IsTrue(pixels.Contains(new IntPoint(1, 1))); Assert.IsTrue(pixels.Contains(new IntPoint(2, 2))); }
public void ConvertTest2() { // Load a test image Bitmap sourceImage = Accord.Imaging.Image.Clone(Properties.Resources.image1); // Make sure values are binary new Threshold().ApplyInPlace(sourceImage); // Create the converters ImageToMatrix imageToMatrix = new ImageToMatrix() { Min = 0, Max = 255 }; MatrixToImage matrixToImage = new MatrixToImage() { Min = 0, Max = 255 }; // Convert to matrix double[,] matrix; // initialization is not needed imageToMatrix.Convert(sourceImage, out matrix); // Revert to image Bitmap resultImage; // initialization is not needed matrixToImage.Convert(matrix, out resultImage); // Show both images, which should be equal // ImageBox.Show(sourceImage, PictureBoxSizeMode.Zoom); // ImageBox.Show(resultImage, PictureBoxSizeMode.Zoom); UnmanagedImage img1 = UnmanagedImage.FromManagedImage(sourceImage); UnmanagedImage img2 = UnmanagedImage.FromManagedImage(resultImage); List <IntPoint> p1 = img1.CollectActivePixels(); List <IntPoint> p2 = img2.CollectActivePixels(); bool equals = new HashSet <IntPoint>(p1).SetEquals(p2); Assert.IsTrue(equals); }
public void ToManagedImageTest(PixelFormat pixelFormat, int x, int y, byte red, byte green, byte blue) { UnmanagedImage image = UnmanagedImage.Create(320, 240, pixelFormat); image.SetPixel(new IntPoint(x, y), Color.FromArgb(255, red, green, blue)); Bitmap bitmap = image.ToManagedImage(); // check colors of pixels Assert.AreEqual(Color.FromArgb(255, red, green, blue), bitmap.GetPixel(x, y)); // make sure there are only 1 pixel UnmanagedImage temp = UnmanagedImage.FromManagedImage(bitmap); List <IntPoint> pixels = temp.CollectActivePixels(); Assert.AreEqual(1, pixels.Count); image.Dispose(); bitmap.Dispose(); temp.Dispose(); }
/// <summary> /// Process the filter on the specified image. /// </summary> /// /// <param name="sourceData">Source image data.</param> /// <param name="destinationData">Destination image data.</param> /// <param name="rect">Image rectangle for processing by the filter.</param> /// /// <exception cref="InvalidImagePropertiesException">Processing rectangle mast be at least 4x4 in size.</exception> /// protected override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData, Rectangle rect) { if ((rect.Width < 4) || (rect.Height < 4)) { throw new InvalidImagePropertiesException("Processing rectangle mast be at least 4x4 in size."); } List <byte[, ]> patterns = Get3X3CornerPatterns(); var hotPoints = new List <IntPoint>(); foreach (var point in sourceData.CollectActivePixels()) { byte[,] matrix3X3 = GetNeighborValues(point, 1, ref sourceData); foreach (byte[,] pattern in patterns) { if (matrix3X3.IsEqual(pattern)) { hotPoints.Add(point); } } } var blackList = new List <int>(); var pairPoints = new List <IntPoint>(); for (int i = 0; i < hotPoints.Count; i++) { float distance = float.MaxValue; int index = -1; for (int j = i + 1; j < hotPoints.Count; j++) { if (blackList.Contains(j)) { continue; } bool isValidPoint = true; foreach (var value in sourceData.Collect8bppPixelValues(GetLineInPoints(hotPoints[i], hotPoints[j]))) { if (value == byte.MaxValue) { isValidPoint = false; break; } } if (!isValidPoint) { continue; } float d = hotPoints[i].DistanceTo(hotPoints[j]); if (d > distance) { continue; } index = j; distance = d; } if (index == -1) { continue; } blackList.Add(index); pairPoints.Add(hotPoints[i]); pairPoints.Add(hotPoints[index]); } for (var i = 0; i < pairPoints.Count; i++) { Drawing.Line(destinationData, pairPoints[i], pairPoints[++i], Color.White); } }
// Slow version (eventually pictures could be resized for more performance) private void FindPersonPosition(UnmanagedImage frameImage) { var stopwatch = Stopwatch.StartNew(); Difference diffFilter = new Difference(_baseImage); UnmanagedImage diffImg = diffFilter.Apply(frameImage); //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff.png"); // TODO ev. ColorFilter Grayscale grayFilter = new GrayscaleBT709(); diffImg = grayFilter.Apply(diffImg); //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray.png"); Threshold thresholdFilter = new Threshold(30); thresholdFilter.ApplyInPlace(diffImg); //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray-thres.png"); Median medianFilter = new Median(3); medianFilter.ApplyInPlace(diffImg); //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray-thres-med.png"); // Find Person Borders (TODO ev. Performance Boost, wenn CollectActivePixels vermieden wird) // per Iteration durch Pixeldaten koennte ev. die groesste und die kleinste x-Koordinate schneller // gefunden werden.. List <AForge.IntPoint> whites = diffImg.CollectActivePixels(BOTTOM_BASE_STRIPE); int minX = whites.Min(obj => obj.X); int maxX = whites.Max(obj => obj.X); // Schneller als MS Algorithmus fuer Min und Max // (auch wenn es assymptotisch gleich schnell sein muesste) //int minX = int.MaxValue; //int maxX = int.MinValue; //foreach(AForge.IntPoint whitePixel in whites) //{ // //if (whitePixel.X < minX) // //{ // // minX = whitePixel.X; // //} // if (whitePixel.X > maxX) // { // maxX = whitePixel.X; // } //} var elapsedStage1 = stopwatch.ElapsedMilliseconds; Debug.WriteLine("Person is this fat: {0}, {1}", minX, maxX); // Find Arm whites = diffImg.CollectActivePixels(new Rectangle(maxX, 0, 640 - maxX, 480 - BOTTOM_BASE_STRIPE.Height)); maxX = whites.Max(obj => obj.X); var armPoint = whites.First(obj => obj.X == maxX); Debug.WriteLine("Persons arm is here: {0}, {1}", armPoint.X, armPoint.Y); Bitmap b = frameImage.ToManagedImage(); using (Graphics g = Graphics.FromImage(b)) { g.DrawEllipse(Pens.Red, armPoint.X, armPoint.Y, 5, 5); b.Save(@"c:\temp\recognition\gach.png"); } Debug.WriteLine("Time: {0}", elapsedStage1); stopwatch.Stop(); }