static double[] Reduce(cv.Mat luMatrix, double[] b) // helper { int n = luMatrix.Rows; double[] x = new double[n]; for (int i = 0; i < n; ++i) { x[i] = b[i]; } for (int i = 1; i < n; ++i) { double sum = x[i]; for (int j = 0; j < i; ++j) { sum -= luMatrix.At <double>(i, j) * x[j]; } x[i] = sum; } x[n - 1] /= luMatrix.At <double>(n - 1, n - 1); for (int i = n - 2; i >= 0; --i) { double sum = x[i]; for (int j = i + 1; j < n; ++j) { sum -= luMatrix.At <double>(i, j) * x[j]; } x[i] = sum / luMatrix.At <double>(i, i); } return(x); }
public double[] GetRadiusCenter3P() { int dataSize = positionInfo[annotIdx].points.Count; double a, b, r; cv.Mat A = cv.Mat.Zeros(dataSize, 3, cv.MatType.CV_64FC1); cv.Mat B = cv.Mat.Zeros(dataSize, 1, cv.MatType.CV_64FC1); cv.Mat X = cv.Mat.Zeros(dataSize, 1, cv.MatType.CV_64FC1); for (int i = 0; i < dataSize; i++) { A.Set <double>(i, 0, positionInfo[annotIdx].points[i].x); A.Set <double>(i, 1, positionInfo[annotIdx].points[i].z); A.Set <double>(i, 2, 1); B.Set <double>(i, -Math.Pow(positionInfo[annotIdx].points[i].x, 2) - Math.Pow(positionInfo[annotIdx].points[i].z, 2)); } cv.Cv2.Solve(A, B, X, cv.DecompTypes.SVD); a = -X.At <double>(0, 0) / 2; b = -X.At <double>(1, 0) / 2; r = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2) - X.At <double>(2, 0)); double[] data = { a, b, r }; return(data); }
public void SetData(cv.Mat input) { //if (xSize != -1 || zSize != -1) //{ // surfaceMeshRenderableSeries.DataSeries.Clear(); //} zMap = new cv.Mat(new cv.Size(input.Width, input.Height), cv.MatType.CV_32FC1); input.ConvertTo(zMap, cv.MatType.CV_32FC1); //Init(zMap.Rows, zMap.Cols); Init(zMap.Rows, zMap.Cols); Parallel.For(0, xSize, x => { for (int z = 0; z < zSize; ++z) { MeshDataSeries[z, x] = zMap.At <float>(x, z); if (yMax < zMap.Get <float>(x, z)) { yMax = zMap.Get <float>(x, z); } } }); double min = 0.0f, max = 0.0f; zMap.MinMaxLoc(out min, out max); surfaceMeshRenderableSeries.Maximum = max; surfaceMeshRenderableSeries.Minimum = min; //backgroundSurfaceMesh.IsVisible = false; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Vector3[,] v = new Vector3[mat.Height, mat.Width]; for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b vyx = mat.At <Vec3b>(yi, xi); v[yi, xi][0] = vyx[0]; v[yi, xi][1] = vyx[1]; v[yi, xi][2] = vyx[2]; } } v = Median(v, mat.Height, mat.Width); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b vyx = new Vec3b(); vyx[0] = (byte)v[yi, xi][0]; vyx[1] = (byte)v[yi, xi][1]; vyx[2] = (byte)v[yi, xi][2]; mat.Set <Vec3b>(yi, xi, vyx); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); Debug.Log(v[0]); float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0]; if (gr < 128) { gr = 0; } else { gr = 255; } v[0] = (byte)gr; v[1] = (byte)gr; v[2] = (byte)gr; mat.Set <Vec3b>(yi, xi, v); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
static cv.Mat MatProduct(cv.Mat matA, cv.Mat matB) { int aRows = matA.Rows; int aCols = matA.Cols; int bRows = matB.Rows; int bCols = matB.Cols; if (aCols != bRows) { throw new Exception("Non-conformable matrices"); } cv.Mat result = new cv.Mat(aRows, bCols, cv.MatType.CV_64F, 0); for (int i = 0; i < aRows; ++i) // each row of A { for (int j = 0; j < bCols; ++j) // each col of B { for (int k = 0; k < aCols; ++k) // could use bRows { result.Set <double>(i, j, result.At <double>(i, j) + matA.At <double>(i, k) * matB.At <double>(k, j)); } } } return(result); }
/// <summary> /// Solve equation AX = Y /// </summary> private void ByMat() { // x + y = 10 // 2x + 3y = 26 // (x=4, y=6) double[,] av = {{1, 1}, {2, 3}}; double[] yv = {10, 26}; Mat a = new Mat(2, 2, MatType.CV_64FC1, av); Mat y = new Mat(2, 1, MatType.CV_64FC1, yv); Mat x = new Mat(); Cv2.Solve(a, y, x, DecompTypes.LU); Console.WriteLine("ByMat:"); Console.WriteLine("X1 = {0}, X2 = {1}", x.At<double>(0), x.At<double>(1)); }
public void ApplyMinMaxCalc(cv.Mat input) { double zMax = double.MinValue; double zMin = double.MaxValue; for (int x = 0; x < xSize; ++x) { for (int z = 0; z < zSize; ++z) { if (zMax < input.At <double>(x, z)) { zMax = input.At <double>(x, z); } if (zMin > input.At <double>(x, z)) { zMin = input.At <double>(x, z); } } } for (int x = 0; x < xSize; ++x) { for (int z = 0; z < zSize; ++z) { input.Set <float>(x, z, input.At <float>(x, z) - (float)zMin); input.Set <float>(x, z, input.At <float>(x, z) * 1000.0f / (float)(zMax - zMin)); MeshDataSeries[z, x] = input.Get <float>(x, z); } } }
static void MatShow(cv.Mat m, int dec, int wid) { for (int i = 0; i < m.Rows; ++i) { for (int j = 0; j < m.Cols; ++j) { double v = m.At <double>(i, j); if (Math.Abs(v) < 1.0e-5) { v = 0.0; // avoid "-0.00" } Console.Write(v.ToString("F" + dec).PadLeft(wid)); } Console.WriteLine(""); } }
static cv.Mat ExtractUpper(cv.Mat lum) { // upper part of an LU (lu values on diagional and above, 0.0s below) int n = lum.Rows; cv.Mat result = lum.Clone().SetTo(0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i <= j) { result.Set <double>(i, j, lum.At <double>(i, j)); } } } return(result); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); v[0] = (byte)(ReduceColor(v[0])); v[1] = (byte)(ReduceColor(v[1])); v[2] = (byte)(ReduceColor(v[2])); mat.Set <Vec3b>(yi, xi, v); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
private static Dictionary <string, double> TransformToDict(OpenCvSharp.Mat cvMat) { var dict = new Dictionary <string, double> { { "m11", cvMat.At <double>(0, 0) }, { "m12", cvMat.At <double>(0, 1) }, { "m21", cvMat.At <double>(1, 0) }, { "m22", cvMat.At <double>(1, 1) }, { "m13", Math.Round(cvMat.At <double>(0, 2), 0) }, { "m23", Math.Round(cvMat.At <double>(1, 2), 0) } }; return(dict); }
static double[] MatVecProd(cv.Mat m, double[] v) { int n = v.Length; if (m.Cols != n) { throw new Exception("non-comform in MatVecProd"); } double[] result = new double[n]; for (int i = 0; i < m.Rows; ++i) { for (int j = 0; j < m.Cols; ++j) { result[i] += m.At <double>(i, j) * v[j]; } } return(result); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < 16; yi++) { for (int xi = 0; xi < 16; xi++) { Vec3b max = new Vec3b(); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj); if (max[0] < v[0]) { max[0] = v[0]; } if (max[1] < v[1]) { max[1] = v[1]; } if (max[2] < v[2]) { max[2] = v[2]; } } } for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, max); } } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Mat changedMat = new Mat(); Mat changedMat1 = new Mat(); Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2HSV); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = changedMat.At <Vec3b>(yi, xi); Debug.Log(v[0]); v[0] = (byte)((v[0] - 180) % 360); changedMat.Set <Vec3b>(yi, xi, v); } } Cv2.CvtColor(changedMat, changedMat1, ColorConversionCodes.HSV2BGR); Texture2D changedTex = Unity.MatToTexture(changedMat1); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < 16; yi++) { for (int xi = 0; xi < 16; xi++) { Vector3 sum = new Vector3(); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj); sum[0] += v[0]; sum[1] += v[1]; sum[2] += v[2]; } } Debug.Log(sum[0]); Vec3b ave = new Vec3b(); ave[0] = (byte)(sum[0] / 64); ave[1] = (byte)(sum[1] / 64); ave[2] = (byte)(sum[2] / 64); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, ave); } } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
static cv.Mat ExtractLower(cv.Mat lum) { // lower part of an LU Crout's decomposition // (dummy 1.0s on diagonal, 0.0s above) int n = lum.Rows; cv.Mat result = lum.Clone().SetTo(0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) { result.Set <double>(i, j, 1.0); } else if (i > j) { result.Set <double>(i, j, lum.At <double>(i, j)); } } } return(result); }
void drawCamera() { // Configuration Scalar camera_color = new Scalar(255, 117, 44, 255); int camera_size = 10; int camera_height = window_height - camera_offset; Point camera_left_pt = new Point(window_width / 2 - camera_size / 2, camera_height); Point camera_right_pt = new Point(window_width / 2 + camera_size / 2, camera_height); // Drawing camera List <Point> camera_pts = new List <Point> { new Point(window_width / 2 - camera_size, camera_height), new Point(window_width / 2 + camera_size, camera_height), new Point(window_width / 2 + camera_size, camera_height + camera_size / 2), new Point(window_width / 2 - camera_size, camera_height + camera_size / 2) }; Cv2.FillConvexPoly(background, camera_pts, camera_color); // Compute the FOV if (fov < 0.0f) { computeFOV(); } // Get FOV intersection with window borders float z_at_x_max = x_max / (float)Math.Tan(fov / 2.0f); Point left_intersection_pt = toCVPoint(x_min, -z_at_x_max), right_intersection_pt = toCVPoint(x_max, -z_at_x_max); Scalar clr = camera_color; // Draw FOV // Second try: dotted line LineIterator left_line_it = new LineIterator(background, camera_left_pt, left_intersection_pt, PixelConnectivity.Connectivity8); int i = 0; foreach (var it in left_line_it) { Point current_pos = it.Pos; /*if (i % 5 == 0 || i % 5 == 1) * { * it.SetValue<Scalar>(clr); * }*/ for (int r = 0; r < current_pos.Y; ++r) { float ratio = (float)r / camera_height; background.At <Vec4b>(r, current_pos.X) = Utils.applyFading(background_color, ratio, fov_color); } i++; } LineIterator right_line_it = new LineIterator(background, camera_right_pt, right_intersection_pt, PixelConnectivity.Connectivity8); int j = 0; foreach (var it in right_line_it) { Point current_pos = it.Pos; /*if (j % 5 == 0 || j % 5 == 1) * { * it.SetValue<Scalar>(clr); * }*/ for (int r = 0; r < current_pos.Y; ++r) { float ratio = (float)r / camera_height; background.At <Vec4b>(r, current_pos.X) = Utils.applyFading(background_color, ratio, fov_color); } j++; } for (int c = window_width / 2 - camera_size / 2; c <= window_width / 2 + camera_size / 2; ++c) { for (int r = 0; r < camera_height; ++r) { float ratio = (float)r / camera_height; background.At <Vec4b>(r, c) = Utils.applyFading(background_color, ratio, fov_color); } } }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); float[] results = new float[256]; float[,] grs = new float[mat.Height, mat.Width]; for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0]; grs[yi, xi] = gr; } } for (int thi = 1; thi < 255; thi++) { int w0 = 0; int w1 = 0; float M0 = 0; float M1 = 0; foreach (float gr in grs) { if (gr < thi) { w0++; M0 += gr; } else { w1++; M1 += gr; } } Debug.Log(w0 + w1); float tmp0 = w0 == 0 ? 0 : M0 / w0; float tmp1 = w1 == 0 ? 0 : M1 / w1; results[thi] = ((float)w0 / (mat.Height * mat.Width)) * ((float)w1 / (mat.Height * mat.Width)) * Mathf.Pow(tmp0 - tmp1, 2); } int z = 0; for (int i = 1; i < 255; i++) { if (results[i] > results[z]) { z = i; } } for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { if (grs[yi, xi] < z) { Vec3b v = new Vec3b(); v[0] = (byte)0; v[1] = (byte)0; v[2] = (byte)0; mat.Set <Vec3b>(yi, xi, v); } else { Vec3b v = new Vec3b(); v[0] = (byte)255; v[1] = (byte)255; v[2] = (byte)255; mat.Set <Vec3b>(yi, xi, v); } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
static int MatDecompose(cv.Mat m, out cv.Mat lum, out int[] perm) { // Crout's LU decomposition for matrix determinant and inverse // stores combined lower & upper in lum[][] // stores row permuations into perm[] // returns +1 or -1 according to even or odd number of row permutations // lower gets dummy 1.0s on diagonal (0.0s above) // upper gets lum values on diagonal (0.0s below) int toggle = +1; // even (+1) or odd (-1) row permutatuions int n = m.Rows; // make a copy of m[][] into result lum[][] lum = m.Clone(); // make perm[] perm = new int[n]; for (int i = 0; i < n; ++i) { perm[i] = i; } for (int j = 0; j < n - 1; ++j) // process by column. note n-1 { double max = Math.Abs(lum.At <double>(j, j)); int piv = j; for (int i = j + 1; i < n; ++i) // find pivot index { double xij = Math.Abs(lum.At <double>(i, j)); if (xij > max) { max = xij; piv = i; } } // i if (piv != j) { cv.Mat tmp = lum.Row(piv).Clone(); // swap rows j, piv lum.Row(j).CopyTo(lum.Row(piv)); tmp.CopyTo(lum.Row(j)); int t = perm[piv]; // swap perm elements perm[piv] = perm[j]; perm[j] = t; toggle = -toggle; } double xjj = lum.At <double>(j, j); if (xjj != 0.0) { for (int i = j + 1; i < n; ++i) { double xij = lum.At <double>(i, j) / xjj; lum.Set <double>(i, j, xij); for (int k = j + 1; k < n; ++k) { lum.Set <double>(i, k, lum.At <double>(i, k) - xij * lum.At <double>(j, k)); } } } } return(toggle); // for determinant }