internal static MatrixOp Jet(ImageTypes type, TwoDimentionObjectBase obj, double max, double min = 0) { MatrixOp matrixOp; switch (type) { case ImageTypes.RgbPixel: matrixOp = Dlib.Jet(obj as Array2D <RgbPixel>, max, min); break; case ImageTypes.RgbAlphaPixel: matrixOp = Dlib.Jet(obj as Array2D <RgbAlphaPixel>, max, min); break; case ImageTypes.UInt8: matrixOp = Dlib.Jet(obj as Array2D <byte>, max, min); break; case ImageTypes.UInt16: matrixOp = Dlib.Jet(obj as Array2D <ushort>, max, min); break; case ImageTypes.UInt32: matrixOp = Dlib.Jet(obj as Array2D <uint>, max, min); break; case ImageTypes.Int8: matrixOp = Dlib.Jet(obj as Array2D <sbyte>, max, min); break; case ImageTypes.Int16: matrixOp = Dlib.Jet(obj as Array2D <short>, max, min); break; case ImageTypes.Int32: matrixOp = Dlib.Jet(obj as Array2D <int>, max, min); break; case ImageTypes.HsiPixel: matrixOp = Dlib.Jet(obj as Array2D <HsiPixel>, max, min); break; case ImageTypes.Float: matrixOp = Dlib.Jet(obj as Array2D <float>, max, min); break; case ImageTypes.Double: matrixOp = Dlib.Jet(obj as Array2D <double>, max, min); break; default: throw new ArgumentOutOfRangeException(); } return(matrixOp); }
private void Assign(TwoDimentionObjectBase obj, int[] array) { if (obj is Matrix <sbyte> sbyteMatrix) { sbyteMatrix.Assign(array.Select(i => (sbyte)i).ToArray()); return; } if (obj is Matrix <short> shortMatrix) { shortMatrix.Assign(array.Select(i => (short)i).ToArray()); return; } if (obj is Matrix <int> intMatrix) { intMatrix.Assign(array.Select(i => (int)i).ToArray()); return; } if (obj is Matrix <byte> byteMatrix) { byteMatrix.Assign(array.Select(i => (byte)i).ToArray()); return; } if (obj is Matrix <ushort> ushortMatrix) { ushortMatrix.Assign(array.Select(i => (ushort)i).ToArray()); return; } if (obj is Matrix <uint> uintMatrix) { uintMatrix.Assign(array.Select(i => (uint)i).ToArray()); return; } if (obj is Matrix <float> floatMatrix) { floatMatrix.Assign(array.Select(i => (float)i).ToArray()); return; } if (obj is Matrix <double> doubleMatrix) { doubleMatrix.Assign(array.Select(i => (double)i).ToArray()); return; } throw new NotSupportedException(); }
internal static MatrixOp Heatmap(ImageTypes type, TwoDimentionObjectBase obj) { MatrixOp matrixOp; switch (type) { case ImageTypes.RgbPixel: matrixOp = Dlib.Heatmap(obj as Array2D <RgbPixel>); break; case ImageTypes.RgbAlphaPixel: matrixOp = Dlib.Heatmap(obj as Array2D <RgbAlphaPixel>); break; case ImageTypes.UInt8: matrixOp = Dlib.Heatmap(obj as Array2D <byte>); break; case ImageTypes.UInt16: matrixOp = Dlib.Heatmap(obj as Array2D <ushort>); break; case ImageTypes.HsiPixel: matrixOp = Dlib.Heatmap(obj as Array2D <HsiPixel>); break; case ImageTypes.Float: matrixOp = Dlib.Heatmap(obj as Array2D <float>); break; case ImageTypes.Double: matrixOp = Dlib.Heatmap(obj as Array2D <double>); break; default: throw new ArgumentOutOfRangeException(); } return(matrixOp); }
public void AssignImage() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true } }; var type = this.GetType().Name; foreach (var input in tests) { foreach (var output in tests) { TwoDimentionObjectBase outObj = null; TwoDimentionObjectBase inObj = null; var expect = input.ExpectResult && output.ExpectResult; try { var inIage = DlibTest.LoadImage(input.Type, path); inObj = inIage; try { var outImage = Array2DTest.CreateArray2D(output.Type); outObj = outImage; Dlib.AssignImage(inIage, outImage); if (!expect) { Assert.Fail($"AssignImage should throw excption for InputType: {input.Type}, OutputType: {output.Type}"); } else { Assert.AreEqual(inIage.Columns, outImage.Columns); Assert.AreEqual(inIage.Rows, outImage.Rows); Dlib.SaveBmp(outImage, $"{Path.Combine(this.GetOutDir(type, "AssignImage"), $"{LoadTarget}_{input.Type}_{output.Type}.bmp")}"); } } catch (ArgumentException) { if (!expect) { Console.WriteLine("OK"); } else { throw; } } catch (NotSupportedException) { if (!expect) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute AssignImage to InputType: {input.Type}, OutputType: {output.Type}"); throw; } finally { if (outObj != null) { this.DisposeAndCheckDisposedState(outObj); } if (inObj != null) { this.DisposeAndCheckDisposedState(inObj); } } } } }
public void SobelEdgeDetector() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.RgbPixel, ExpectResult = false }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.UInt8, ExpectResult = false }, new { Type = ImageTypes.UInt16, ExpectResult = false }, new { Type = ImageTypes.HsiPixel, ExpectResult = false }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true } }; var type = this.GetType().Name; foreach (ImageTypes inputType in Enum.GetValues(typeof(ImageTypes))) { if (inputType == ImageTypes.Matrix) { continue; } foreach (var test in tests) { TwoDimentionObjectBase imageObj = null; TwoDimentionObjectBase horzObj = null; TwoDimentionObjectBase vertObj = null; try { var image = DlibTest.LoadImage(inputType, path); imageObj = image; var horz = Array2DTest.CreateArray2D(test.Type); horzObj = horz; var vert = Array2DTest.CreateArray2D(test.Type); vertObj = vert; try { Dlib.SobelEdgeDetector(image, horz, vert); if (!test.ExpectResult) { Assert.Fail($"SobelEdgeDetector should throw excption for InputType: {inputType}, Type: {test.Type}."); } else { Dlib.SaveBmp(horz, $"{Path.Combine(this.GetOutDir(type, "SobelEdgeDetector"), $"{LoadTarget}_{inputType}_{test.Type}_horz.bmp")}"); Dlib.SaveBmp(vert, $"{Path.Combine(this.GetOutDir(type, "SobelEdgeDetector"), $"{LoadTarget}_{inputType}_{test.Type}_vert.bmp")}"); } } catch (Exception) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute SobelEdgeDetector to InputType: {inputType}, Type: {test.Type}."); throw; } finally { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (horzObj != null) { this.DisposeAndCheckDisposedState(horzObj); } if (vertObj != null) { this.DisposeAndCheckDisposedState(vertObj); } } } } }
public void SaveJpegThrowException() { var path = this.GetDataFile($"{LoadTarget}.jpg"); var tests = new[] { new { Type = ImageTypes.RgbPixel, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.UInt8, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.UInt16, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.HsiPixel, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.Float, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.Double, ExpectResult = true, Quality = 0 }, new { Type = ImageTypes.RgbPixel, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.UInt8, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.UInt16, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.HsiPixel, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.Float, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.Double, ExpectResult = true, Quality = 100 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.UInt8, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.UInt16, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.Float, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.Double, ExpectResult = false, Quality = -1 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.UInt8, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.UInt16, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.Float, ExpectResult = false, Quality = 101 }, new { Type = ImageTypes.Double, ExpectResult = false, Quality = 101 } }; var type = this.GetType().Name; foreach (var test in tests) { TwoDimentionObjectBase dimentionObject = null; var filepath = $"{Path.Combine(this.GetOutDir(type, "SaveJpegThrowException"), $"{LoadTarget}_{test.Type}_{test.Quality}.jpg")}"; try { switch (test.Type) { case ImageTypes.RgbPixel: { var image = Dlib.LoadImage <RgbPixel>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.RgbAlphaPixel: { var image = Dlib.LoadImage <RgbAlphaPixel>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.UInt8: { var image = Dlib.LoadImage <byte>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.UInt16: { var image = Dlib.LoadImage <ushort>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.HsiPixel: { var image = Dlib.LoadImage <HsiPixel>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.Float: { var image = Dlib.LoadImage <float>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; case ImageTypes.Double: { var image = Dlib.LoadImage <double>(path.FullName); dimentionObject = image; Dlib.SaveJpeg(image, filepath, test.Quality); this.DisposeAndCheckDisposedState(image); } break; default: throw new ArgumentOutOfRangeException(); } if (!test.ExpectResult) { Assert.Fail($"Failed to save {test.Type} for quality: {test.Quality}."); } } catch { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } finally { if (dimentionObject != null && !dimentionObject.IsDisposed) { this.DisposeAndCheckDisposedState(dimentionObject); } } } }
public void Create1() { var tests = new[] { new { Type = MatrixElementTypes.RgbPixel, ExpectResult = true }, new { Type = MatrixElementTypes.RgbAlphaPixel, ExpectResult = true }, new { Type = MatrixElementTypes.HsiPixel, ExpectResult = true }, new { Type = MatrixElementTypes.UInt8, ExpectResult = true }, new { Type = MatrixElementTypes.UInt16, ExpectResult = true }, new { Type = MatrixElementTypes.UInt32, ExpectResult = true }, new { Type = MatrixElementTypes.Int8, ExpectResult = true }, new { Type = MatrixElementTypes.Int16, ExpectResult = true }, new { Type = MatrixElementTypes.Int32, ExpectResult = true }, new { Type = MatrixElementTypes.Float, ExpectResult = true }, new { Type = MatrixElementTypes.Double, ExpectResult = true } }; var rules = new[] { new { Column = 0, Row = 0, ExpectResult = true }, new { Column = 0, Row = -1, ExpectResult = false }, new { Column = -1, Row = 0, ExpectResult = false }, new { Column = -1, Row = -1, ExpectResult = false }, new { Column = 1, Row = 0, ExpectResult = true }, new { Column = 0, Row = 1, ExpectResult = true }, new { Column = 1, Row = 1, ExpectResult = true }, new { Column = 10, Row = 0, ExpectResult = true }, new { Column = 0, Row = 10, ExpectResult = true } }; foreach (var r in rules) { foreach (var test in tests) { TwoDimentionObjectBase matrix = null; try { if (r.ExpectResult) { matrix = CreateMatrix(test.Type, r.Row, r.Column); } else { try { matrix = CreateMatrix(test.Type, r.Row, r.Column); Assert.Fail($"{matrix.GetType().Name} should throw excption for Type: {test.Type}, Row: {r.Row}, Column: {r.Column}."); } catch { Console.WriteLine("OK"); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to create for Type: {test.Type}, Row: {r.Row}, Column: {r.Column}."); throw; } finally { if (matrix != null) { this.DisposeAndCheckDisposedState(matrix); } } } } }
private void CheckRowsColumnsSize(TwoDimentionObjectBase obj, int row, int columnn) { if (obj is Matrix <sbyte> sbyteMatrix) { Assert.AreEqual(sbyteMatrix.Rows, row); Assert.AreEqual(sbyteMatrix.Columns, columnn); Assert.AreEqual(sbyteMatrix.Size, row * columnn); return; } if (obj is Matrix <short> shortMatrix) { Assert.AreEqual(shortMatrix.Rows, row); Assert.AreEqual(shortMatrix.Columns, columnn); Assert.AreEqual(shortMatrix.Size, row * columnn); return; } if (obj is Matrix <int> intMatrix) { Assert.AreEqual(intMatrix.Rows, row); Assert.AreEqual(intMatrix.Columns, columnn); Assert.AreEqual(intMatrix.Size, row * columnn); return; } if (obj is Matrix <byte> byteMatrix) { Assert.AreEqual(byteMatrix.Rows, row); Assert.AreEqual(byteMatrix.Columns, columnn); Assert.AreEqual(byteMatrix.Size, row * columnn); return; } if (obj is Matrix <ushort> ushortMatrix) { Assert.AreEqual(ushortMatrix.Rows, row); Assert.AreEqual(ushortMatrix.Columns, columnn); Assert.AreEqual(ushortMatrix.Size, row * columnn); return; } if (obj is Matrix <uint> uintMatrix) { Assert.AreEqual(uintMatrix.Rows, row); Assert.AreEqual(uintMatrix.Columns, columnn); Assert.AreEqual(uintMatrix.Size, row * columnn); return; } if (obj is Matrix <float> floatMatrix) { Assert.AreEqual(floatMatrix.Rows, row); Assert.AreEqual(floatMatrix.Columns, columnn); Assert.AreEqual(floatMatrix.Size, row * columnn); return; } if (obj is Matrix <double> doubleMatrix) { Assert.AreEqual(doubleMatrix.Rows, row); Assert.AreEqual(doubleMatrix.Columns, columnn); Assert.AreEqual(doubleMatrix.Size, row * columnn); return; } if (obj is Matrix <RgbPixel> rgbPixelMatrix) { Assert.AreEqual(rgbPixelMatrix.Rows, row); Assert.AreEqual(rgbPixelMatrix.Columns, columnn); Assert.AreEqual(rgbPixelMatrix.Size, row * columnn); return; } if (obj is Matrix <RgbAlphaPixel> rgbAlphaPixelMatrix) { Assert.AreEqual(rgbAlphaPixelMatrix.Rows, row); Assert.AreEqual(rgbAlphaPixelMatrix.Columns, columnn); Assert.AreEqual(rgbAlphaPixelMatrix.Size, row * columnn); return; } if (obj is Matrix <HsiPixel> hsiPicelMatrix) { Assert.AreEqual(hsiPicelMatrix.Rows, row); Assert.AreEqual(hsiPicelMatrix.Columns, columnn); Assert.AreEqual(hsiPicelMatrix.Size, row * columnn); return; } throw new NotSupportedException(); }
public void ToStringTest() { var array = new[] { 1, 2, 6, 5, 3, 6, 4, 5, 0 }; const string answer = "1 2 6 \n5 3 6 \n4 5 0 \n"; const string answer1 = "\u0001 \u0002 \u0006 \n\u0005 \u0003 \u0006 \n\u0004 \u0005 "; var tests = new[] { new { Type = MatrixElementTypes.RgbPixel, ExpectResult = false, Answer = "" }, new { Type = MatrixElementTypes.RgbAlphaPixel, ExpectResult = false, Answer = "" }, new { Type = MatrixElementTypes.HsiPixel, ExpectResult = false, Answer = "" }, new { Type = MatrixElementTypes.UInt8, ExpectResult = true, Answer = answer1 }, new { Type = MatrixElementTypes.UInt16, ExpectResult = true, Answer = answer }, new { Type = MatrixElementTypes.UInt32, ExpectResult = true, Answer = answer }, new { Type = MatrixElementTypes.Int8, ExpectResult = true, Answer = answer1 }, new { Type = MatrixElementTypes.Int16, ExpectResult = true, Answer = answer }, new { Type = MatrixElementTypes.Int32, ExpectResult = true, Answer = answer }, new { Type = MatrixElementTypes.Float, ExpectResult = true, Answer = answer }, new { Type = MatrixElementTypes.Double, ExpectResult = true, Answer = answer } }; foreach (var test in tests) { TwoDimentionObjectBase matrix = null; try { if (test.ExpectResult) { matrix = CreateMatrix(test.Type, 3, 3); this.Assign(matrix, array); var str = matrix.ToString(); Assert.AreEqual(test.Answer, str); } else { try { matrix = CreateMatrix(test.Type, 3, 3); var str = matrix.ToString(); Assert.Fail($"{matrix.GetType().Name} should throw excption for Type: {test.Type}."); } catch { Console.WriteLine("OK"); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to create for Type: {test.Type}."); throw; } finally { if (matrix != null) { this.DisposeAndCheckDisposedState(matrix); } } } }
private void ExecuteMaxCostAssignment(TwoDimentionObjectBase obj) { if (obj is Matrix <sbyte> sbyteMatrix) { Dlib.MaxCostAssignment(sbyteMatrix); return; } if (obj is Matrix <short> shortMatrix) { Dlib.MaxCostAssignment(shortMatrix); return; } if (obj is Matrix <int> intMatrix) { Dlib.MaxCostAssignment(intMatrix); return; } if (obj is Matrix <byte> byteMatrix) { Dlib.MaxCostAssignment(byteMatrix); return; } if (obj is Matrix <ushort> ushortMatrix) { Dlib.MaxCostAssignment(ushortMatrix); return; } if (obj is Matrix <uint> uintMatrix) { Dlib.MaxCostAssignment(uintMatrix); return; } if (obj is Matrix <float> floatMatrix) { Dlib.MaxCostAssignment(floatMatrix); return; } if (obj is Matrix <double> doubleMatrix) { Dlib.MaxCostAssignment(doubleMatrix); return; } if (obj is Matrix <RgbPixel> rgbPixelMatrix) { Dlib.MaxCostAssignment(rgbPixelMatrix); return; } if (obj is Matrix <RgbAlphaPixel> rgbAlphaPixelMatrix) { Dlib.MaxCostAssignment(rgbAlphaPixelMatrix); return; } if (obj is Matrix <HsiPixel> hsiPicelMatrix) { Dlib.MaxCostAssignment(hsiPicelMatrix); return; } throw new NotSupportedException(); }