Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LinearTransform.cs
1using System;
4
6{
10 public static partial class LinearTransformationOperations
11 {
18 {
19 float[] v = M.Data;
20 int i, c = M.Data.Length;
21 DoubleNumber[] Elements = new DoubleNumber[c];
22
23 for (i = 0; i < c; i++)
24 Elements[i] = new DoubleNumber(v[i]);
25
26 return new DoubleMatrix(M.Height, M.Width, Elements);
27 }
28
37 public static Matrix<float> LinearTransform(this Matrix<float> M, Matrix<float> Transform, int Width, int Height)
38 {
39 return LinearTransform(M, Transform.ToDoubleMatrix(), Width, Height);
40 }
41
50 public static Matrix<float> LinearTransform(this Matrix<float> M, DoubleMatrix Transform, int Width, int Height)
51 {
52 if (Transform.Columns != 3 || Transform.Rows != 3)
53 throw new ArgumentException("Invalid transform matrix.", nameof(Transform));
54
55 DoubleMatrix T = (DoubleMatrix)Transform.Invert();
56 double[,] a = T.Values;
57 float a00 = (float)a[0, 0];
58 float a10 = (float)a[0, 1];
59 float a20 = (float)a[0, 2];
60 float a01 = (float)a[1, 0];
61 float a11 = (float)a[1, 1];
62 float a21 = (float)a[1, 2];
63 float a02 = (float)a[2, 0];
64 float a12 = (float)a[2, 1];
65 float a22 = (float)a[2, 2];
66
67 Matrix<float> Result = new Matrix<float>(Width, Height);
68 float[] Dest = Result.Data;
69 int DestIndex = 0;
70 int x, y;
71 int w = M.Width;
72 int h = M.Height;
73 float x0, y0, t0, v0, v1, v2, v3, w0, w1;
74 int ix0, iy0;
75 float fx0, fy0;
76
77 for (y = 0; y < Height; y++)
78 {
79 for (x = 0; x < Width; x++)
80 {
81 x0 = a00 * x + a10 * y + a20;
82 y0 = a01 * x + a11 * y + a21;
83 t0 = a02 * x + a12 * y + a22;
84
85 if (t0 != 0 && t0 != 1)
86 {
87 x0 /= t0;
88 y0 /= t0;
89 }
90
91 if (x0 >= 0 && x0 < w && y0 >= 0 && y0 < h)
92 {
93 ix0 = (int)x0;
94 iy0 = (int)y0;
95 fx0 = x0 - ix0;
96 fy0 = y0 - iy0;
97
98 v0 = M[ix0, iy0];
99
100 if (fx0 == 0 && fy0 == 0)
101 Dest[DestIndex] = v0;
102 else
103 {
104 if (ix0 + 1 < w)
105 v1 = M[ix0 + 1, iy0];
106 else
107 v1 = v0;
108
109 if (iy0 + 1 < h)
110 {
111 v2 = M[ix0, iy0 + 1];
112
113 if (ix0 + 1 < w)
114 v3 = M[ix0 + 1, iy0 + 1];
115 else
116 v3 = v0;
117 }
118 else
119 v2 = v3 = v0;
120
121 w0 = v1 * fx0 + v0 * (1 - fx0);
122 w1 = v3 * fx0 + v2 * (1 - fx0);
123
124 Dest[DestIndex] = w1 * fy0 + w0 * (1 - fy0);
125 }
126 }
127
128 DestIndex++;
129 }
130 }
131
132 return Result;
133 }
134
135 }
136}
Implements a Matrix, basic component for computations in Image Processing and Computer Vision.
Definition: Matrix.cs:12
int Height
Height of matrix (number of rows)
Definition: Matrix.cs:136
T[] Data
Underlying data on which the matrix is defined.
Definition: Matrix.cs:114
int Width
Width of matrix (number of columns)
Definition: Matrix.cs:120
Static class for Linear Transformation Operations, implemented as extensions.
static DoubleMatrix ToDoubleMatrix(this Matrix< float > M)
Converts a CV Matrix to a script Matrix.
static Matrix< float > LinearTransform(this Matrix< float > M, Matrix< float > Transform, int Width, int Height)
Performs an image convolution on a matrix.
static Matrix< float > LinearTransform(this Matrix< float > M, DoubleMatrix Transform, int Width, int Height)
Performs an image convolution on a matrix.
double[,] Values
Matrix element values.
Definition: DoubleMatrix.cs:56
override IRingElement Invert()
Inverts the element, if possible.